首页  

spring bean 生命周期实例     所属分类 spring 浏览量 1392
BeanFactoryPostProcessor (可动态维护bean属性)


set注入

BeanNameAware#setBeanName 
BeanFactoryAware#setBeanFactory 
ApplicationContextAware#setApplicationContext 


BeanPostProcessor#postProcessBeforeInitialization

@PostConstruct 注解方法
InitializingBean#afterPropertiesSet
bean xml中定义的 init-method 方法

BeanPostProcessor#postProcessAfterInitialization


@PreDestroy#preDestroy
DisposableBean#destroy



注意!!!
@PostConstruct 与 @PreDestroy 需要开启组件扫描 ,否则不会生效




实例代码
https://gitee.com/dyyx/hellocode/blob/master/project/springtest/src/main/java/dyyx/life/LifeCycleTestBean.java

测试类
https://gitee.com/dyyx/hellocode/blob/master/project/springtest/src/main/java/dyyx/life/LifeCycleTest.java

spring xml 配置
https://gitee.com/dyyx/hellocode/blob/master/project/springtest/src/main/resources/life-cycle.xml

日志输出

BeanFactoryPostProcessorBean() run 1551750663964


********
BeanFactoryPostProcessor#postProcessBeanFactory run 1551750663981
org.springframework.context.annotation.internalConfigurationAnnotationProcessor bean properties: PropertyValues: length=0
org.springframework.context.annotation.internalAutowiredAnnotationProcessor bean properties: PropertyValues: length=0
org.springframework.context.annotation.internalRequiredAnnotationProcessor bean properties: PropertyValues: length=0
org.springframework.context.annotation.internalCommonAnnotationProcessor bean properties: PropertyValues: length=0
lifeCycleTestBean bean properties: PropertyValues: length=1; bean property 'name'
lifeCycleTestBean2 bean properties: PropertyValues: length=0
beanPostProcessorBean bean properties: PropertyValues: length=0
beanFactoryPostProcessorBean bean properties: PropertyValues: length=0
org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor bean properties: PropertyValues: length=0
org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor bean properties: PropertyValues: length=0
BeanFactoryPostProcessor#postProcessBeanFactory done 1551750663982
********


BeanPostProcessorBean() run 1551750663990
LifeCycleTestBean() run 1551750664003
setInject#setName run 1551750664025
BeanNameAware#setBeanName run 1551750664025
BeanFactoryAware#setBeanFactory run 1551750664025
ApplicationContextAware#setApplicationContext run 1551750664025

BeanPostProcessor#postProcessBeforeInitialization.beanName=lifeCycleTestBean

@PostConstruct#postConstruct run 1551750664025
InitializingBean#afterPropertiesSet run 1551750664025
init-method#init run 1551750664025

BeanPostProcessor#postProcessAfterInitialization.beanName=lifeCycleTestBean

LifeCycleTestBean2() run 1551750664026

BeanPostProcessor#postProcessBeforeInitialization.beanName=lifeCycleTestBean2


BeanPostProcessor#postProcessAfterInitialization.beanName=lifeCycleTestBean2

bean=name=tiger,beanName=lifeCycleTestBean,beanFactory=org.springframework.beans.factory.support.DefaultListableBeanFactory@343f4d3d: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,lifeCycleTestBean,lifeCycleTestBean2,beanPostProcessorBean,beanFactoryPostProcessorBean,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor]; root of factory hierarchy
bean2=dyyx.life.LifeCycleTestBean2@1a3869f4

@PreDestroy#preDestroy run 1551750664035
DisposableBean#destroy run 1551750664035

上一篇     下一篇
hadoop2.0生态技术简介

mysql连接错误ERROR 2002 (HY000)解决方法

maven处理本地jar包依赖

spring4种注入方式

aql输出格式和TTL设置

aerospike使用truncate删除历史数据