Spring组件注册 (2)

测试方法

@Test public void testCondition() { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MainConfig2.class); ConfigurableEnvironment environment = ac.getEnvironment(); String[] beanNamesForType = ac.getBeanNamesForType(Person.class); //动态获取环境变量的值:windows 7 String property = environment.getProperty("os.name"); System.out.println(property); for (String p:beanNamesForType) { System.out.println(p); } }

运行结果如下,Windows 7的系统所以bill注册了进来

Windows 7 bill person2 @Import

快速导入组件,id默认是组件的全类名

配置类上加了@Import注解

@Configuration @Import({Color.class, Dog.class}) public class MainConfig2 { /** *Conditional({Condition}):按照一定的条件进行判断,满足条件给容器中注册bean * 如果系统是windows,给容器中注册("bill") * 如果系统是linux,给容器中注册("linus") * @return */ @Conditional({WindowsCondition.class}) @Bean("bill") public Person person(){ return new Person("Bill Gates",62); } @Conditional({LinuxCondition.class}) @Bean("linus") public Person person2(){ return new Person("linus",22); } }

测试方法

@Test public void testImport() { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MainConfig2.class); String[] beanNamesForType = ac.getBeanDefinitionNames(); for (String p:beanNamesForType) { System.out.println(p); } }

运行结果:除了内部的bean,Color和Dog也被注册进来了

org.springframework.context.annotation.internalConfigurationAnnotationProcessor org.springframework.context.annotation.internalAutowiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.event.internalEventListenerProcessor org.springframework.context.event.internalEventListenerFactory mainConfig2 com.eric.bean.Color com.eric.bean.Dog bill person2 @Import使用ImportSelector //自定义逻辑返回需要导入的组件 public class MyImportSelector implements ImportSelector { //返回值就是导入到容器中的组件的全类名 //AnnotationMetadata 当前标注@Import注解类的所有注解信息 @Override public String[] selectImports(AnnotationMetadata annotationMetadata) { Set<String> annotationTypes = annotationMetadata.getAnnotationTypes(); for(String str : annotationTypes){ System.out.println("===="+str); } return new String[]{"com.eric.bean.Blue","com.eric.bean.Red"}; } }

@Import注解加上自定义的组件MyImportSelector

@Configuration @Import({Color.class, Dog.class,MyImportSelector.class}) public class MainConfig2 { /** *Conditional({Condition}):按照一定的条件进行判断,满足条件给容器中注册bean * 如果系统是windows,给容器中注册("bill") * 如果系统是linux,给容器中注册("linus") * @return */ @Conditional({WindowsCondition.class}) @Bean("bill") public Person person(){ return new Person("Bill Gates",62); } @Conditional({LinuxCondition.class}) @Bean("linus") public Person person2(){ return new Person("linus",22); } }

测试方法

@Test public void testImport() { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MainConfig2.class); String[] beanNamesForType = ac.getBeanDefinitionNames(); for (String p:beanNamesForType) { System.out.println(p); } }

运行结果:Blue Red 都被注册进来了

====org.springframework.context.annotation.Configuration ====org.springframework.context.annotation.Import org.springframework.context.annotation.internalConfigurationAnnotationProcessor org.springframework.context.annotation.internalAutowiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.event.internalEventListenerProcessor org.springframework.context.event.internalEventListenerFactory mainConfig2 com.eric.bean.Color com.eric.bean.Dog com.eric.bean.Blue com.eric.bean.Red bill person2 @Import使用ImportBeanDefinitionRegistrar

手动注册bean到容器中

//要被注册的bean public class Rainbow { } public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { /** * * @param importingClassMetadata 当前类的注解信息 * @param registry BeanDefinition注册类 * 把所有需要添加到容器中的bean,调用 * BeanDefinitionRegistry.registerBeanDefinition手工注册进来 */ @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { boolean blueDefinition = registry.containsBeanDefinition("com.eric.bean.Blue"); if (blueDefinition){ //指定bean的定义信息 RootBeanDefinition beanDefinition = new RootBeanDefinition(Rainbow.class); //注册一个bean,指定bean名 registry.registerBeanDefinition("rainbow", beanDefinition); } } }

配置类:@Import加入了中MyImportBeanDefinitionRegistrar

@Configuration @Import({Color.class, Blue.class,MyImportBeanDefinitionRegistrar.class}) public class MainConfig2 { }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsfsgj.html