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 ...
随机推荐
- 为什么Object.prototype在Function的原型链上与Function.prototype在Object的原型链上都为true
关于javascript的原型链有一个问题我一直很疑惑:为什么 Function instanceof Object 与 Object instanceof Function都为true呢? Func ...
- 00_UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: # <class 'django.contrib.auth.models.Group'> QuerySet.
访问groups时,后端报警告 UnorderedObjectListWarning: Pagination may yield inconsistent results with an unorde ...
- Flutter布局2--Align
Align控件即对齐控件,能将子控件所指定方式对齐,并根据子控件的大小调整自己的大小. eg: 文字组件对齐于右下方 new Align( alignment: FractionalOffset.bo ...
- BZOJ3791 作业 动态规划
你发现染 $k$ 次最多会将这个序列分成 $2k-1$ 段,然后任何 $2k-1$ 段以内的方案一定能被构建出来,所以直接 dp 就好了 #include <bits/stdc++.h> ...
- CF915E 动态开线段树
CF915E 动态开线段树 题面 因为\(n\le10^9\),所以动态开点,线段树维护\([1,n]\)天非工作日数量. 之前的结构体写法被卡了,只能改成函数传l,r(虽然也不难) 动态开点好写,但 ...
- Codeforces 1272 A-E
Codeforces 1272 A-E A Three Friends 直接枚举所有情况,共\(3\times 3\times 3=27\)种. code #include<bits/stdc+ ...
- cyk追楠神系列一(SDUT3703)
cyk追楠神系列一 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 众所周知,cyk ...
- 1937:【06NOIP普及组】数列
woc 太捞了简直捞的一匹 我居然会写博客 反正呀没有人看 随便写写喽
- NSArray 的创建和遍历
数组 用来存贮对象的有序列表,它是不可变的 不能存数C语言的基本数据类型 只支持OC对象 #pragma mark Create a array //Initialize NSArray void a ...
- win10 下载安装tasm
下载tasm http://www.technorange.com/wp-content/uploads/Tasm%201.4%20Windows%207-Windows%208%2064%20bit ...