Bean的生命週期


Bean的生命週期

bean建立-->初始化-->銷燬app

容器管理Bean的生命週期

咱們能夠自定義初始化和銷燬方法,容器在bean進行到當前生命週期的時候來調用咱們自定義的初始化和銷燬方法ide

  • 建立(對象建立)post

    單實例:在容器啓動的時候建立測試

    多實例:在每次獲取的時候建立code

  • 初始化對象

    對象建立完成,並賦值好,調用初始化方法排序

  • 銷燬接口

    單實例bean在容器關閉的時候銷燬生命週期

    多實例bean容器不會調用銷燬,這時候能夠手動來調用銷燬方法get

1. 指定初始化和銷燬方法:

經過@Bean註解指定init-method和destory-method

要被注入的bean類Car

@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 destory---");
    }
}

配置類:指定了初始化方法initMethod = "init"和銷燬方法destroyMethod = "destroy"

@Configuration
public class BeanConfigOfLifeCycle {

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

測試方法

public class IOCTestLifeCycle {

    @Test
    public void test01() {
        //一、ioc容器的建立
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfigOfLifeCycle.class);
        System.out.println("容器建立完成---");
        //關閉容器
        applicationContext.close();
    }
}

運行結果

car constructor---
car init---
容器建立完成---
car destory---

2. 經過實現接口的方式

讓Bean實現Spring提供的InitializingBean(定義初始化邏輯)接口和 DisposableBean(定義銷燬邏輯)接口

@Component
public class Cat implements InitializingBean, DisposableBean {

    public Cat(){
        System.out.println("car constructor---");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("cat destory---");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("cat afterPropertiesSet---");
    }
}

3. 使用JSR250

@PostConstruct:在bean建立完成而且屬性賦值完成以後來執行初始化方法

@PreDestroy:在容器銷燬bean以前通知咱們進行清理工做

@Component
public class Dog implements ApplicationContextAware {

    public Dog(){
        System.out.println("dog constructor---");
    }

    /**
     * 對象建立並賦值以後調用
     */
    @PostConstruct
    public void init(){
        System.out.println("dog postConstructor---");
    }

    /**
     * 在容器移除對象以前調用
     */
    @PreDestroy
    public void preDestroy(){
        System.out.println("dog preDestroy---");
    }

}

4. 使用Bean的後置處理器

BeanPostProcessor接口:bean的後置處理器,在bean初始化先後進行一些處理工做
postProcessBeforeInitialization:在初始化以前工做
postProcessAfterInitialization:在初始化以後工做

自定義的BeanPostProcessor還能夠實現Ordered接口進行自定義排序

public class MyBeanPostProcessor implements BeanPostProcessor, Ordered {

    /**
     * 在全部其餘初始化操做(Bean指定、實現、JSR250)以前執行處理
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization---"+beanName+"---");
        return bean;
    }

    /**
     * 在全部初始化以後操做以後執行處理
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization---"+beanName+"---");
        return bean;
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

配置類:@Import注入MyBeanPostProcessor

@Configuration
@Import(MyBeanPostProcessor.class)
public class BeanConfigOfLifeCycle {

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

}

 

測試方法:

public class IOCTestLifeCycle {

    @Test
    public void test01() {
        //一、ioc容器的建立
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfigOfLifeCycle.class);
        System.out.println("容器建立完成---");
        //關閉容器
        applicationContext.close();
    }
}

運行結果

car constructor---
postProcessBeforeInitialization---car---
car init---
postProcessAfterInitialization---car---
容器建立完成---
car destory---
相關文章
相關標籤/搜索