BeanLiftCycle(1)

Bean生命周期图:

1、 创建BeanFactory(DefaultListableBeanFactory)

2、 手动注册BeanDefinition(ImportBeanDefinitionRegistrar.registerBeanDefinitions)

3、 BeanFactory后置处理(BeanFactoryPostProcessor.postProcessBeanFactory)

4、 调用Bean.class的构造器实例化Bean

5、 Bean初始化之前处理(BeanPostProcessor.postProcessBeforeInitialization)

6、 Bean构造器后置处理(PostConstruct)

7、 设置属性(InitializingBean .afterPropertiesSet)

8、 执行InitMethod

9、 Bean初始化结束之后处理(BeanPostProcessor.postProcessAfterInitialization)

10、Bean摧毁之前处理(PreDestroy)

11、Bean摧毁

 

启动类:

public class Start {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
        applicationContext.close();
    }
}

配置类:

@Configurable
@ComponentScan({ "com.spring.demo.beanlifecycle" })
@Import(ImportBeanDefinitionRegistrar_.class)
public class Config {
}

Bean:

public class Bean implements InitializingBean {
    public Bean() {
        System.out.println("invoke Construct: " + this);
    }
    public void init() {
        System.out.println("init: " + this);
    }
    public void destroy() {
        System.out.println("destroy: " + this);
    }
    @PostConstruct
    public void postConstruct() {
        System.out.println("PostConstruct: " + this);
    }
    @PreDestroy
    public void preDestroy(){
        System.out.println("PreDestroy: " + this);
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet: " + this);
    }
}

  

registerBeanDefinitions:

public class ImportBeanDefinitionRegistrar_ implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        BeanDefinition beanDefinition =
            BeanDefinitionBuilder.rootBeanDefinition(Bean.class).setInitMethodName("init").setDestroyMethodName("destroy").getBeanDefinition();
        registry.registerBeanDefinition("bean", beanDefinition);
        System.out.println("registerBeanDefinitions: " + beanDefinition);
    }
}

  

BeanFactoryPostProcessor:

@Component
public class BeanFactoryPostProcessor_ implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("postProcessBeanFactory :" + beanFactory.getBeanDefinition("bean"));
    }
}

  

BeanPostPrcesser

@Component
public class BeanPostProcessor_ implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean.getClass().equals(Bean.class)) {
            System.out.println("postProcessBeforeInitialization : " + bean.toString());
        }
        return bean;
    }
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean.getClass().equals(Bean.class)) {
            System.out.println("postProcessAfterInitialization : " + bean.toString());
        }
        return bean;
    }
}

执行结果

registerBeanDefinitions: Root bean: class [com.spring.demo.beanlifecycle.Bean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=init; destroyMethodName=destroy
postProcessBeanFactory : Root bean: class [com.spring.demo.beanlifecycle.Bean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=init; destroyMethodName=destroy
invoke Construct: com.spring.demo.beanlifecycle.Bean@74294adb
postProcessBeforeInitialization : com.spring.demo.beanlifecycle.Bean@74294adb
PostConstruct: com.spring.demo.beanlifecycle.Bean@74294adb
afterPropertiesSet: com.spring.demo.beanlifecycle.Bean@74294adb
init: com.spring.demo.beanlifecycle.Bean@74294adb
postProcessAfterInitialization : com.spring.demo.beanlifecycle.Bean@74294adb
PreDestroy: com.spring.demo.beanlifecycle.Bean@74294adb
destroy: com.spring.demo.beanlifecycle.Bean@74294adb

over

posted @ 2019-06-22 23:14  喝花茶  阅读(255)  评论(0编辑  收藏  举报