Spring 4.0新功能:@Conditional注解详细介绍

 更新时间:2017年09月06日 10:29:06   作者:沈子平  
Spring Boot的强大之处在于使用了Spring 4框架的新特性:@Conditional注释,此注释使得只有在特定条件满足时才启用一些配置。下面这篇文章主要给大家介绍了关于Spring4.0中新功能:@Conditional注解的相关资料,需要的朋友可以参考下。

前言

最近在学习spring,抽空会将学习的知识总结下面,本文我们会接触spring 4的新功能:@Conditional注解。在之前的spring版本中,你处理conditions只有以下两个方法:

  • 在3.1版本之前,你需要使用spring expression language
  • 在3.1版本发布时,profiles被引入来处理conditions。

让我们分别看看以上两者,在来理解spring 4带来的@Conditional注解。

Spring Expression Language(SPeL)

SPeL的三元标识符(IF-THEN-ELSE)可以在spring配置文件中用来表达条件语句。

<bean id="flag">
 <constructor-arg value="#{systemProperties['system.propery.flag'] ?: false }" />
</bean>
<bean id="bean">
 <property name="property" value="#{ flag ? 'yes' : 'no' }"/>
</bean>

这个bean的属性依赖于flag的值,该值是使用外部属性注入的,这样bean就具有了动态的能力。

使用 Profiles

这是在spring 3.1引入的。像下面这样使用。

<!-- default configuration - will be loaded if no profile is specified -->
<!-- This will only work if it's put at the end of the configuration file -->
<!-- so no bean definitions after that -->
<beans profile="default">
  <import resource="classpath:default.xml" />
</beans>
<!-- some other profile -->
<beans profile="otherProfile">
 <import resource="classpath:other-profile.xml" />
</beans>

使用spring 4的@Conditional注解

现在介绍@Conditional注解。官方文档的说明是“只有当所有指定的条件都满足是,组件才可以注册”。主要的用处是在创建bean时增加一系列限制条件。

Conditional接口的声明如下:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE, ElementType.METHOD)
public @interface Conditional{
 Class <!--?extends Condition-->[] value();
}

所以@Conditional注解使用方法如下

  • 类型级别,可以在@Component 或是 @Configuration类上使用
  • 原型级别,可以用在其他自定义的注解上
  • 方法级别,可以用在@Bean的方法上

如果一个@Configuration类使用了@Conditional,会影响所有@Bean方法和@Import关联类

public interface Condition{
/** Determine if the condition matches.
* @param context the condition context
* @param metadata meta-data of the {@link AnnotationMetadata class} or
* {@link Method method} being checked.
* @return {@code true} if the condition matches and the component can be registered
* or {@code false} to veto registration.
*/
boolean matches(ConditionContext context, AnnotatedTypeMedata metadata);
}

下面是一个例子

public class SystemPropertyCondition implements Condition {
 @Override
 public boolean matches(ConditionContext context, AnnotatedTypeMetadata   metadata) {
  return (System.getProperty("flag") != null);
 }
}

class SystemPropertyAbsentCondition implements Condition {
 @Override
 public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
  return (System.getProperty("flag") == null);
 }
}

这里我们有两个类:SystemPropertyCondition和SystemPropertyAbsentCondtion. 这两个类都实现了Condition接口.覆盖的方法基于属性flag返回一个布尔值。

现在我们定义两个类,一个是positive条件,一个是negative条件:

@Bean
@Conditional(SystemPropertyCondition.class)
public SampleService service1() {
 return new SampleServiceImpl1();
}

@Bean
@Conditional(SystemPropertyAbsentCondition.class)
public SampleService service2() {
 return new SampleServiceImpl2();
}

上面提到的profiles已经通过conditional原型注解进行了修改。

总结

本文介绍了spring 4的conditianal注解。注意condition注解是不会继承的。如果一个父类使用了conditional注解,其子类是不会拥有conditions的。如果你动手尝试以上的例子,会帮助你获得更好的理解。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

相关文章

  • 解决引入spring-cloud-starter-openfeign后部分类找不到的问题

    解决引入spring-cloud-starter-openfeign后部分类找不到的问题

    这篇文章主要介绍了解决引入spring-cloud-starter-openfeign后部分类找不到的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • Java详细分析讲解泛型

    Java详细分析讲解泛型

    在正式进入内容之前说明一下:泛型的内容太多,也太复杂。这里因为Java中写数据结构的时候会使用到,所以加上。关于泛型我找了挺多文章,再结合自己的理解,尽可能将其讲清楚。不求会使用泛型,只要求后面数据结构出现泛型的时候能够知道是在干什么即可
    2022-05-05
  • Java异常处理机制try catch流程详解

    Java异常处理机制try catch流程详解

    这篇文章主要介绍了Java异常处理机制try catch流程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • Mybatis-Plus 动态表名的实践

    Mybatis-Plus 动态表名的实践

    本文主要介绍了Mybatis-Plus 动态表名的实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-08-08
  • java7钻石语法知识点总结

    java7钻石语法知识点总结

    在本篇文章里小编给大家整理的是关于java7钻石语法的相关知识点内容,有需要的朋友们参考下。
    2019-11-11
  • 修改idea运行内存大小的方法总结

    修改idea运行内存大小的方法总结

    在开发过程中,总会遇到idea运行内存不足,所以本文小编给大家介绍了修改idea运行内存大小的两种方法,文中通过图文给大家讲解的非常详细,需要的朋友可以参考下
    2023-12-12
  • Java 中DateUtils日期工具类的实例详解

    Java 中DateUtils日期工具类的实例详解

    这篇文章主要介绍了Java 中DateUtils日期工具类的实例详解的相关资料,有时候开发java项目使用日期类型,这里介绍下日期工具类,需要的朋友可以参考下
    2017-08-08
  • java实现仿windows 字体设置选项卡实例

    java实现仿windows 字体设置选项卡实例

    本篇文章介绍了java仿windows 字体设置选项卡,可实现类似windows字体设置效果,需要的朋友可以参考下。
    2016-10-10
  • Java线程间共享与协作详细介绍

    Java线程间共享与协作详细介绍

    这篇文章主要介绍了Java线程间共享与协作详细介绍,Java 支持多个线程同时访问一个对象或者对象的成员变量,更多相关介绍需要的朋友可以参考一下
    2022-09-09
  • springsecurity 企业微信登入的实现示例

    springsecurity 企业微信登入的实现示例

    本文主要介绍了springsecurity 企业微信登入的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04

最新评论