@Conditional的使用
@Conditional({
MacOsCondition.class
})
@Bean("person2") //给容器注册一个bean ;类型维返回值的类型,id默认用方法名作为id
public Person person2(){
return new Person("wendi","18");
}
@Bean("person1") //给容器注册一个bean ;类型维返回值的类型,id默认用方法名作为id
@Conditional({WindowsOsCondition.class})
public Person person(){
return new Person("xushuai","18");
}
package com.example.paymentdemo.config;
import com.example.paymentdemo.entity.MacOsCondition;
import com.example.paymentdemo.entity.Person;
import com.example.paymentdemo.entity.WindowsOsCondition;
import com.example.paymentdemo.main.MyimportBeanDefinitionRegistrar;
import com.example.paymentdemo.main.MyimportSelector;
import org.springframework.context.annotation.*;
@Configuration
//当标注在类上的时候,满足当前条件,这个类中标注的bean才会生效
@Conditional({WindowsOsCondition.class})
//快速导入组件 id默认是组件的全类名
//可以多个
@import({Person.class, MyimportSelector.class, MyimportBeanDefinitionRegistrar.class})
//@import(Person.class)
public class Main{
//Specifies the name of the scope to use for the annotated component/bean.
//Defaults to an empty string ("") which implies SCOPE_SINGLETON.
// 默认是单例的 单例的请求ioc容器会调用方法创建对象放到ioc容器中,以后每次获取就是直接从容器中拿
//ioc在容器启动的时候不会创建新对象,多实例每次调用都会获取个新的对象
//ConfigurableBeanFactory.SCOPE_PROTOTYPE, ConfigurableBeanFactory.SCOPE_SINGLETON, 可以配置为多例
// org.springframework.web.context.WebApplicationContext.SCOPE_REQUEST, web环境下可以有request 和session
// org.springframework.web.context.WebApplicationContext.SCOPE_SESSION, value
//还有个全局的global
@Scope(value = "prototype")
@Lazy //懒加载
@Bean("person1") //给容器注册一个bean ;类型维返回值的类型,id默认用方法名作为id
@Conditional({WindowsOsCondition.class})
public Person person(){
return new Person("xushuai","18");
}
@Conditional({
MacOsCondition.class
})
@Bean("person2") //给容器注册一个bean ;类型维返回值的类型,id默认用方法名作为id
public Person person2(){
return new Person("wendi","18");
}
}
package com.example.paymentdemo.entity;
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.context.annotation.Conditional;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypemetadata;
public class WindowsOsCondition implements Condition {
//conditionContext: 判断条件能使用的上下文环境
//AnnotatedTypeMetsdata:注释信息
@Override
public boolean matches(ConditionContext context, AnnotatedTypemetadata metadata) {
//获取当前ioc使用的工厂bean
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
//获取类加载器
ClassLoader classLoader = context.getClassLoader();
//获取环境
Environment environment = context.getEnvironment();
//获取Bean定义的注册类
BeanDefinitionRegistry registry = context.getRegistry();
//判断容器中bean的注册情况
boolean person2 = registry.containsBeanDefinition("person2");
//获取操作系统
String property = environment.getProperty("os.name");
if (property.contains("windows")){
return true;
}
return false;
}
}
package com.example.paymentdemo.main;
import com.example.paymentdemo.entity.Person;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import java.util.Map;
public class ConditionTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext("com.example.paymentdemo");
String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class);
for (String s : beanNamesForType) {
System.out.println(s);
}
//获取所有bean
Map beansOfType = applicationContext.getBeansOfType(Person.class);
//获取ioc的运行环境
ConfigurableEnvironment environment = applicationContext.getEnvironment();
String property = environment.getProperty("os.name");
System.err.println(property);
}
}
@import的使用
package com.example.paymentdemo.main;
import org.springframework.context.annotation.importSelector;
import org.springframework.core.type.Annotationmetadata;
import java.util.function.Predicate;
public class MyimportSelector implements importSelector {
//返回值就是要导入到容器中的组件全类名
//Annotationmetadata:当前标注@Impoert注解的类的所有注解信息(其他注解)
@Override
public String[] selectimports(Annotationmetadata importingClassmetadata) {
//把全类名返回
//不能返回null值
return new String[]{"com.example.paymentdemo.entity.Person","com.example.paymentdemo.entity.WindowsOsConfig"};
}
@Override
public Predicate getExclusionFilter() {
return importSelector.super.getExclusionFilter();
}
}
package com.example.paymentdemo.main;
import com.example.paymentdemo.entity.Person;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.importBeanDefinitionRegistrar;
import org.springframework.core.type.Annotationmetadata;
public class MyimportBeanDefinitionRegistrar implements importBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(Annotationmetadata importingClassmetadata, BeanDefinitionRegistry registry) {
boolean person = registry.containsBeanDefinition("person");
if (person){
//注册时需要一个beanDefinition的类型
//指定bean的定义信息,bean的类型等信息
RootBeanDefinition beanDefinition = new RootBeanDefinition(Person.class);
registry.registerBeanDefinition("person1",beanDefinition);
}
}
}



