美文网首页
Spring源码解析(十五)-Bean生命周期

Spring源码解析(十五)-Bean生命周期

作者: 秋水畏寒 | 来源:发表于2020-05-27 22:30 被阅读0次

    Spring版本

    5.2.5.RELEASE

    参考

    《芋道源码》

    Demo

    从前面的众多源码解读章节,其实我们大致了解了bean的生命周期,下面通过一个具体的例子,来加深对于这一块的理解。

    LifeCycleBean

    public class LifeCycleBean implements BeanPostProcessor, BeanNameAware, BeanClassLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean {
    
        private String name;
    
    
    
        @Override
        public void setBeanClassLoader(ClassLoader classLoader) {
            System.out.println("setBeanClassLoader");
        }
    
        @Override
        public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
            System.out.println("setBeanFactory");
        }
    
        @Override
        public void setBeanName(String name) {
            System.out.println("setBeanName");
        }
    
        @Override
        public void destroy() throws Exception {
            System.out.println("destroy");
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("afterPropertiesSet");
        }
    
        public void destroyMethod() {
            System.out.println("destroyMethod");
        }
    
        public void initMethod() {
            System.out.println("initMethod");
        }
    
        public LifeCycleBean() {
            System.out.println("constructionMethod");
        }
    
        public void normalMethod() {
            System.out.println("normalMethod");
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            System.out.println("set name property");
            this.name = name;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("postProcessAfterInitialization");
            return bean;
        }
    
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("postProcessBeforeInitialization");
            return bean;
        }
    }
    

    spring.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="lifeCycleBean" class="com.kungyu.custom.element.LifeCycleBean" destroy-method="destroyMethod" init-method="initMethod">
            <property name="name" value="name"/>
        </bean>
    </bean>
    

    测试

    public class Test {
        public static void main(String[] args) throws ClassNotFoundException {
    
            ClassPathResource resource = new ClassPathResource("spring.xml");
            DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
            factory.addBeanPostProcessor(new LifeCycleBean());
            XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
            reader.loadBeanDefinitions(resource);
    
            LifeCycleBean lifeCycleBean = (LifeCycleBean) factory.getBean("lifeCycleBean");
            lifeCycleBean.normalMethod();
    
            factory.destroySingleton("lifeCycleBean");
    
        }
    }
    

    结果

    结果

    总结

    bean生命周期.png

    相关文章

      网友评论

          本文标题:Spring源码解析(十五)-Bean生命周期

          本文链接:https://www.haomeiwen.com/subject/pmwbahtx.html