Config类
package com.atguigu.config;
import com.atguigu.bean.Person;
import com.atguigu.condition.Liunx;
import com.atguigu.condition.Windows;
import org.springframework.context.annotation.*;
// @Conditional({Windows.class})
// 类中组件统一设置。满足当前条件,这个类中配置的所有bean才会生效
@Configuration //表示配置文件类
public class MyConfig2 {
/*
* @Conditional:按照一定的条件判断,满足条件的给容器中注册bean
*
*
* 如果是windows系统,给容器注册("bill")
* 如果是linux系统,给容器注册("linus")
* */
@Conditional({Windows.class})
@Bean("bill")
public Person person01(){
return new Person("Bill Gates",62);
}
@Conditional({Liunx.class})
@Bean("linus")
public Person person02(){
return new Person("linus",48);
}
}
Condition类
liunx
package com.atguigu.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;
//判断是否wlinux系统
public class Liunx implements Condition {
/*
* conditionContext:判断条件能使用的上下文(环境)
* AnnotatedTypeMetadata:注释信息
* */
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
// 判断是否liunx系统
//1.获取到ioc使用的BeanFactory
ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
//2.获取类加载器
ClassLoader classLoader = conditionContext.getClassLoader();
//3.获取当前环境信息
Environment environment = conditionContext.getEnvironment();
//4.获取到bean定义的注册类
BeanDefinitionRegistry registry = conditionContext.getRegistry();
//判断操作系统
String property = environment.getProperty("os.name");
//可以判断容器中的bean的注册情况,也可以给容器注册bean
boolean person = registry.containsBeanDefinition("person");
if(property.contains("linux")){
return true;
}
return false;
}
}
windows
package com.atguigu.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;
//判断是否windows系统
public class Windows implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
// 判断是否Windows系统
//1.获取到ioc使用的BeanFactory
ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
//2.获取类加载器
ClassLoader classLoader = conditionContext.getClassLoader();
//3.获取当前环境信息
Environment environment = conditionContext.getEnvironment();
//4.获取到bean定义的注册类
BeanDefinitionRegistry registry = conditionContext.getRegistry();
//判断操作系统
String property = environment.getProperty("os.name");
if(property.contains("Windows")){
return true;
}
return false;
}
}
Test类
@Test
public void Test03(){
String[] names = context.getBeanNamesForType(Person.class);
ConfigurableEnvironment environment = context.getEnvironment();
String property = environment.getProperty("os.name");
System.out.println("操作系统"+property);
for (String name : names) {
System.out.println(name);
}
Map<String, Person> map = context.getBeansOfType(Person.class);
System.out.println(map);
}