Spring注解和接口
0
列举一些比较容易混淆的注解
配置
@Configurable:对象管理
@Configuration:配置
@PropertySource:加载配置
@ConfigurationProperties:单独使用不会管理(结合Component
/Configuration
/EnableConfigurationProperties
使用)
@EnableConfigurationProperties:配合ConfigurationProperties
使用
初始化销毁
@PostConstruct:初始化
@PreDestroy:销毁
接口
BeanNameAware:获取名称
BeanFactoryAware:获取BeanFactory
ApplicationContextAware:获取上下文
ApplicationEvent/ApplicationListener:事件和监听
生命周期
FactoryBean:创建Bean
InitializingBean:初始化
DisposableBean:销毁
BeanPostProcessor:初始化
BeanFactoryPostProcessor:工厂初始化(Bean生命周期没关:可以用来修改配置)
InstantiationAwareBeanPostProcessor:实例化(每个Bean都会执行)
完整过程:
// 没有实现FactoryBean
BeanFactoryPostProcessor.postProcessBeanFactory
InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation
InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation
InstantiationAwareBeanPostProcessor.postProcessBeforeInitialization
BeanPostProcessor.postProcessBeforeInitialization
@PostConstruct
InitializingBean.afterPropertiesSet
InstantiationAwareBeanPostProcessor.postProcessAfterInitialization
BeanPostProcessor.postProcessAfterInitialization
@PreDestroy
DisposableBean.destroy
// 实现FactoryBean
BeanFactoryPostProcessor.postProcessBeanFactory
InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation
InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation
InstantiationAwareBeanPostProcessor.postProcessBeforeInitialization
BeanPostProcessor.postProcessBeforeInitialization
@PostConstruct
InitializingBean.afterPropertiesSet
InstantiationAwareBeanPostProcessor.postProcessAfterInitialization
BeanPostProcessor.postProcessAfterInitialization
FactoryBean.getObject
InstantiationAwareBeanPostProcessor.postProcessAfterInitialization
BeanPostProcessor.postProcessAfterInitialization
@PreDestroy
DisposableBean.destroy
我们可以注意一下实现和没有实现FactoryBean这个两个的区别
Stream
@RabbitListener:使用SpringBoot:spring-boot-starter-amqp
@StreamListener:使用SpringCloudStream:spring-cloud-stream
/spring-cloud-stream-binder-rabbit