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

作者:沈子平 时间:2022-01-19 06:37:35 

前言

最近在学习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的。如果你动手尝试以上的例子,会帮助你获得更好的理解。

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

来源:https://segmentfault.com/a/1190000011033012

标签:spring4.0,@conditional注解
0
投稿

猜你喜欢

  • 详解Struts2中json 相互引用死循环解决办法

    2022-09-08 04:04:46
  • spring retry实现方法请求重试的使用步骤

    2021-12-31 15:11:11
  • Java中避免空指针异常的方法

    2023-05-08 21:00:27
  • Java利用Selenium操作浏览器的示例详解

    2022-06-17 17:34:20
  • java 字符串截取的三种方法(推荐)

    2021-05-24 06:49:52
  • Java建造者模式构建复杂对象的最佳实践

    2021-06-27 10:07:16
  • ClassLoader类加载源码解析

    2023-11-25 18:17:09
  • springmvc @RequestBody String类型参数的使用

    2023-03-08 09:30:05
  • Android TextView中文字通过SpannableString设置属性用法示例

    2023-07-26 07:11:51
  • C# Winform实现捕获窗体最小化、最大化、关闭按钮事件的方法

    2021-10-18 08:01:29
  • Java 超详细讲解数据结构中的堆的应用

    2021-08-19 08:10:53
  • maven打包如何指定jdk的版本

    2022-12-21 20:59:20
  • ElasticSearch学习之ES Mapping实战示例

    2023-11-25 06:12:25
  • Java Selenium实现多窗口切换的示例代码

    2022-01-22 22:48:08
  • C#多线程之任务的用法详解

    2023-08-27 10:51:18
  • SpringBoot后端进行数据校验JSR303的使用详解

    2022-02-19 01:15:31
  • 关于springboot集成阿里云短信的问题

    2023-08-23 09:46:15
  • 关于activemq安装配置以及启动错误的解决

    2023-11-13 05:18:02
  • Java实现仿淘宝滑动验证码研究代码详解

    2022-12-28 00:51:11
  • Java执行hadoop的基本操作实例代码

    2022-11-13 12:08:04
  • asp之家 软件编程 m.aspxhome.com