@Conditional 通过改注解能判读根据条件动态创建bean
person类
public class Person {
private String name;
private int age;
public Person() {
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + 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;
}
}
配置类
import com.myspring.condition.linuxCondition;
import com.myspring.condition.windowsContidion;
import com.myspring.entity.Person;
import org.springframework.context.annotation.*;
@ComponentScan(value = "com.myspring")
@Configuration
public class MainConfig2 {
@Conditional(windowsContidion.class)
@Bean("bill")
public Person person01(){
return new Person("Bill Grate",62);
}
@Conditional(linuxCondition.class)
@Bean("linus")
public Person person02(){
return new Person("linus",48);
}
}
新建linuxCondition 实现 Condition 接口
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
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 linuxCondition implements Condition {
/**
* conditionContext 判读条件能使用的上下文
*annotatedTypeMetadata 注解信息
*
*/
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
// 获取到bean的创建工程
ConfigurableBeanFactory configurableBeanFactory = conditionContext.getBeanFactory();
// 获取类加载器
ClassLoader loader = conditionContext.getClassLoader();
// 获取环境
Environment environment = conditionContext.getEnvironment();
// 获取到bean定义的注册类
BeanDefinitionRegistry beanDefinitionRegistry = conditionContext.getRegistry();
// 获取系统名称
String property = environment.getProperty("os.name");
if(property.contains("linux")){
return true;
}
return false;
}
}
新建windowsCondition 实现 Condition 接口
package com.myspring.condition;
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 windowsCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
// 获取环境
Environment environment = conditionContext.getEnvironment();
String property = environment.getProperty("os.name");
if(property.contains("Windows")){
return true;
}
return false;
}
}
测试类
@Test
public void Test2()
{
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
String[] names = applicationContext.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
// 获取环境变量 Windows 10
Environment environment = applicationContext.getEnvironment();
String property = environment.getProperty("os.name");
System.out.println(property);
Map<String,Person> persons = applicationContext.getBeansOfType(Person.class);
System.out.println(persons);
}
运行结果
person
bill
备注
@Conditional 不仅能放在方法上 也能放在类上。
当放在类上时,表明该类的创建都要符号 该注解的条件