@Conditional通过不同的判断条件,来注册对应的bean
可以配置在类上:满足此条件下,这个类下面的bean都会注册
也可以配置在方法上:方法满足条件才会注册对应方法
下面写一个例子(以写在方法上为例子)
通过查看@Conditional注解源码,需要写对应的条件类来结合使用,此类需要实现Condition接口,重写其方法
public interface Condition {
/**
* Determine if the condition matches.
* @param context the condition context
* @param metadata metadata of the {@link org.springframework.core.type.AnnotationMetadata class}
* or {@link org.springframework.core.type.MethodMetadata method} being checked.
* @return {@code true} if the condition matches and the component can be registered
* or {@code false} to veto registration.
*/
boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);
1.Condition 条件类如下
判断是linux系统的条件类
package chd.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 LinuxCondition implements Condition{
// 是否为linux系统
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment environment = context.getEnvironment();
String property = environment.getProperty("os.name");
if(property.contains("linux")) {
return true;
}
return false;
}
}
判断是windows系统的条件类
package chd.condition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
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 windowsCondition implements Condition{
/*
* ConditionContext:判断条件使用的上下文、
* AnnotatedTypeMetadata:注释信息
* */
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// 是否为windows系统
//1.能获取到ioc使用的beanfactory
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
//2.获取类加载器
ClassLoader classLoader = context.getClassLoader();
//3.获取当前环境信息
Environment environment = context.getEnvironment();
//4.获取bean定义的注册类
BeanDefinitionRegistry registry = context.getRegistry();
String property = environment.getProperty("os.name");
if(property.contains("Windows")) {
return true;
}
return false;
}
}
2…写一个配置类
package config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import chd.condition.LinuxCondition;
import chd.condition.windowsCondition;
import spring.Person;
@Configuration
public class MainConfig2 {
/*@Conditional:按照一定的条件进行判断,满足条件给容器中注册bean
* */
@Conditional({windowsCondition.class})//注册条件为windosw条件类的判断
@Bean(value="bill")
public Person person01() {
return new Person("bill", "28");
}
@Conditional({LinuxCondition.class})//注册条件位linux
@Bean(value="danney")
public Person person02() {
return new Person("danney", "38");
}
}
3.写一个测试类测试
@Test
public void test02() {
String[] names= applicationContext.getBeanNamesForType(Person.class);
//动态获取环境变量的值:获取操作系统的名字
ConfigurableEnvironment environment=applicationContext.getEnvironment();
String property=environment.getProperty("os.name");
System.out.println(property);
for (String name1 : names) {
System.out.println(name1);
}
//获取bean的id和bean的对象
Map <String,Person> persons=applicationContext.getBeansOfType(Person.class);
System.out.println(persons);
}
结果系统位windows系统,只注册windowscondition类中所判断的
4.把系统环境切换成liunx系统试一下,这里只需要配置一些变量就可以,不需要换操作系统。
写入(-Dos.name=linux)
再次测试一下(结果就是linux条件类下所对应判断的结果)