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---
相关文章
相关标签/搜索