1、@Conditional注解概述
a、@Conditional是Spring4新提供的注解,它的作用是按照自定义的条件进行判断,满足条件的就将指定的JavaBean组件注册到SpringIOC容器中。
b、@Conditional注解是由Spring Framework提供的一个注解,它位于org.springframework.context.annotation包内,
c、从@Conditional注解的源码来看,该注解不仅可以添加到类上,也可以添加到方法上。在@Conditional注解中,还存在着一个Condition类型或者其子类型的Class对象数组,Condition是个啥呢?我们点进去看一下。
它是一个接口。所以,我们使用@Conditional注解时,需要自定义一个类来实现Condition接口 ,然后实现接口里面的matches方法,在该方法中自定义注入条件(将bean是否注入ioc的判断条件)返回true为注入,false则相反。
d、我们可以在哪些场合使用@Conditional注解呢?@Conditional注解的使用场景如下图所示。
2、定义需求
1、如果运行环境是Windows,就将含有【比尔盖茨】(windows创始人)信息的javaBean注入到ioc容器中。
2、如果运行环境是Linux,就将含有【林纳斯】(linux创始人)信息的javaBean注入到ioc容器中。
3、实现步骤
1、在配置类中创建两个方法。一个向ioc容器中注入【比尔盖茨】对象,一个向ioc容器中注入【林纳斯】对象
@Configuration
@ComponentScan(value = "com.leo")
public class MainConfig2 {
//创建一个含有【比尔盖茨】信息的对象
@Bean("bill")
public Person person1() {
return new Person("比尔盖茨", 62);
}
//创建一个含有【林纳斯】信息的对象
@Bean("Linus")
public Person person2() {
return new Person("林纳斯", 48);
}
}
2、定义两个类并实现【Condition】接口,并实现接口中的方法,在方法中编写将bean注入ioc的条件
WindowsConditional.java
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 WindowsConditional implements Condition {
/**
* 判断当前的运行环境是不是Windows系统
*
* @param context 判断的上下文条件(环境)
* @param metadata 当前标注了@Conditional注解的注释信息
* @return
*/
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//获取当前环境信息,它里面就封装了我们这个当前运行时的一些信息,包括环境变量,以及包括虚拟机的一些变量
Environment environment = context.getEnvironment();
//如果当前运行的环境是Windows环境就返回true.否则就返回false
String property = environment.getProperty("os.name");
if (property.contains("Windows")) {
return true;
}
return false;
}
}
LinuxConditional.java
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 LinuxConditional implements Condition {
/**
* 判断当前的运行环境是不是linux系统
*
* @param context 判断的上下文条件(环境)
* @param metadata 当前标注了@Conditional注解的注释信息
* @return
*/
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// 获取当前环境信息,它里面就封装了我们这个当前运行时的一些信息,包括环境变量,以及包括虚拟机的一些变量
Environment environment = context.getEnvironment();
//如果当前运行的环境是linux环境就返回true.否则就返回fals
String property = environment.getProperty("os.name");
if (property.contains("linux")) {
return true;
}
return false;
}
}
3、在向ioc容器中创建bean对象的方法上添加@Conditional注解,并在注解中设置注入的条件
源码如下
@Configuration
@ComponentScan(value = "com.leo")
public class MainConfig2 {
//设置注入条件
@Conditional({WindowsConditional.class})
//创建一个含有【比尔盖茨】信息的对象
@Bean("bill")
public Person person1() {
return new Person("比尔盖茨", 62);
}
//设置注入条件
@Conditional({LinuxConditional.class})
//创建一个含有【林纳斯】信息的对象
@Bean("Linus")
public Person person2() {
return new Person("林纳斯", 48);
}
}
4、查看运行结果
测试代码
@Test
public void test05() {
//AnnotationConfigApplicationContext:该对象是根据读取配置类的方式创建一个ioc容器
ApplicationContext applicationContext_config = new AnnotationConfigApplicationContext(MainConfig2.class);
//获取容器中所有的组件
String[] names = applicationContext_config.getBeanDefinitionNames();
for (String name : names) {
System.out.println("当前IOC容器中有组件:" + name);
}
//拿到ioc的运行环境对象
Environment environment = applicationContext_config.getEnvironment();
//从环境中取出当前的运行系统名字
String property = environment.getProperty("os.name");
System.out.println("当前系统环境是:"+property);
//在容器中获取Person.class的所有对象
Map<String, Person> beansOfType = applicationContext_config.getBeansOfType(Person.class);
System.out.println(beansOfType);
}
通过在idea中模拟设置当前运行环境为linux
设置的值为:-Dos.name=linux
运行结果
5、细节补充
1、在接口【Condition】中【matches】方法参数说明和使用介绍
2、这里细说一下context的【getRegistry()】方法(上图的第3点)获取到的bean定义的注册对象,即BeanDefinitionRegistry对象了。它到底是个啥呢?我们可以点进去看一下它的源码,如下所示,可以看到它是一个接口。