@Conditional介绍
它的作用是按照一定的条件进行判断,需要注入的Bean满足给定条件才可以注入到IOC容器中。
举个经典例子:操作系统中你是用window还是linux
如果是window系统就不能用linux,如果是linux就不能用windows
下面开始先做使用配置
1.1 创建domain类
package com.Bean; public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Bean{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
1.2 创建配置类Config,定义Bean
package com.Config; import com.Bean.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Scope; public class MainConfig { @Bean("bill") public Person person1(){ return new Person("Bill",60); } @Bean("Lin") public Person person2(){ return new Person("Lin",48); } }
1.3 创建测试类,运行
@Test public void Test03(){ AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MainConfig.class); String[] beanNamesForType = annotationConfigApplicationContext.getBeanNamesForType(Person.class); ConfigurableEnvironment environment = annotationConfigApplicationContext.getEnvironment(); String property = environment.getProperty("os.name"); System.out.println(property); for (String name:beanNamesForType) { System.out.println(name); } }
运行后我们发现IOC容器中管理了三个Bean,且操作系统现在为windows 10,下面我们来使用Conditional注解操作
二 Conditional注解使用详情
2.1创建二个类,windows,linux
package com.Conditional; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; import org.springframework.core.env.Environment; import org.springframework.core.type.AnnotatedTypeMetadata; public class Linux implements Condition { //ConditionContext:判断上下文环境 //AnnotatedTypeMetadata,注释信息 @Override public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { //获取IOC使用beanfactory ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory(); //获取当前环境信息 Environment environment = conditionContext.getEnvironment(); String property = environment.getProperty("os.name"); //如果是linux系统返回真 if(property.contains("linux")){ return true; } return false; } }
package com.Conditional; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.env.Environment; import org.springframework.core.type.AnnotatedTypeMetadata; public class windows implements Condition { //ConditionContext:判断上下文环境 //AnnotatedTypeMetadata,注释信息 @Override public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { //获取当前环境信息 Environment environment = conditionContext.getEnvironment(); String property = environment.getProperty("os.name"); //如果是Windows系统返回真 if(property.contains("Windows")){ return true; } return false; } }
2.2 在Config中添加Conditional注解
@Conditional({windows.class}) @Bean("bill") public Person person1(){ return new Person("Bill",60); } @Conditional({Linux.class}) @Bean("Lin") public Person person2(){ return new Person("Lin",48); }
2.3运行后
最终我们得到了答案,因为Lin是在Linux操作系统下才会运行的,但是我们计算机是windows系统所以不会运行,则不会加载到IOC容器中
三 Conditional注解可以配置在方法上,也可以配置在类上
注意:如果我们配置到类上就必须满足这个操作条件才会让类方法的Bean加载到IOC容器中
3.1在类上添加注解Conditional 并设置为Linux系统
package com.Config; import com.Bean.Person; import com.Conditional.Linux; import com.Conditional.windows; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Scope; @Conditional({Linux.class}) public class MainConfig { @Conditional({windows.class}) @Bean("bill") public Person person1(){ return new Person("Bill",60); } @Conditional({Linux.class}) @Bean("Lin") public Person person2(){ return new Person("Lin",48); } }