目录
@Conditional 按条件注入
一、知识点
注入bean时,指定一些条件@Conditional,标记在配置类或bean方法上
二、例子
废话小少说了,直接上例子,我这里使用的是《hualinux spring 4.3:idea 2020创建简单的spring注解式》 建立的项目为基础的
例子的功能是根据系统动态输出
2.1 目录结构
2.2 实现代码
src-->com.hualinux.spring.WindowsConditon.java
package conf;
import com.hualinux.spring.Hello;
import com.hualinux.spring.LinuxConditon;
import com.hualinux.spring.WindowsConditon;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
//告诉spring 这是一个配置文件,这个注解是一定要有的
@Configuration
public class HelloConf {
@Conditional({WindowsConditon.class})
@Bean("bill")
public Hello hello1(){
return new Hello("win");
}
@Conditional({LinuxConditon.class})
@Bean("linus")
public Hello hello2(){
return new Hello("linux");
}
}
com.hualinux.spring.LinuxConditon.java
package com.hualinux.spring;
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 LinuxConditon implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//获取IOC的beanFactory
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
//获取类加载器
ClassLoader classLoader = context.getClassLoader();
//获取当前环境信息
Environment environment = context.getEnvironment();
//获取到bean的定义注册类
BeanDefinitionRegistry registry = context.getRegistry();
String property = environment.getProperty("os.name");
if(property.contains("linux")){
return true;
}
return false;
}
}
conf.HelloConf.java 配置如下
package conf;
import com.hualinux.spring.Hello;
import com.hualinux.spring.LinuxConditon;
import com.hualinux.spring.WindowsConditon;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
//告诉spring 这是一个配置文件,这个注解是一定要有的
@Configuration
public class HelloConf {
@Conditional({WindowsConditon.class})
@Bean("bill")
public Hello hello1(){
return new Hello("win");
}
@Conditional({LinuxConditon.class})
@Bean("linus")
public Hello hello2(){
return new Hello("linux");
}
}
com.hualinux.spring.HelloMain.java配置如下:
package com.hualinux.spring;
import conf.HelloConf;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
public class HelloMain {
public static void main(String[] args) {
//1. 创建 Spring 的IOC容器对象
//这次使用的不是ClassPathXmlApplicationContext类了,而是注解类
AnnotationConfigApplicationContextctx= new AnnotationConfigApplicationContext(HelloConf.class);
ConfigurableEnvironment environment= (ConfigurableEnvironment) ctx.getEnvironment();
//动态获取环境变量的值,Windows
String property = environment.getProperty("os.name");
System.out.println(property);
String[] beanNamesForType = ctx.getBeanNamesForType(Hello.class);
for (String name : beanNamesForType){
System.out.println(name);
}
//2. 从 IOC 容器中获取 Bean 实例,填写Bean的ID就是java配置文件的方法名
//Hello hello= (Hello) ctx.getBean("hello");
//3. 调用sayHello方法
//hello.sayHello();
}
}
2.3 运行
运行上面的.HelloMain.java,结果如下: