Spring中的@ConditionalOnProperty注解
1.概述
介绍@ConditionalOnProperty注解的主要目的。
2.@ConditionalOnProperty的目的
通常,在开发基于Spring的应用程序时,可能需要根据配置属性的存在和值有条件地创建一些bean。
例如,取决于是否将属性值设置为“ prod”或“ test”,可能想要注册一个DataSource bean来指向生产或测试数据库。
幸运的是,实现这一目标并不像想象的那样困难。 Spring框架正是为此目的提供了@ConditionalOnProperty注解。
简而言之,@ConditionalOnProperty仅在存在环境属性且具有特定值的情况下才启用Bean注册。 默认情况下,必须定义指定的属性,并且不等于false。
现在已经熟悉了@ConditionalOnProperty注解的用途,接下来深入地了解它的工作原理。
3.实践中的@ConditionalOnProperty注解
为了举例说明@ConditionalOnProperty的用法,接下来开发一个基本的通知系统。 为了使事情现在变得简单,假设要发送电子邮件通知。
首先,需要创建一个简单的服务来发送通知消息。 例如,考虑NotificationSender接口:
public interface NotificationSender {String send(String message);
}
接下来,提供NotificationSender接口的实现,以发送电子邮件:
public class EmailNotification implements NotificationSender {@Overridepublic String send(String message) {return "Email Notification: " + message;}
}
现在,看看如何使用@ConditionalOnProperty注解。 以这样的方式配置NotificationSender Bean,使其仅在定义了属性notification.service时才加载:
@Bean(name = "emailNotification")
@ConditionalOnProperty(prefix = "notification", name = "service")
public NotificationSender notificationSender() {return new EmailNotification();
}
prefix和name属性用于表示应检查的配置属性。
最后,需要在application.properties文件中定义的自定义属性:
notification.service=email
4.高级配置
正如前面已经了解的那样,@ConditionalOnProperty注解允许根据配置属性的存在条件有条件地注册bean。
使用此注释,可以做更多的事情。
假设要添加另一个通知服务,例如,一个允许发送SMS通知的服务。
为此,需要创建另一个NotificationSender实现:
public class SmsNotification implements NotificationSender {@Overridepublic String send(String message) {return "SMS Notification: " + message;}
}
由于有两个实现,看看如何使用@ConditionalOnProperty有条件地加载正确的NotificationSender bean。
注解提供了havingValue属性。 非常有趣的是,它定义了一个属性必须具有的值,以便将特定的Bean添加到Spring容器中。
现在,指定要在什么条件下在上下文中注册SmsNotification实现:
@Bean(name = "smsNotification")
@ConditionalOnProperty(prefix = "notification", name = "service", havingValue = "sms")
public NotificationSender notificationSender2() {return new SmsNotification();
}
借助于hadingValue属性,清楚地表明,仅当notification.service设置为sms时,才希望加载SmsNotification。
@ConditionalOnProperty具有另一个名为matchIfMissing的属性。 此属性指定在属性不可用的情况下条件是否应匹配。
单元测:
public class NotificationUnitTest {private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();@Testpublic void sender() {this.contextRunner.withPropertyValues("notification.service=email").withUserConfiguration(NotificationConfig.class).run(context -> {assertThat(context).hasBean("emailNotification");NotificationSender notificationSender = context.getBean(EmailNotification.class);assertThat(notificationSender.send("Hello From spring!")).isEqualTo("Email Notification: Hello From spring!");assertThat(context).doesNotHaveBean("smsNotification");});}
}
Spring中的@ConditionalOnProperty注解相关推荐
- Spring中的@Value注解详解
本文主要介绍Spring @Value 注解注入属性值的使用方法的分析,文章通过示例代码非常详细地介绍,对于每个人的学习或工作都有一定的参考学习价值 文章目录 概述 使用方式 基于配置文件的注入 基于 ...
- Spring中利用java注解声明切面
Spring中利用java注解声明切面 第一步:确定在Spring的XML文件中包含AOP的命名空间: 第二步:在Spring的XML文件中输入<aop:aspectj-autoproxy/&g ...
- Spring中常见的注解收集
Spring中常见的注解收集 文章目录 Spring中常见的注解收集 1.@ResponseBody 2.@RequestBody 3.@RequestParam 4.@Controller 5.@R ...
- Spring 中的各种注解,光会用可不够哦!
来源:https://digdeep.cnblogs.com/digdeep/p/4525567.html 1. Java中的注解 2. 使用 元注解 来自定义注解 和 处理自定义注解 3. spri ...
- 详细讲解Spring中的@Bean注解
点击关注公众号,实用技术文章及时了解 来源:blog.csdn.net/weixin_42140261/ article/details/104864333 随着SpringBoot的流行,我们现在更 ...
- Spring中如何使用注解来配置Bean?有哪些相关的注解?
首先需要在Spring配置文件中增加如下配置: <context:component-scan base-package="org.example"/> 然后可以用@C ...
- 在spring中使用自定义注解注册监听器
2019独角兽企业重金招聘Python工程师标准>>> 接口回调 监听器本质上就是利用回调机制,在某个动作发生前或后,执行我们自己的一些代码.在Java语言中,可以使用接口来实现. ...
- 五、Spring中的@Import注解
一.使用@Import注解导入组件 @Import注解的作用是给容器中导入组件,回顾下我们给容器中导入组件的方式,可以通过Spring的xm配置方式,可以通过注解,如@Component等,也可以通过 ...
- spring中@Value的注解解析
@Value的注解是通过AutowiredAnnotationBeanPostProcessor来处理的. 其处理时序为 其构造函数中添加了支持的注解类型 AutowireCandidateResol ...
最新文章
- spring第五讲:aop
- linux下文件操作之cp和mv
- javascript console自动点击页面元素
- CentOS7搭建ftp服务器(vsftpd,亲测成功)
- k8s配置以使得pod可以运行于master上
- 65 年来,全英国向他道歉三次,图灵,计算机人不能忘记的男人
- 在Anaconda上安装Caffe 和 CUDA
- 闭包函数 use 改变外部变量
- matlab-自控原理 已知x~=Ax+Bu中的AB矩阵和X0,求单位输入下的时间响应
- 关于log4j的配置
- 缓存块着色算法和优化的缓存块着色算法
- 微信 vue html缓存,解决微信浏览器缓存站点入口文件(IIS部署Vue项目)_唇印_前端开发者...
- 熊出没之奇幻空间里面的机器人图片_《熊出没之奇幻空间》里面令人触动的两个角色...
- 计算机鼠标装有,计算机插入鼠标时无法安装设备驱动程序的解决方法
- No matching provisioning profile found: Your build settings specify a provisioning profile with the
- Telephony 拨号流程
- 校招Java后端开发面经专栏——序
- 【亲测有效】树莓派4B安装realsense(Intel深度摄像头)
- Solaris 问答集
- 神都夜行录无法显示服务器,神都夜行录这款手游都有哪些渠道服?神都夜行录服务器汇总介绍...