bean 加载时的生命周期

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/aimeimeiTS/article/details/82875821

bean 加载时的生命周期

0 类加载(静态代码块)
1 对象初始化(构造代码块,构造器)
2 BeanNameAware#setBeanName
3 BeanFactoryAware#setBeanFactory
4 ApplicationContextAware#setApplicationContext
5 @PostConstruct
6 InitializingBean#afterPropertiesSet
7 BeanPostProcessor#postProcessBeforeInitialization
8 BeanPostProcessor#postProcessAfterInitialization
9 DisposableBean#destroy

其中 7、8 在容器装配 bean 时会重复回调。

示例

分别定义了两个 CDPlayer 的 bean

    @Bean
    public CDPlayer cdPlayer() {
        return new CDPlayer();
    }

CDPlayer 类上使用了 @Component 注解:

@Component
public class CDPlayer implements
//        MediaPlayer,
        BeanNameAware,
        BeanFactoryAware,
        ApplicationContextAware,
        BeanPostProcessor,
        InitializingBean,
        DisposableBean {

    @Resource(name = "sgtPeppers")
    private CompactDisc disc;

    static {
        System.out.println("CDPlayer: load class static{}");
    }

    public CDPlayer() {
        System.out.println("CDPlayer: step constructer");
    }

    {
        // 1
        System.out.println("CDPlayer: step 1 {}");
    }

    @PostConstruct
    public void init() {
        // 5
        System.out.println("CDPlayer: step 5 init ");
    }

//    @Override
//    public void play() {
//        disc.play(1);
//    }

    @Override
    public void setBeanName(String name) {
        // 2
        System.out.println("CDPlayer: step 2 setBeanName=" + name);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        // 3
        System.out.println("CDPlayer: step 3 setBeanFactory=" + beanFactory.toString());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // 6
        System.out.println("CDPlayer: step 6 afterPropertiesSet");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // 4
        System.out.println("CDPlayer: step 4 setApplicationContext=" + applicationContext.toString());
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        // 7
        System.out.println("CDPlayer: step 7 postProcessBeforeInitialization=" + beanName);
        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        // 8
        System.out.println("CDPlayer: step 8 postProcessAfterInitialization=" + beanName);
        return null;
    }

    @Override
    public void destroy() throws Exception {
        // 9
        System.out.println("CDPlayer: step 9 destroy");
    }
}

启动项目,日志就会输出:


// 类加载
CDPlayer: load class static{}

// 装配 CDPlayer bean
CDPlayer: step 1 {}
CDPlayer: step constructer
CDPlayer: step 2 setBeanName=CDPlayer
CDPlayer: step 3 setBeanFactory=org.springframework.beans.factory.support.DefaultListableBeanFactory@464bee09: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,appConfig,aspectJInvoker,audience,encoreableIntroducer,invoker,trackCounter,aspectConfig,cake,cookies,dessertProvider,iceCream,sgtPeppers,envConfig,envOne,spELOne,main,MVCConfig,notepad,padConfig,shoppingConfig,shoppingService,CDPlayer,CDPlayerConfig,DVDPlayer,propertySourcesPlaceholderConfigurer,enumConverterFactory,platformSessionArgumentResolvers,myNotPad,scopedTarget.shoppingCart,shoppingCart,scopedTarget.shoppingCart2,shoppingCart2,setPeppers,cdPlayer,myDisc,nowDate,myDessert,org.springframework.aop.config.internalAutoProxyCreator,point,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,org.springframework.aop.aspectj.AspectJPointcutAdvisor#3,org.springframework.aop.aspectj.AspectJPointcutAdvisor#4,org.springframework.aop.aspectj.AspectJPointcutAdvisor#5,trackPlayer,org.springframework.aop.aspectj.DeclareParentsAdvisor#0,disc]; root of factory hierarchy
CDPlayer: step 4 setApplicationContext=org.springframework.context.annotation.AnnotationConfigApplicationContext@48533e64: startup date [Tue Sep 25 10:24:42 CST 2018]; root of context hierarchy
CDPlayer: step 5 init
CDPlayer: step 6 afterPropertiesSet

// 装配 cdPlayer bean
CDPlayer: step 1 {}
CDPlayer: step constructer
CDPlayer: step 2 setBeanName=cdPlayer
CDPlayer: step 3 setBeanFactory=org.springframework.beans.factory.support.DefaultListableBeanFactory@464bee09: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,appConfig,aspectJInvoker,audience,encoreableIntroducer,invoker,trackCounter,aspectConfig,cake,cookies,dessertProvider,iceCream,sgtPeppers,envConfig,envOne,spELOne,main,MVCConfig,notepad,padConfig,shoppingConfig,shoppingService,CDPlayer,CDPlayerConfig,DVDPlayer,propertySourcesPlaceholderConfigurer,enumConverterFactory,platformSessionArgumentResolvers,myNotPad,scopedTarget.shoppingCart,shoppingCart,scopedTarget.shoppingCart2,shoppingCart2,setPeppers,cdPlayer,myDisc,nowDate,myDessert,org.springframework.aop.config.internalAutoProxyCreator,point,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,org.springframework.aop.aspectj.AspectJPointcutAdvisor#3,org.springframework.aop.aspectj.AspectJPointcutAdvisor#4,org.springframework.aop.aspectj.AspectJPointcutAdvisor#5,trackPlayer,org.springframework.aop.aspectj.DeclareParentsAdvisor#0,disc]; root of factory hierarchy
CDPlayer: step 4 setApplicationContext=org.springframework.context.annotation.AnnotationConfigApplicationContext@48533e64: startup date [Tue Sep 25 10:24:42 CST 2018]; root of context hierarchy
CDPlayer: step 5 init
CDPlayer: step 6 afterPropertiesSet

// 容器中其他的 bean 被装配
CDPlayer: step 7 postProcessBeforeInitialization=org.springframework.context.event.internalEventListenerProcessor
CDPlayer: step 8 postProcessAfterInitialization=org.springframework.context.event.internalEventListenerProcessor
CDPlayer: step 7 postProcessBeforeInitialization=org.springframework.context.event.internalEventListenerFactory
CDPlayer: step 8 postProcessAfterInitialization=org.springframework.context.event.internalEventListenerFactory
CDPlayer: step 7 postProcessBeforeInitialization=appConfig
CDPlayer: step 8 postProcessAfterInitialization=appConfig
CDPlayer: step 7 postProcessBeforeInitialization=trackCounter
CDPlayer: step 8 postProcessAfterInitialization=trackCounter
CDPlayer: step 7 postProcessBeforeInitialization=disc
CDPlayer: step 8 postProcessAfterInitialization=disc
CDPlayer: step 7 postProcessBeforeInitialization=aspectJInvoker
CDPlayer: step 8 postProcessAfterInitialization=aspectJInvoker
CDPlayer: step 7 postProcessBeforeInitialization=audience
CDPlayer: step 8 postProcessAfterInitialization=audience
CDPlayer: step 7 postProcessBeforeInitialization=encoreableIntroducer
CDPlayer: step 8 postProcessAfterInitialization=encoreableIntroducer

猜你喜欢

转载自blog.csdn.net/aimeimeiTS/article/details/82875821