zoukankan      html  css  js  c++  java
  • spring(二):bean的生命周期

    bean的生命周期指的是bean的创建——>初始化——>销毁的过程,该过程是由spring容器进行管理的

    我们可以自定义bean初始化和销毁的方法:容器在bean进行到当前生命周期时,调用自定义的初始化和销毁方法

    自定义初始化和销毁方法

    init-method、destroy-method

    配置文件的bean标签中配置init-method、destroy-method属性

    <bean id="user" class="com.enjoy.study.pojo.User" init-method="init" destroy-method="destroy"/>

    bean类
    public class User {
        public User() {
            System.out.println("user....constructor..........");
        }
    
        public void init(){
            System.out.println("user....init..........");
        }
    
        public void destroy(){
            System.out.println("user....destroy..........");
        }
    }

    测试类
    public class Cap7Test {
        @Test
        public void cap7Test(){
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml");
            System.out.println("ioc容器启动完成");
            context.close();
        }
    }

    结果
    user....constructor..........
    user....init..........
    
    ioc容器启动完成
    user....destroy..........

    initMethod、destroyMethod

    @Bean配置initMethod、destroyMethod属性

    单实例情况:

    @Configuration
    public class Cap7MainConfig {
        @Bean(initMethod = "init",destroyMethod = "destroy")
        public User user(){
            return new User();
        }
    }

    测试方法
    @Test
    public void annoTest(){
    	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Cap7MainConfig.class);
    	System.out.println("ioc容器启动完成");
    	context.close();
    }

    结果
    user....constructor..........
    user....init..........
    
    ioc容器启动完成
    user....destroy..........
    

    多实例情况:

    @Configuration
    public class Cap7MainConfig {
        @Scope("prototype")
        @Bean(initMethod = "init",destroyMethod = "destroy")
        public User user(){
            return new User();
        }
    }

    结果
    ioc容器启动完成  

    单实例情况下,容器启动时完成bean的实例化,容器关闭时进行bean的销毁

    多实例情况下,容器启动时不进行bean的实例化,获取bean时实例化bean对象,所以启动后直接关闭容器,容器中并未进行bean的管理

    InitializingBean、DisposableBean接口

    bean实现InitializingBean接口,实现afterPropertiesSet()方法:beanFactory创建对象完成,并且设置完属性后,调用该方法,相当于初始化方法

    bean实现DisposableBean接口,实现destroy()方法:bean对象销毁时,调用该方法

    public class User implements InitializingBean,DisposableBean {
        public User() {
            System.out.println("user....constructor..........");
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("user....afterPropertiesSet.........");
        }
    
        @Override
        public void destroy() throws Exception {
            System.out.println("user....destroy............");
        }
    }

    配置类
    @Configuration
    public class Cap7MainConfig {
        @Bean
        public User user(){
            return new User();
        }
    }

    结果
    user....constructor..........
    user....afterPropertiesSet.........
    ioc容器启动完成
    user....destroy............
    

    @PostConstruct、@PreDestroy

    @Component
    public class User{
        public User() {
            System.out.println("user....constructor..........");
        }
    
        //对象创建并赋值后使用
        @PostConstruct
        public void init() {
            System.out.println("user....init.........");
        }
    
        //容器移除对象之前回调通知,销毁bean
        @PreDestroy
        public void destroy() {
            System.out.println("user....destroy............");
        }
    }

    配置类
    @Configuration
    @ComponentScan("com.enjoy.study.cap7")
    public class Cap7MainConfig {
    }

    结果
    user....constructor..........
    user....init.........
    
    ioc容器启动完成
    user....destroy............
    

    BeanPostProcessor

    后置处理器,负责在初始化前后做相应处理

    /**
     * 后置处理器
     * @Component,将后置处理器加入容器中
     * @author qf
     * @create 2019-05-22 10:05
     */
    @Component
    public class MyBeanPostProcessor implements BeanPostProcessor {
    
        /**
         * 在初始化之前进行相应后置处理工作。比如在init-method方法执行之前,或者initializingBean的afterPropertiesSet方法之前
         * @param bean
         * @param beanName
         * @return
         * @throws BeansException
         */
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("postProcessBeforeInitialization......"+bean+"........."+beanName);
            return bean;
        }
    
        /**
         * 在初始化之后进行相应后置处理工作
         * @param bean
         * @param beanName
         * @return
         * @throws BeansException
         */
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("postProcessAfterInitialization......"+bean+"........."+beanName);
            return bean;
        }
    }
    

    测试

    @Component
    public class User implements InitializingBean,DisposableBean {
    
        @Override
        public void destroy() throws Exception {
            System.out.println("user....destroy........");
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("user....afterPropertiesSet........");
        }
    }

    配置类
    @Configuration
    @ComponentScan("com.enjoy.study.cap8")
    public class Cap8MainConfig {
    }

    测试类
    public class Cap8Test {
        @Test
        public void cap8Test(){
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Cap8MainConfig.class);
            System.out.println("ioc容器创建完成");
            context.close();
        }
    }

    结果
    postProcessBeforeInitialization...............org.springframework.context.event.internalEventListenerProcessor
    postProcessAfterInitialization...............org.springframework.context.event.internalEventListenerProcessor
    postProcessBeforeInitialization...............org.springframework.context.event.internalEventListenerFactory
    postProcessAfterInitialization...............org.springframework.context.event.internalEventListenerFactory
    postProcessBeforeInitialization...............cap8MainConfig
    postProcessAfterInitialization...............cap8MainConfig
    postProcessBeforeInitialization...............user
    user....afterPropertiesSet........
    postProcessAfterInitialization...............user
    
    ioc容器创建完成
    
    user....destroy........
    

      

  • 相关阅读:
    bzoj-2748 2748: [HAOI2012]音量调节(dp)
    bzoj-2338 2338: [HNOI2011]数矩形(计算几何)
    bzoj-3444 3444: 最后的晚餐(组合数学)
    codeforces 709E E. Centroids(树形dp)
    codeforces 709D D. Recover the String(构造)
    codeforces 709C C. Letters Cyclic Shift(贪心)
    codeforces 709B B. Checkpoints(水题)
    codeforces 709A A. Juicer(水题)
    Repeat Number
    hdu 1003 Max Sum (动态规划)
  • 原文地址:https://www.cnblogs.com/qf123/p/10901308.html
Copyright © 2011-2022 走看看