zoukankan      html  css  js  c++  java
  • Spring应用上下文中Bean的生命周期

    Bean装载到Spring应用上下文的生命周期,如图:

    Bean在Spring容器中从创建到销毁经历了若干个阶段,每一阶段都可以对Spring如何管理Bean进行个性化定制,以下我们通过代码去验证生命周期以及个性化定制方法;

    BeanLife实现Aware接口、InitializingBean、DisposableBean接口,自定义生命周期中的方法。

    /**
     * @name Bean生命周期
     */
    public class BeanLife implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean,DisposableBean{
        
        private String beanProperty;//私有属性
        
        private String beanName; //接收BeanNameAware的beanName传入
        private BeanFactory beanFactory;//接收BeanFactoryAware的beanFactory传入
        private ApplicationContext applicationContext;//接收ApplicationContextAware的applicationContext传入
        
        /**
         * 构造函数
         */
        public BeanLife(String beanProperty){
            System.out.println("BeanLife constructed with "+beanProperty+"......");
        }
        
        /**
         * bean属性get
         */
        public String getBeanProperty() {
            return beanProperty;
        }
    
        /**
         * bean填充属性
         */
        public void setBeanProperty(String beanProperty) {
            System.out.println("BeanLife setBeanProperty:"+beanProperty);
            this.beanProperty = beanProperty;
        }
    
        /**
         * init-method关联方法
         */
        public void begin(){
            System.out.println("init-method:begin()");
        }
        /**
         * destroy-method关联方法
         */
        public void end(){
            System.out.println("destroy-method:end()");
        }
        
        /**
         * 准备就绪的bean,使用Bean做些事情
         */
        public void doSomething(){
            System.out.println("BeanLife can be used: do something.");
        }
        
        /**
         * Spring将BeanFactory容器实例传入
         */
        public void setBeanFactory(BeanFactory arg0) throws BeansException {
            this.beanFactory=arg0;
            System.out.println("BeanFactoryAware---BeanLife setBeanFactory:"+this.beanFactory.toString());
        }
        
        /**
         * Spring将Bean的ID传入
         */
        public void setBeanName(String arg0) {
            this.beanName = arg0;
            System.out.println("BeanNameAware---BeanLife setBeanName:"+this.beanName);
        }
        
        /**
         * Spring将应用上下文的引用传入
         */
        public void setApplicationContext(ApplicationContext arg0)
                throws BeansException {
            this.applicationContext = arg0;
            System.out.println("ApplicationContextAware---BeanLife setApplicationContext:"+this.applicationContext.getApplicationName());
        }
    
        /**
         * 属性设置完毕后
         */
        public void afterPropertiesSet() throws Exception {
            System.out.println("InitializingBean---After SetProperties");
        }
        /**
         * Bean销毁
         */
        public void destroy() throws Exception {
            System.out.println("DisposableBean---BeanLife Bean destroy.");
        }
    }

    自定义一个BeanPostProcessor进行监控Spring容器Bean实例化注入时的回调方法定制

    /**
     * @name BeanPostProcessor实现
     */
    public class BeanLifePostProcessor implements BeanPostProcessor{
    
        /**
         * 实例化、依赖注入完毕,在调用显示的初始化之前完成一些定制的初始化任务
         * @param arg0 Bean对象
         * @param arg1 Bean的ID
         */
        public Object postProcessBeforeInitialization(Object arg0, String arg1)
                throws BeansException {
            System.out.println("BeanPostProcessor---Before "+arg1+"'s Initialization ");
            return arg0;
        }
        
        /**
         * 实例化、依赖注入、初始化完毕时执行
         * @param arg0 Bean对象
         * @param arg1 Bean的ID
         */
        public Object postProcessAfterInitialization(Object arg0, String arg1)
                throws BeansException {
    
            System.out.println("BeanPostProcessor---After "+arg1+"'s Initialization");
            return arg0;
        }
        
    }

    applicationContext.xml配置

    <!-- BeanLife -->
    <bean id="beanlife" class="com.wjx.betalot.BeanLife" init-method="begin" destroy-method="end">
        <constructor-arg value="beanlife construct param"></constructor-arg>
        <property name="beanProperty" value="beanlife cycle"></property>
    </bean>
    <!-- beanPostProcessor -->
    <bean id="beanPostProcessor" class="com.wjx.betalot.BeanLifePostProcessor"></bean>

    验证测试:

    public static void main( String[] args ) throws Exception{
    
            ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
            BeanLife beanlife = (BeanLife) context.getBean("beanlife");
            beanlife.doSomething();
            //销毁bean
            ((ClassPathXmlApplicationContext)context).registerShutdownHook();
    }

    测试结果:

    BeanLife constructed with beanlife construct param......
    BeanLife setBeanProperty:beanlife cycle
    BeanNameAware---BeanLife setBeanName:beanlife
    BeanFactoryAware---BeanLife setBeanFactory:org.springframework.beans.factory.support.DefaultListableBeanFactory@5eed2fce: defining beans [beanlife,beanPostProcessor]; root of factory hierarchy
    ApplicationContextAware---BeanLife setApplicationContext:
    BeanPostProcessor---Before beanlife's Initialization 
    InitializingBean---After SetProperties
    init-method:begin()
    BeanPostProcessor---After beanlife's Initialization
    BeanLife can be used: do something.
    DisposableBean---BeanLife Bean destroy.
    destroy-method:end()

    可对照Bean的生命周期图,进行验证。

     

  • 相关阅读:
    特殊方法(双下方法)
    反射
    属性
    类方法、静态方法
    封装
    python接口类,抽象类
    Yii2基本概念之——事件(Event)
    Yii2基本概念之——行为(Behavior)
    yii2 migrate 数据库迁移的简单分享
    Yii2.0 RESTful API 之速率限制
  • 原文地址:https://www.cnblogs.com/fightingcoding/p/springBean.html
Copyright © 2011-2022 走看看