spring中@Conditional注解
@Conditional是Spring4新提供的注解,它的作用是根据某个条件加载特定的bean。
我们需要创建实现类来实现Condition接口,这是Condition的源码
public interface Condition {
boolean matches(ConditionContext var1, AnnotatedTypeMetadata var2);
}
所以我们需要重写matches方法,该方法返回boolean类型。
首先我们准备根据不同的操作系统环境进行对容器加载不同的bean,先创建Person
public class Person {
}
创建实现类LinuxCondition和WindowCondiction,
LinuxCondition:
public class WindowCondiction implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
return true;
}
}
WindowCondiction:
public class LinuxCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
return true;
}
}
配置类:给相应的bean加上 @Conditional注解,里面的括号将返回boolean类型,返回true则加载bean
@Configuration
public class MainConfig { @Profile("window")
@Conditional(WindowCondiction.class)
@Bean
public Person person01(){
return new Person("李思",30);
} @Profile("linux")
@Conditional(LinuxCondition.class)
@Bean
public Person person02(){
return new Person("wangwu",35);
}
}
测试:现在是按照linux环境,@Profile注解先匹配linux的bean,再根据@Conditional 返回的类型判断是否加载bean,这里都设置返回true,所以应该打印
Person{name='wangwu', age=35}
public class CondictionTest {
@Test
public void test(){
//创建容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//设置需要激活的环境
applicationContext.getEnvironment().setActiveProfiles("linux");
//设置主配置类
applicationContext.register(MainProfileConfig.class);
//启动刷新容器
applicationContext.refresh();
String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
for (String name : beanNamesForType){
System.out.println(name);
}
applicationContext.close();
}
}
如果把LinuxCondition的返回值该为false,会报找不到bean的异常
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.springbean.Person' available
spring中@Conditional注解的更多相关文章
- Spring中@Autowired注解、@Resource注解的区别 (zz)
Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@ ...
- Spring中Value注解的使用
Spring中Value注解的使用 分类: Spring2014-08-16 17:28 2985人阅读 评论(0) 收藏 举报 有的时候我们定义了Properties文件,并且使用Spring的Pr ...
- 全面解析Spring中@ModelAttribute注解的用法
本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:全面解析Spring中@ModelAttribute注解的用法: @ModelAttribute注解用于将方法的参数或方法的返回值绑定到 ...
- EnableAutoConfiguration注解 Spring中@Import注解的作用和使用
EnableAutoConfiguration注解 http://www.51gjie.com/javaweb/1046.html springboot@EnableAutoConfiguration ...
- Spring中常用注解的介绍
spring中使用注解时配置文件的写法: <?xml version="1.0" encoding="UTF-8"?> <span style ...
- Spring中使用注解时启用<context:component-scan/>
在spring中使用注解方式时需要在spring配置文件中配置组件扫描器:http://blog.csdn.net/j080624/article/details/56277315 <conte ...
- Spring中异步注解@Async的使用、原理及使用时可能导致的问题
前言 其实最近都在研究事务相关的内容,之所以写这么一篇文章是因为前面写了一篇关于循环依赖的文章: <面试必杀技,讲一讲Spring中的循环依赖> 然后,很多同学碰到了下面这个问题,添加了S ...
- Spring中@Import注解的使用
Spring中@Import注解的使用 @Import注解算是SpringBoot自动配置原理中一个很重要的注解 认识@Import注解 先看一下源码 @Target(ElementType.TYPE ...
- Spring中常用注解
1.@Component 创建类对象,相当于配置<bean/> 2.@Service @Service与@Component功能相同,写在ServiceImpl类上 3.@Reposito ...
随机推荐
- shiro框架的UsernamePasswordToken与对应Realm中的AuthenticationToken的一点比较(转)
这里以简单的登陆为例子 控制器对应的登陆方法: @RequestMapping(value = "/login", method = RequestMethod.GET) publ ...
- python--第六天练习题
#1.正则表达式计算 origin = "1 - 2 * ( ( 60 - 30 + ( -40.0 / 5 ) * ( 9 - 2 * 5 / 3 + 7 / 3 * 99 / 4 * 2 ...
- luogu2034
/* * 正难则反 * f[i] 表示前 i 个数中被删除的数的最小和 * f[i] = min(f[j]) + num, i - k + 1 <= j < i; * 单调队列维护 */ ...
- read,write,lseek
转自 http://blog.csdn.net/todd911/article/details/11237627 1.read 调用read函数从文件去读数据,函数定义如下: #include < ...
- unix/linux 进程间文件锁
转自 http://www.cnblogs.com/hjslovewcl/archive/2011/03/14/2314333.html 有三种不同的文件锁,这三种都是“咨询性”的,也就是说它们依靠程 ...
- C语言学习笔记1-数据类型和标识符
http://blog.csdn.net/jadeshu/article/details/50751901 1.数据类型 ---1.1基本类型 --------------数值型(short(2) i ...
- try catch块的秘密
最近有同事遇到问题: 她在4处手动抛运行异常,5处存在return语句,结果程序在2出现异常时没有抛出运行异常,导致事务不一致. 我们都知道,当程序出现异常时候并且在不采取任何措施的情况下,是会抛出异 ...
- StringBuffer 拼写串
StringBuffer ssBuffer=new StringBuffer(); for (int i = 0; i < lista.size(); i++) { ssBuffer=ssBuf ...
- G-LAB四月份作业-数据可视化问题探讨
G-LAB四月份作业-数据可视化问题探讨 引子: 数据平台项目建设正在按照公司的计划开展执行中,作为平台建设项目参与者之一,感觉目前我们现有的MIS报表平台数据也不可谓不丰富,但是不论从提供给用户的数 ...
- nginx 配置简单反向代理
假设端口号是 3000 server { listen ; server_name your.domain; location / { proxy_pass http://127.0.0.1:3000 ...