文章目录
1 引入
增加一个study.wyy.spring.anno.other.config.OtherConfig
配置类
@Configuration
public class OtherConfig {
@Bean
public Person other(){
return new Person("other", 15);
}
}
注意我们的主配置类:没有扫到这个study.wyy.spring.anno.other.config
包路径
@Configuration
@ComponentScan(value = {"study.wyy.spring.anno.learn"})
public class MainConfig {
我们容器中是不会有OtherConfig
和other
测试:
@Test(expected= NoSuchBeanDefinitionException.class)
public void testOtherPerson() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
Person other = (Person) applicationContext.getBean("other");
OtherConfig bean = applicationContext.getBean(OtherConfig.class);
}
@Test(expected= NoSuchBeanDefinitionException.class)
public void testOtherConfig() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
OtherConfig bean = applicationContext.getBean(OtherConfig.class);
}
2 @Import
2.1 入门案例
如何导入这个OtherConfig
这个配置类呢?—> 使用@Import注解导入即可
- 在MainConfig使用@Import注解导入即可
*/
@Configuration
@ComponentScan(value = {"study.wyy.spring.anno.learn"})
@Import(OtherConfig.class)
public class MainConfig {
- 测试
@Test
public void testImportOtherPerson() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
Person other = (Person) applicationContext.getBean("other");
OtherConfig bean = applicationContext.getBean(OtherConfig.class);
}
@Test
public void testImportOtherConfig() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
OtherConfig bean = applicationContext.getBean(OtherConfig.class);
}
2.2 @Import介绍
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
/**
* {@link Configuration}, {@link ImportSelector}, {@link ImportBeanDefinitionRegistrar}
* or regular component classes to import.
*/
Class<?>[] value();
}
- 使用在类上
- 可以导入Configuration,ImportSelector,ImportBeanDefinitionRegistrar
刚刚演示的就是导入了一个配置类,将other这个Person导入
2.3 导入一个ImportSelector
2.3.1 ImportSelector介绍
public interface ImportSelector {
/**
* Select and return the names of which class(es) should be imported based on
* the {@link AnnotationMetadata} of the importing @{@link Configuration} class.
*/
String[] selectImports(AnnotationMetadata importingClassMetadata);
}
- 返回值:返回需要到导入组件的全类名数组
- importingClassMetadata:当前类(在谁的类上时候导入ImportSelector)的注解信息
- 注意返回值不要是null,可以返回一个空数组
2.3.2 使用案例
使用ImportSelector导入OtherConfig
- 实现Importector接口,导入OtherConfig
public class MyImportSelector implements ImportSelector {
/**
*
* @param importingClassMetadata 类的注解信息
* @return 返回需要到导入组件的全类名数组
*/
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
// 返回OtherConfig的全类名
return new String[]{"study.wyy.spring.anno.other.config.OtherConfig"};
}
}
- 在主配置类导入MyImportSelector
@Configuration
@ComponentScan(value = {"study.wyy.spring.anno.learn"})
@Import(MyImportSelector.class)
public class MainConfig {
- 在执行刚刚的测试方法进行断言
2.4 导入一个ImportBeanDefinitionRegistrar
2.4.1 ImportBeanDefinitionRegistrar介绍
通过registerBeanDefinitions方法,将需要添加到容器的组件手工注册进来
public interface ImportBeanDefinitionRegistrar {
/**
* Register bean definitions as necessary based on the given annotation metadata of
* the importing {@code @Configuration} class.
* <p>Note that {@link BeanDefinitionRegistryPostProcessor} types may <em>not</em> be
* registered here, due to lifecycle constraints related to {@code @Configuration}
* class processing.
* @param importingClassMetadata annotation metadata of the importing class
* @param registry current bean definition registry
*/
public void registerBeanDefinitions(
AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry);
}
- importingClassMetadata: 当前类的注解信息
- registry: bean定义的注册类
2.4.2 演示案例
- 实现ImportBeanDefinitionRegistrar接口
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
BeanDefinition otherConfig = new RootBeanDefinition(OtherConfig.class);
registry.registerBeanDefinition("otherConfig", otherConfig);
}
}
- 在主配置中导入
@Configuration
@ComponentScan(value = {"study.wyy.spring.anno.learn"})
@Import(value = MyImportBeanDefinitionRegistrar.class)
public class MainConfig {
- 在执行刚刚的测试方法进行断言