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


猜你喜欢
- 目前Android 发展至今优秀的图片加载框架太多,例如: Volley ,Picasso,Imageloader,Glide等等。但是作为
- public class MainActivity extends Activity { TextView tv; Ch
- 记录一下微信第三方实现登录的方法。还是比较简单。一、必要的准备工作1.首先需要注册并被审核通过的微信开放平台帐号,然后创建一个移动应用,也需
- 在学习Android开发的过程你,你往往会去借鉴别人的应用是怎么开发的,那些漂亮的动画和精致的布局可能会让你爱不释手,作为一个开发者,你可能
- 开发环境使用jdk1.8.0_60,把springboot 项目打成war包后,部署到apache-tomcat-7.0.68时报错如下,换
- 本文实例讲述了Java正则验证正整数的方法。分享给大家供大家参考,具体如下:package des;import java.util.reg
- 本文实例讲述了Android开发之App widget用法。分享给大家供大家参考,具体如下:放在桌面上的控件叫做——App widget,例
- 现在许多系统的注册、登录或者发布信息模块都添加的随机码功能,就是为了避免自动注册程序或者自动发布程序的使用。验证码实际上就是随机选择一些字符
- 大家好,我是老三,Spring是我们最常用的开源框架,经过多年发展,Spring已经发展成枝繁叶茂的大树,让我们难以窥其全貌。这节,我们回归
- 效果 使用compile 'site.gemus:openingstartanimation:1.0.0' //在gra
- Java集合是java提供的工具包,包含了常用的数据结构:集合、链表、队列、栈、数组、映射等。Java集合工具包位置是java.util.*
- 项目完整代码链接:代码链接跨服务上传文件示意图一、创建项目springboot:2.2.6JDK:1.8由于资源有限,就用不同端口表示不同服
- SpringBoot 整合RabbitMq 自定义消息监听容器来实现消息批量处理前言RabbitMQ是一种常用的消息队列,Spring Bo
- 上一篇我们学习了自定义ViewGroup的基本步骤,并做了一个CustomGridLayout的实例,这篇我们继续来说说自定义ViewGro
- 1、概述 限流的含义是在单位时间内确保发往某个模块的请求数量小于某个数值,比如在实现秒杀功能时,需要确保在10秒内发往支付模块的请求数量小
- 前言Docker旨在提供一种应用程序的自动化部署解决方案,在 Linux 系统上迅速创建一个容器(轻量级虚拟机)并部署和运行应用程序,并通过
- 当我们拿到一大段JSON字符串的时候,分析起来简直头皮发麻,相信很大一部分朋友也都会直接去BEJSON等网站去做一个JSON格式化,已方便自
- 本文实例讲述了Android控件之CheckBox、RadioButton用法。分享给大家供大家参考。具体如下:CheckBox和Radio
- 本文实例为大家分享了WPF ProgressBar实现实时进度的具体代码,供大家参考,具体内容如下简单测试,页面如图:利用上班的一点点空闲时
- 前言:Timer是一个定时器,作为C#开发Timer控件是我们用的比较多的一个控件,它的功能很简单,但是也是值得我们去学习的一个知识点,今天