使用@Conditional注解在bean加载之前对其进行控制 案例如下:
package com.enjoy.cap5;
import com.enjoy.cap1.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MainConfig {
@Conditional(WinCodition.class)
@Bean
public Person zhangsan(){
System.out.println("给我们的容器中添加张三");
return new Person("张三",22);
}
@Conditional(LinCodition.class)
@Bean
public Person lisi(){
System.out.println("给我们的容器中添加李四");
return new Person("李四",33);
}
@Bean
public Person wangwu(){
System.out.println("给我们的容器中添加王五");
return new Person("王五",44);
}
}
package com.enjoy.cap5;
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 LinCodition implements Condition {
/**
* ConditionContext :判断条件可以使用的上下文(环境)
* AnnotatedTypeMetadata:注解信息
* @param context
* @param metadata
* @return
*/
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//是否为win系统
//能够获取IOC容器正在使用的beanFactory
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
//获取当前的环境变量(包括我们操作系统是win还是linux)
Environment environment = context.getEnvironment();
String osName = environment.getProperty("os.name");
System.out.println(osName);
if(osName.contains("win")){
return true;
}
return false;
}
}
package com.enjoy.cap5;
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 WinCodition implements Condition {
/**
* ConditionContext :判断条件可以使用的上下文(环境)
* AnnotatedTypeMetadata:注解信息
* @param context
* @param metadata
* @return
*/
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//是否为win系统
//能够获取IOC容器正在使用的beanFactory
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
//获取当前的环境变量(包括我们操作系统是win还是linux)
Environment environment = context.getEnvironment();
String osName = environment.getProperty("os.name");
System.out.println(osName);
if(osName.contains("Win")){
return true;
}
return false;
}
}
package test;
import com.enjoy.cap1.Person;
import com.enjoy.cap5.MainConfig;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* FactoryBean 和 BeanFactorty 有什么区别?
*
* FactoryBean :可以把我们的JAVA实例通过FactoryBean注入到容器中
*
* BeanFactorty:从我们的容器中获取实例化后的bean
*
*/
public class MainTEwst5 {
/**
* win:张三进入容器
* linux:李四进入容器
*/
@Test
public void sss(){
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
System.out.println("创建容器完成");
}
}
运行结果