conditional系列常用注解
文章目录
- 背景
- 注解
- @Conditional
- @ConditionalOnProperty
- @ConditionalOnBean
- @ConditionalOnClass
- @ConditionalOnMissingBean
- @ConditionalOnMissingClass
- @ConditionalOnExpression
- @ConditionalOnResource
背景
在spring场景下,@Conditional系列注解是在特定的条件下⼀些bean是否被实例化并加载到spring容器。
注解
@Conditional
说明:实现Condition接口的matches,matches方法返回true则注入bean,false则不注入
@Configuration
public class BeanConfig {//若是WindowsCondition的实现方法返回true,则注入这个bean @Conditional({CustomCondition.class})@Bean(name = "user")public User user(){return new User("admin");}
}
创建CustomCondition类并实现Condition接口
public class CustomCondition implements Condition {@Overridepublic boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {//获取ioc使⽤的beanFactoryConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();ClassLoader classLoader = conditionContext.getClassLoader();Environment environment = conditionContext.getEnvironment();BeanDefinitionRegistry registry = conditionContext.getRegistry();String property = environment.getProperty("user.username");if ("admin".equals(property)){// 当user.username的属性值为admin时注入 return true;}return false;
}
@ConditionalOnProperty
说明:读取application.properties属性值并决定是否加载bean
@ConditionalOnProperty(prefix = "user", value = {"username"}, havingValue = "admin", matchIfMissing = true)@Bean(name = "user")public User user(){return new User("admin");}
@ConditionalOnProperty注解中的属性解释:
// 数组,获取对应property名称的值,与name不可同时使用
String[] value() default {};
// 配置属性名称的前缀,比如spring.http.encoding
String prefix() default “”;
// 数组,配置属性完整名称或部分名称
// 可与prefix组合使用,组成完整的配置属性名称(如spring.http.encoding.xxx),与value不可同时使用
String[] name() default {};
// 可与name组合使用,比较获取到的属性值与havingValue给定的值是否相同,相同才加载配置
(注, value/name = {“username”}只有一个值时才生效,判断这个属性值和havingValue中指定的值是否一样)
String havingValue() default “”;
// 缺少该配置属性时是否可以加载。如果为true,没有该配置属性时也会正常加载;反之则不会生效
boolean matchIfMissing() default false;
@ConditionalOnBean
说明: 仅仅在当前上下文中存在某个对象时,才会实例化一个Bean
//RedisOperBean依赖redisTemplate
@Component
@ConditionalOnBean(name="redisTemplate")
public class RedisOperBean {private final RedisTemplate redisTemplate;public RedisOperBean(RedisTemplate redisTemplate) {// ...}
}
@ConditionalOnClass
说明:某个class位于类路径上,才会实例化一个Bean,要求指定的class必须存在
@ConditionalOnMissingBean
说明:在当前上下文中不存在某个对象时,才会实例化一个Bean
@ConditionalOnMissingClass
说明: 某个class类路径上不存在的时候,才会实例化一个Bean
@ConditionalOnExpression
可通过spring提供的spEL表达式灵活配置,当表达式为true的时候,才会实例化一个Bean
@Configuration
@ConditionalOnExpression("${log.enabled:false}")
public class CustomConfiguration {@Beanpublic Custom custom(ConfigContext configContext) {return new Custom(configContext);}
}
其他写法
@ConditionalOnExpression("${log.enabled}==1&&${test.enabled:true}")@ConditionalOnExpression("'${user.username}'.equals('admin')")
@ConditionalOnResource
说明:String[] resources参数指定的静态资源⽂件存在 才加载
conditional系列常用注解相关推荐
- SpringBoot @Conditional系列注解
目录 @ConditionalOnClass.@ConditionalOnMissingClass @ConditionalOnBean.@ConditionalOnMissingBean @Cond ...
- 详解Spring Boot框架中的@Conditional系列注解
目录 1. @Conditional 注解 2. Spring boot 扩展 1) @ConditionalOnClass和@ConditionalOnMissingClass注解 2) @Cond ...
- Spring系列之Spring常用注解总结
参看博客:https://www.cnblogs.com/xiaoxi/p/5935009.html 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺 ...
- autowired注解_Spring系列之Spring常用注解总结
作者:平凡希来源:https://www.cnblogs.com/xiaoxi/p/5935009.html 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做 ...
- sessionattribute 被spring 扫描不到_Spring 系列之 Spring 常用注解总结(肝硬化的干货)...
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1. 如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml ...
- Spring系列之Spring常用注解总结 原文:https://www.cnblogs.com/xiaoxi/p/5935009.html
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文 ...
- 40 个 Spring Boot 常用注解
以下文章来源方志朋的博客,回复"666"获面试宝典 作者 | 谭朝红 链接 | ramostear.com 一.Spring Web MVC 与 Spring Bean 注解 Sp ...
- 40 个 SpringBoot 常用注解
以下文章来源方志朋的博客,回复"666"获面试宝典 来源:https://ramostear.com/ 一.Spring Web MVC 与 Spring Bean 注解 Spri ...
- Spring MVC常用注解说明
2019独角兽企业重金招聘Python工程师标准>>> 使用Spring MVC的注解及其用法和其它相关知识来实现控制器功能. 02 之前在使用Struts2实现MV ...
最新文章
- 无法提升彼此,夫妻关系就要终结?
- Leetcode 376. 摆动序列 解题思路及C++实现
- forward 和redirect的区别
- 图片上传之后清空_OSS文件上传及OSS与ODPS之间数据连通
- PP团队圣经巨著《Application Architecture Guide2.0》24章-Web程式开发向导
- 机器学习也可以在线做!
- 【C语言笔记初级篇】第一章:C语言快速入门及注意事项
- 【协议相关】一些可用的RTMP,HTTP等源
- 我的大学--单片机高手郭天祥的学习心得体会
- 一步一步定制自己的google map(各个省市的经纬度查询)
- 经典网络建设方案案例
- ngx.var与ngx.ctx的区别
- 新磁盘分区格式化挂载
- Linux常用过滤命令之grep命令的使用
- http工作中常见的状态码
- 二、buildroot菜单配置
- 全国二级c语言库理论,全国计算机等级考试二级C语言理论基础习.doc
- 转行软件测试,简简单单3个月出来就是高薪工作,靠谱吗?我的第一桶金...
- 如何向瑞芯微平台添加驱动
- 2022年服装进销存软件排行榜重磅出炉!