美文网首页
Bean生命周期

Bean生命周期

作者: JBryan | 来源:发表于2020-01-19 10:08 被阅读0次

bean的生命周期:创建----->初始化----->销毁
自定义初始化和销毁方法
新建Car类,添加@Component注解,新建init(),destroy方法

package com.ljessie.bean;

import org.springframework.stereotype.Component;

@Component
public class Car {
    public Car(){
        System.out.println("Car Constructor......");
    }

    public void init(){
        System.out.println("Car init......");
    }

    public void destroy(){
        System.out.println("Car destroy......");
    }
}

新建MainConfigOfLifeCircle类,添加@Configuration,@ComponentScan注解。使用@Bean方式添加Car组件,并指定初始化和销毁方法

package com.ljessie.config;

import com.ljessie.bean.Car;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.ljessie.bean")
public class MainConfigOfLifeCycle {

    @Bean(initMethod = "init",destroyMethod = "destroy")
    public Car car(){
        return new Car();
    }
}

新建IOCTest_LifeCycle

package com.ljessie.test;

import com.ljessie.config.MainConfigOfLifeCycle;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class IOCTest_LifeCycle {

    @Test
    public void test01(){
        //创建IOC容器
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
        System.out.println("容器创建完成");
        //关闭容器
        annotationConfigApplicationContext.close();
    }
}

运行test01()

Car Constructor......
Car init......
容器创建完成
一月 19, 2020 9:29:13 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7a46a697: startup date [Sun Jan 19 09:29:13 CST 2020]; root of context hierarchy
Car destroy......

通过让Bean实现InitializingBean(定义初始化逻辑),实现DisposableBean(定义销毁逻辑)
新建Cat类,添加@Component注解,实现InitializingBean和DisposableBean接口

package com.ljessie.bean;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

@Component
public class Cat implements InitializingBean, DisposableBean {
    public Cat(){
        System.out.println("Cat Constructor....");
    }

    public void destroy() throws Exception {
        System.out.println("Cat destroy....");
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("Cat afterPropertiesSet....");
    }
}

运行test01():afterPropertiesSet()方法在属性赋值完成之后调用。

Car Constructor......
Car init......
Cat Constructor....
Cat afterPropertiesSet....
容器创建完成
一月 19, 2020 9:34:50 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7a46a697: startup date [Sun Jan 19 09:34:49 CST 2020]; root of context hierarchy
Cat destroy....
Car destroy......

通过注解指定初始化和销毁方法
@PostConstruct:在bean创建完成,并属性赋值完成之后,来执行初始化方法
@PreDestroy:在容器销毁bean之前,通知我们进行清理操作
新建Dog类

package com.ljessie.bean;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component
public class Dog implements ApplicationContextAware {
    public Dog(){
        System.out.println("Dog Constructor.......");
    }

    /**
     * 对象创建并赋值之后调用
     */
    @PostConstruct
    public void postConstructor(){
        System.out.println("Dog postConstructor.......");
    }

    /**
     * 容器移除之前调用
     */
    @PreDestroy
    public void preDestroy(){
        System.out.println("Dog preDestroy.......");
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

    }
}

运行test01()

Car Constructor......
Car init......
Cat Constructor....
Cat afterPropertiesSet....
Dog Constructor.......
Dog postConstructor.......
容器创建完成
一月 19, 2020 9:47:45 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7a46a697: startup date [Sun Jan 19 09:47:44 CST 2020]; root of context hierarchy
Dog preDestroy.......
Cat destroy....
Car destroy......

接口BeanPostProcessor:bean的后置处理器
在bean初始化前后进行一些处理工作
postProcessBeforeInitialization:初始化之前调用
postProcessAfterInitialization:初始化之后调用
新建MyBeanPostProcessor实现BeanPostProcessor接口

package com.ljessie.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    /**
     *
     * @param o 刚创建的实例
     * @param s 刚创建的实例名字
     * @return
     * @throws BeansException
     */
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        System.out.println("postProcessBeforeInitialization......"+s+"--->"+o);
        return o;
    }

    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
        System.out.println("postProcessAfterInitialization......"+s+"--->"+o);
        return o;
    }
}

运行test01()

postProcessBeforeInitialization......org.springframework.context.event.internalEventListenerProcessor--->org.springframework.context.event.EventListenerMethodProcessor@36f0f1be
postProcessAfterInitialization......org.springframework.context.event.internalEventListenerProcessor--->org.springframework.context.event.EventListenerMethodProcessor@36f0f1be
postProcessBeforeInitialization......org.springframework.context.event.internalEventListenerFactory--->org.springframework.context.event.DefaultEventListenerFactory@6ee12bac
postProcessAfterInitialization......org.springframework.context.event.internalEventListenerFactory--->org.springframework.context.event.DefaultEventListenerFactory@6ee12bac
postProcessBeforeInitialization......mainConfigOfLifeCycle--->com.ljessie.config.MainConfigOfLifeCycle$$EnhancerBySpringCGLIB$$dbd8006e@64c87930
postProcessAfterInitialization......mainConfigOfLifeCycle--->com.ljessie.config.MainConfigOfLifeCycle$$EnhancerBySpringCGLIB$$dbd8006e@64c87930
Car Constructor......
postProcessBeforeInitialization......car--->com.ljessie.bean.Car@212bf671
Car init......
postProcessAfterInitialization......car--->com.ljessie.bean.Car@212bf671
Cat Constructor....
postProcessBeforeInitialization......cat--->com.ljessie.bean.Cat@2aece37d
Cat afterPropertiesSet....
postProcessAfterInitialization......cat--->com.ljessie.bean.Cat@2aece37d
Dog Constructor.......
postProcessBeforeInitialization......dog--->com.ljessie.bean.Dog@1d548a08
Dog postConstructor.......
postProcessAfterInitialization......dog--->com.ljessie.bean.Dog@1d548a08
容器创建完成
一月 19, 2020 9:53:32 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7a46a697: startup date [Sun Jan 19 09:53:31 CST 2020]; root of context hierarchy
Dog preDestroy.......
Cat destroy....
Car destroy......

相关文章

网友评论

      本文标题:Bean生命周期

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