conditional_如何:在Spring中使用@Conditional和Condition注册组件
conditional
Spring中的@Profile
批注可用于任何自动检测候选对象的Spring组件(例如, @Service
Component, @Service
@Component
, @Service
@Configuration
等)。 @Profile
批注接受单个配置文件或一组必须是活动的配置文件,以使带注释的组件有资格进行自动检测。 对于给定的@Profile({"p1", "!p2"})
,如果配置文件p1
处于活动状态或配置文件p2
不处于活动状态,则会进行注册。 或至关重要。
但是,如何使用@Profile
实现此目标:如果配置文件p1
处于活动状态,并且配置文件p2
和 p3
均处于非活动状态,我们想激活给定的组件吗?
让我们假设以下情况:我们有一个NotificationSender
接口,该接口由以下方式实现:
SendGridNotificationSender
–仅在sendgrid
配置文件处于活动状态时才处于活动状态,EmailNotificationSender
–仅在email
配置文件处于活动状态时才活动。NoOpNotificationSender
–仅在development
配置文件处于活动状态且sendgrid
和email
没有处于活动状态时才处于活动状态。
另外:一次只能注册一个NotificationSender
, development
配置文件可以与sendgrid
和email
配置文件结合使用。
在上述情况下,使用@Profile
注释似乎还不够。 也许我使事情变得有些复杂,但实际上我真的很想实现上述目标而没有介绍其他配置文件。 我是怎么做到的?
我使用了Spring的4 @Conditional
注释。 当所有指定Condition
匹配时, @Conditional
允许注册组件:
@Component
@Conditional(value = NoOpNotificationSender.ProfilesCondition.class)
class NoOpNotificationSender extends NotificationSenderAdapter {}
ProfilesCondition
实现org.springframework.context.annotation.Condition
接口:
public static class ProfilesCondition implements Condition {@Overridepublic boolean matches(ConditionContext c, AnnotatedTypeMetadata m) {}
}
问题的整体解决方案:
@Component
@Conditional(value = NoOpNotificationSender.ProfilesCondition.class)
class NoOpNotificationSender extends NotificationSenderAdapter {static class ProfilesCondition implements Condition {@Overridepublic boolean matches(ConditionContext c, AnnotatedTypeMetadata m) {return accepts(c, Profiles.DEVELOPMENT)&& !accepts(c, Profiles.MAIL)&& !accepts(c, Profiles.SEND_GRID);}private boolean accepts(ConditionContext c, String profile) {return c.getEnvironment().acceptsProfiles(profile);}}
}
当适当的配置文件处于活动状态时,其他组件将被激活:
@Component
@Profile(value = Profiles.SEND_GRID)
public class SendGridNotificationSender extends NotificationSenderAdapter {}@Component
@Profile(value = Profiles.MAIL)
class EmailNotificationSender extends NotificationSenderAdapter {}
用法示例:
活动资料 | 豆 |
---|---|
发展 | NoOpNotificationSender |
开发,sendgrid | SendGridNotificationSender |
开发,邮件 | EmailNotificationSender |
sendgrid | SendGridNotificationSender |
邮件 | EmailNotificationSender |
你怎么看? 您将如何解决这个问题?
翻译自: https://www.javacodegeeks.com/2015/11/register-components-using-conditional-condition-spring.html
conditional
conditional_如何:在Spring中使用@Conditional和Condition注册组件相关推荐
- 如何:在Spring中使用@Conditional和Condition注册组件
Spring中的@Profile批注可以用于任何自动检测候选的Spring组件(例如, @Service Component, @Service @Component , @Service @Conf ...
- 四、Spring中使用@Conditional按照条件注册Bean
以前其实是写过@Condtional注解的笔记的,这里附上链接: Spring中的@conditional注解 但已经忘记的差不多了,所以今天再重新学习下,可以互补着学习 @Contional:按照一 ...
- 给容器中注册组件 || @Scope -- @Lazy -- @Conditional({Condition}) -- @Import--使用Spring提供的 FactoryBean
* @Scope:调整作用域 * prototype:多实例的:ioc容器启动并不会去调用方法创建对象放在容器中. * 每次获取的时候才会调用方法创建对象: ...
- 惊呆了,Spring中竟然有12种定义bean的方法
前言 在庞大的 Java 技术体系中,Spring 有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜. 我们都知道 Spring 是创建和管理bean的工厂,它提供了多种方式定义 bean,能 ...
- spring中那些让你爱不释手的代码技巧
紧接上文<spring中这些能升华代码的技巧,可能会让你爱不释手>.本文继续总结我认为spring中还不错的知识点,希望对您有所帮助. 一. @Conditional的强大之处 不知道你们 ...
- 厉害了,Spring中bean的12种定义方法!
前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...
- Spring中注解注入bean和配置文件注入bean
注解的方式确实比手动写xml文件注入要方便快捷很多,省去了很多不必要的时间去写xml文件 按以往要注入bean的时候,需要去配置一个xml,当然也可以直接扫描包体,用xml注入bean有以下方法: 1 ...
- java快速注释怎么配置_详解如何在低版本的Spring中快速实现类似自动配置的功能...
在 Spring 4 后才引入了 @Conditional 等条件注解,它是 Spring Boot 中实现自动配置的最大功臣! 那么问题来了:如果我们还在使用 Spring 3.x 的老版本,这时候 ...
- Spring中的一些常用注解
目录 1. @Autowired 2. @Qualifier 3. @Resource 4. @Component 5. @Value 6. @Bean 7. 声明bean的几个注解 7.1 @Ser ...
最新文章
- ios中图片不显示的问题
- mt4指标最精准组合指标_股市最赚钱的黄金指标组合:KDJ+MACD指标的配合使用,助于买在低点卖在高点!...
- Markdown创建页面和目录?
- (转)【超详细教程】使用Windows Live Writer 2012和Office Word 2013 发布文章到博客园全面总结...
- MySQL高级 - 复制 - 集群搭建
- python 百度ai批量识别_Python基于百度AI的文字识别的示例
- NAR | ZKSCAN3延缓人干细胞衰老的表观遗传机制
- 基于Android平台淘宝网手机客户端实战开发(共四季,涉及各类技术应用)
- java ini_java读取ini格式的文件
- 探究光栅图像学之水纹渲染与折射滤镜
- jar httpclient 少包,此处 区别 common-http包
- Netty2:粘包/拆包问题与使用LineBasedFrameDecoder的解决方案
- pythonATM,购物车项目实战_补充9文档说明
- tp-link与台式计算机连接教程,台式电脑连接到无线路由器上网教程
- CentOS7.X安装部署Glassfish4.1.2
- 数据结构--算法达人修炼学习安排及方法指导
- Qt捕获键盘按键消息
- wordpress 瀑布流ajax,WordPress瀑布流主题:蛋花儿Free版
- 我的MSN机器人终于有了自己的头像
- c语言之奇偶数分开排序
热门文章
- P3329-[ZJOI2011]最小割【最小割树】
- jzoj3301-[集训队互测2013]家族【并查集,暴力】
- 2021牛客暑期多校训练营8 F-Robots(bitset优化dp)
- codeforces1496 D. Let‘s Go Hiking(乱搞+讨论)
- Codeforces Round #658 (Div. 2)
- [NOI2014] 起床困难综合症(二进制,贪心)
- 一文搞定 Spring Data Redis 详解及实战
- 响应式布局(手机端)
- 【Servlet】获取并输出服务器获得的数据
- C++描述杭电OJ 2011.多项式求和 ||