@Import[快速给容器中导入一个组件]
@Import(要导入到容器中的组件);容器中就会自动注册这个组件,id默认是全类名
- @Import 注解 其内部是一个 Class 数组
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
Class<?>[] value();
}
- 新建一个Bean Color
public class Color {
}
- 使用@Import 注解快速导入
@Conditional({WindowsCondition.class})
@Import(Color.class)
@Configuration
public class MainConfig2 {
@Scope("singleton")
@Lazy
//@Scope("prototype")
@Bean("person")
public Person person(){
return new Person("zhangsan",22);
}
@Conditional(LinuxCondition.class)
@Bean("linux")
public Person person01(){
return new Person("linus",10);
}
@Conditional(WindowsCondition.class)
@Bean("windows")
public Person person02(){
return new Person("bill",30);
}
}
- 同时导入多个bean
@Conditional({WindowsCondition.class})
@Import({Color.class, Red.class})
@Configuration
public class MainConfig2 {
@Scope("singleton")
@Lazy
//@Scope("prototype")
@Bean("person")
public Person person(){
return new Person("zhangsan",22);
}
......
}
@ImportSelector:返回需要导入的组件的全类名数组
- 实现 ImportSelector 接口
public class MyImportSelector implements ImportSelector {
/**
*
* @param importingClassMetadata 当前标注@Import注解的类的所有注解信息
* @return 返回值,就是到导入到容器中的组件全类名
*/
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
//方法不要返回null值
return new String[]{"com.spring.annotation.bean.Bule","com.spring.annotation.bean.Yellow"};
}
}
- 单元测试
@Test
public void testImport(){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
System.out.println(beanDefinitionName);
}
}
- 测试结果
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig2
com.spring.annotation.bean.Color
com.spring.annotation.bean.Bule
com.spring.annotation.bean.Yellow
person
windows
注意:@Import 注入bean的id 是全类名
ImportBeanDefinitionRegistrar: 手动注册bean到容器中
- 实现ImportBeanDefinitionRegistrar接口
/**
* ImportBeanDefinitionRegistrar: 手动注册bean到容器中
*/
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
/**
*
* @param annotationMetadata 当前类的注解信息
* @param beanDefinitionRegistry BeanDefinition注册类
*
* BeanDefinitionRegistry.registerBeanDefinition手工注册进来
*/
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata,
BeanDefinitionRegistry beanDefinitionRegistry) {
boolean definition = beanDefinitionRegistry.containsBeanDefinition("com.atguigu.bean.Red");
boolean definition2 = beanDefinitionRegistry.containsBeanDefinition("com.atguigu.bean.Blue");
if(definition && definition2){
//指定Bean定义信息;(Bean的类型,Bean。。。)
RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class);
//注册一个Bean,指定bean名
beanDefinitionRegistry.registerBeanDefinition("rainBow", beanDefinition);
}
}
}
- 在@Import注解中使用 中使用
//类中组件统一设置。满足当前条件,这个类中配置的所有bean注册才能生效;
@Conditional({WindowsCondition.class})
@Import({Color.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})
@Configuration
public class MainConfig2 {
@Scope("singleton")
@Lazy
//@Scope("prototype")
@Bean("person")
public Person person(){
return new Person("zhangsan",22);
}
@Conditional(LinuxCondition.class)
@Bean("linux")
public Person person01(){
return new Person("linus",10);
}
@Conditional(WindowsCondition.class)
@Bean("windows")
public Person person02(){
return new Person("bill",30);
}
}
- 测试代码
@Test
public void testImport(){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
System.out.println(beanDefinitionName);
}
}
- 测试结果
Person constructor....
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig2
com.spring.annotation.bean.Color
com.spring.annotation.bean.Bule
com.spring.annotation.bean.Yellow
person
windows
Process finished with exit code 0