@Conditional系列注解例子
1. @Conditional
说明:指定的Condition实现类,matches方法返回true则注入bean,false则不注入
@Configuration
public class BeanConfig { //只有一个类时,大括号可以省略
//如果WindowsCondition的实现方法返回true,则注入这个bean
@Conditional({WindowsCondition.class})
@Bean(name = "bill")
public Person person1(){
return new Person("Bill Gates",62);
} //如果LinuxCondition的实现方法返回true,则注入这个bean
@Conditional({LinuxCondition.class})
@Bean("linus")
public Person person2(){
return new Person("Linus",48);
}
}
创建 LinuxCondition和 WindowsCondition类,并实现Condition接口
public class LinuxCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
Environment environment = conditionContext.getEnvironment();
String property = environment.getProperty("os.name");
if (property.contains("Linux")){
return true;
}
return false;
}
}
2. @ConditionalOnBean
说明:仅仅在当前上下文中存在某个对象时,才会实例化一个Bean
//RedisOperBean依赖redisTemplate
@Component
@ConditionalOnBean(name="redisTemplate")
public class RedisOperBean {
private final RedisTemplate redisTemplate;
public RedisOperBean(RedisTemplate redisTemplate) {
// ...
}
}
3. @ConditionalOnClass
说明:某个class位于类路径上,才会实例化一个Bean,要求指定的class必须存在
4. @ConditionalOnProperty
说明:
(1)matchIfMissing:从application.properties中读取某个属性值,如果该值为空,默认值为true
(2)havingValue:通过其两个属性name以及havingValue来实现的,其中name用来从application.properties中读取某个属性值,如果该值为空,则返回false;
如果值不为空,则将该值与havingValue指定的值进行比较,如果一样则返回true;否则返回false。如果返回值为false,则该configuration不生效;为true则生效。
@Configuration
@ConditionalOnClass({ Feign.class })
@ConditionalOnProperty(value = "feign.oauth2.enabled", havingValue = "true", matchIfMissing = true)
public class OAuth2FeignAutoConfiguration { @Bean
@ConditionalOnBean(OAuth2ClientContext.class)
public RequestInterceptor oauth2FeignRequestInterceptor(OAuth2ClientContext oauth2ClientContext) {
return new OAuth2FeignRequestInterceptor(oauth2ClientContext);
}
}
5.@ConditionalOnMissingBean
说明:仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean
@Bean
@ConditionalOnMissingBean(name = "imageValidateCodeGenerator")
public ValidateCodeGenerator imageValidateCodeGenerator() {
ImageCodeGenerator codeGenerator = new ImageCodeGenerator();
codeGenerator.setSecurityProperty(securityProperties);
return codeGenerator;
}
6.@ConditionalOnMissingClass
说明:某个class类路径上不存在的时候,才会实例化一个Bean
7.@ConditionalOnExpression
说明:当表达式为true的时候,才会实例化一个Bean
@Configuration
@ConditionalOnExpression("${enabled:false}")
public class BigpipeConfiguration {
@Bean
public OrderMessageMonitor orderMessageMonitor(ConfigContext configContext) {
return new OrderMessageMonitor(configContext);
}
}
其他样式:
@ConditionalOnExpression("${mq.cumsumer.enabled}==1&&${rabbitmq.comsumer.enabled:true}")
@ConditionalOnExpression("'${mq.comsumer}'.equals('rabbitmq')")
8.@ConditionalOnNotWebApplication
说明:当前不是web应用
9.@ConditionalOnWebApplication
说明:当前是web应用
10. @ConditionalOnResource
说明:类路径下是否存在指定的资源文件
@Bean
@ConditionalOnResource(resources="classpath:shiro.ini")
protected Realm iniClasspathRealm(){ }
@Conditional系列注解例子的更多相关文章
- [转帖]Java高级系列——注解(Annotations)
Java高级系列——注解(Annotations) 2018年01月13日 :: RonTech 阅读数 3405更多 所属专栏: Java高级系列文章 版权声明:转载请注明出处,谢谢配合. http ...
- Spring源码系列 — 注解原理
前言 前文中主要介绍了Spring中处理BeanDefinition的扩展点,其中着重介绍BeanDefinitionParser方式的扩展.本篇文章承接该内容,详解Spring中如何利用BeanDe ...
- Spring5源码深度分析(二)之理解@Conditional,@Import注解
代码地址: 1.源码分析二主要分析的内容 1.使用@Condition多条件注册bean对象2.@Import注解快速注入第三方bean对象3.@EnableXXXX 开启原理4.基于ImportBe ...
- 简单的JPA注解例子
package ssh.entity; import java.math.BigDecimal; import java.util.Date; import javax.persistence.*; ...
- Android Annotations 注解例子
1.AndroidAnnotations官网: http://androidannotations.org/ (也许你需要FQ) 2.eclipse中使用androidannotations的配置方法 ...
- 注解Annotation实现原理与自定义注解例子
什么是注解? 对于很多初次接触的开发者来说应该都有这个疑问?Annontation是Java5开始引入的新特征,中文名称叫注解.它提供了一种安全的类似注释的机制,用来将任何的信息或元数据(metada ...
- 【转】java编程思想第20章的注解例子用到的com.sun.mirror的jar包
Java編程思想>中的注解代码中引入过这么一个包(com.sun.mirror),书上说的是在Jdk中有个tools.jar中,引入这个包就每这个问题了,但是笔者用的是JDK 1.8,把这个包i ...
- Java开发系列-注解
概述 在JDK5之后提供了一个新特性,和类.接口同级.定义时使用的关键字是@interface.注解主要有三个作用,分别是编译检查.替代配置文件.定义注解(元注解).分析代码(用到反射).注解的本质就 ...
- Hadoop概念学习系列之例子形象再谈Client、NameNode、元数据(三十一)
Client相当于是送货人或提货人. NameNode相当于是仓库管理员. 元数据相当于是账本清单.
随机推荐
- Spring项目下js文件无法引用
问题:最近练习spring-mvc框架的时候,引用了jQuery,但是运行的时候,jQuery做的事情都没有响应,代码也没有任何报错,在网上找了一下,发现是由于spring配制的是/,所有的资源都被屏 ...
- Java中date和calendar的用法
获取现在系统的时间和日期看起来是一件非常神奇的事情,但是当使用date和calendar之后发现仍然非常神奇. 1.date 使用date日期之前需要导入包: import java.text.Sim ...
- Grep的过滤使用
grep的过滤使用 已知文件test里有以下内容 [root@yangwenbo /]# cat test yuni yunwei YUNWEI YWEI yunjijsuan yunsuan YUN ...
- 初撩RESTful
1. 什么是RESTful? 一种软件架构风格,设计风格,用于客户端和服务端交互类的架构. 一组架构约束条件和原则 2. 什么是RESTful架构? 客户端通过http动词(get/post等)对服务 ...
- 3、Python 基础类型 -- List 列表类型
2.更新列表:list.append() 3.删除列表元素 del
- mybatis 多表查询sql
在使用spring,spring mvc,mybatis时,mybatis链接数据库做多表查询的时候,sql语句中直接使用left join等链接字符就可以 链接多个表,参数类型是parameterT ...
- leetcode-160周赛-5241-铺瓷砖
题目描述: 方法一:动态规划 class Solution: def f(self, n, m): if n < m: n, m = m, n if (n, m) in self.mem: re ...
- v-model 双向数据绑定以及修饰符
<!--v-model 实现双向数据绑定 其中一个值发生改变,另一个值也将实时发生改变--> <div id="app09"> <h1>{{ m ...
- MaxCompute新功能发布
2018年Q3 MaxCompute重磅发布了一系列新功能. 本文对主要新功能和增强功能进行了概述. 实时交互式查询:Lightning on MaxCompute 生态兼容:Spark on Max ...
- Delphi 实现简易语音发音(基于TTS方式)
uses Comobj; procedure TForm1.Button1Click(Sender: TObject); var voice: OLEVariant; begin voice := C ...