@ComponentScan(value = "com.example.demo.annotation")spring会将
com.example.demo.annotation
目录下标注了spring能识别的注解的类注册为bean@ComponentScan
还可以指定排除和包含规则
@ComponentScan(value = "com.example.demo.annotation", /*excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class}) },*/ includeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Configuration.class}), @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = {TestController.class}), @ComponentScan.Filter(type = FilterType.CUSTOM,classes = {MyTypeFilter.class}) },useDefaultFilters = false )其中
CUSTOM
自定义规则中的MyTypeFilter
需要实现TypeFilter
接口,举例如下public class MyTypeFilter implements TypeFilter { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { String className = metadataReader.getClassMetadata().getClassName(); System.out.println("------>"+className); if (className.contains("er")){ return true; } return false; } }
@Bean Person person() { return new Person("zhang"); }
@Import({Color.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})
ImportSelector
:返回需要注册的组件public class MyImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata annotationMetadata) { return new String[]{"com.example.demo.annotation.bean.Red"}; } }
ImportBeanDefinitionRegistrar
类public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { boolean b = registry.containsBeanDefinition("com.example.demo.annotation.bean.Color"); boolean b1 = registry.containsBeanDefinition("com.example.demo.annotation.bean.Red"); if (b && b1){ registry.registerBeanDefinition("rainBow",new RootBeanDefinition(RainBow.class)); } } }
public class ColorFactoryBean implements FactoryBean<Color> { @Override public Color getObject() throws Exception { return new Color(); } @Override public Class<?> getObjectType() { return Color.class; } } @Bean ColorFactoryBean colorFactoryBean() { return new ColorFactoryBean(); }applicationContext.getBean("colorFactoryBean") 默认获取到的是FactoryBean调用getObject方法返回的对象
@Conditional
举例:在windows和linux上分别注册不同的bean
@Conditional({WindowsConditional.class}) @Bean("windows") Person person1() { return new Person("windows"); } @Bean("linux") @Conditional({LinuxConditional.class}) Person person2() { return new Person("linux"); } public class WindowsConditional implements Condition { @Override public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { Environment environment = conditionContext.getEnvironment(); String property = environment.getProperty("os.name"); return property.toLowerCase().contains("windows"); } } public class LinuxConditional implements Condition { @Override public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { Environment environment = conditionContext.getEnvironment(); String property = environment.getProperty("os.name"); return property.toLowerCase().contains("linux"); } }
我们可以自定义初始化和销毁方法,容器在bean进行到当前生命周期时来调用我们自定义的初始化和销毁方法
创建对象:
单实例:在容器启动时创建对象
多实例:在每次获取bean的时候创建对象
每个BeanPostProcessor的 postProcessBeforeInitialization 方法会在初始化之前执行
初始化: 对象创建好,调用初始化方法
每个BeanPostProcessor的 postProcessAfterInitialization 方法会在初始化之后执行
销毁:
单实例:容器关闭时销毁
多实例:容器会帮助创建这个bean,但不会管理这个bean,所以容器不会调用销毁方法,可以手动调用销毁方法
bean的后置处理器 BeanPostProcessor,其有如下两个方法,在bean的初始化前后做一些处理:
@Component public class MyBeanPostProcessor implements BeanPostProcessor { private final ApplicationContext applicationContext; public MyBeanPostProcessor(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessBeforeInitialization--->"+beanName); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessAfterInitialization--->"+beanName); return bean; } }
如果自定义组件想要使用spring容器底层的组件(ApplicationContext,BeanFactory,***),自定义组件可以实现 ***Aware,
在创建bean的时候,相关BeanPostProcessor会调用接口规定的方法注入相关组件
例如:
如果自定义bean 实现了ApplicationContextAware 接口,在ApplicationContextAwareProcessor中会调用ApplicationContextAware的
setApplicationContext方法,注入ApplicationContext组件