SpringBoot将自己的代码加到Bean的生命周期中

bean的生命周期就是 bean的创建----->初始化----->销毁方法

由容器管理Bean的生命周期,我们可以通过自己指定bean的初始化方法和bean的销毁方法

主要知识点:

1. 在主配置类中定义一个类的init()方法和destory()方法,destory()方法只有在单例模式下才有效。其中的init方法会在BeanPostProcessor接口的postProcessBeforeInitialization()方法之后,postProcessAfterInitialization方法之前被调用。destory()方法会在容器销毁时被调用。

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

再Car中定义init和destroy方法 

package com.tuling.testbeanlifecycle;


public class Car {

    public Car() {
        System.out.println("Car ==  的构造方法..........");
    }
    public void init() {
        System.out.println("Car ==  init() 方法......");
    }
    public void destroy() {
        System.out.println("Car ==  destroy() 方法,单例模式下会被调用.....");
    }
}

2. 设置bean的Scope,默认是singleton。只有singleton的bean生命周期才会被spring容器管理。针对多实例bean的话,容器启动的时候,bean是不会被创建的而是在获取bean的时候被创建,而且bean的销毁不受IOC容器的管理.

@Scope(value = "prototype"), @Scope(value = "singleton")

@Bean
@Scope(value = "prototype")
public Person person() {   
 return new Person();
}

3. 类A实现 了InitializingBean接口,会默认重写afterPropertiesSet()方法,此方法会在BeanPostProcessor 接口的postProcessBeforeInitialization()方法后,该类A的init()方法前执行。

package com.tuling.testbeanlifecycle;

import org.springframework.beans.factory.InitializingBean;

public class TulingLog implements InitializingBean {

    public TulingLog() {
        System.out.println("TulingLog -InitializingBean == 构造方法");
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("TulingLog -InitializingBean ==  afterPropertiesSet() 方法");
    }
    public void init() {
        System.out.println("TulingLog -InitializingBean ==  init()方法");
    }
}

4.实现了DisposableBean接口,会默认重写destroy()方法,该方法会在容器销毁时被调用。同样只有单例的bean该方法才会被调用。多例的bean的生命周期不受spring控制。

package com.tuling.testbeanlifecycle;

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


public class Person implements InitializingBean,DisposableBean {

    public Person() {
        System.out.println("Person -- 的构造方法");
    }
    @Override
    public void destroy() throws Exception {
        System.out.println("Person -- DisposableBean === destroy()方法 ");
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Person -- InitializingBean === afterPropertiesSe()t方法");
    }
}

5.通过JSR250规范 提供的注解,@PostConstruct注解,@PreDestroy注解。@PostConstruct注解的方法, 会在默认的init方法之前执行。@PreDestroy注解定义的方法会在默认的destory方法之前执行,具体顺序如下:

    @PostConstruct
    public void init() {
        System.out.println("Book ==  的 @PostConstruct 标志的方法");
    }
    @PreDestroy
    public void destory() {
        System.out.println("Book ==  的 @PreDestroy 标注的方法,单例模式下会被调用..... ");
    }

6. 实现了BeanPostProcessor的类,会默认重写postProcessBeforeInitialization() 方法和postProcessAfterInitialization()方法,容器中的每个类在初始化之前都会被这连个方法所拦截。这两个方法一般在init()的前后被执行。

package com.tuling.testbeanlifecycle;

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

/**
 * 后置处理器  在bean调用初始化方法前后进行调用
 */
public class TulingBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("------------- PostProcessor...BeforeInitial :"+beanName);
        return bean;
    }
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("------------- PostProcessor...AfterInitial :"+beanName);
        return bean;
    }
}

 

所具体类如下:

Book 类,用来观察以上所有钩子的执行顺序。。。

package com.tuling.testbeanlifecycle;

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

public class Book {

    public Book() {
        System.out.println("Book ==  的构造方法");
    }
    @PostConstruct
    public void init() {
        System.out.println("Book ==  的 @PostConstruct 标志的方法");
    }
    @PreDestroy
    public void destory() {
        System.out.println("Book ==  的 @PreDestroy 标注的方法,单例模式下会被调用..... ");
    }
    public void defaultInit(){
        System.out.println("Book ==  的 init() 方法 ... ");
    }
    public void defaultDestory(){
        System.out.println("Book ==  的 destory() 方法 ... ");
    }
}

 

主配置类: 

package com.tuling.testbeanlifecycle;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
@ComponentScan(basePackages = "com.tuling.testbeanlifecycle")
public class MainConfig {

    @Bean(initMethod = "init",destroyMethod = "destroy")
    public Car car() {
        return new Car();
    }
    @Bean(initMethod = "init")
    public TulingLog tulingLog() {
        return new TulingLog();
    }
    @Bean
    public TulingBeanPostProcessor tulingBeanPostProcessor() {
        return new TulingBeanPostProcessor();
    }
    @Bean(initMethod = "defaultInit",destroyMethod = "defaultDestory")
    public Book book() {
        return new Book();
    }
    @Bean
    @Scope(value = "prototype")
    public Person person() {
        return new Person();
    }
}

 main方法类:

package com.tuling.testbeanlifecycle;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
ed by smlz on 2019/5/24.
 */
public class MainClass {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class);
        Car car = ctx.getBean(Car.class);
        Book book = ctx.getBean(Book.class);
        Person person = ctx.getBean(Person.class);
        TulingLog  log = ctx.getBean(TulingLog.class);
        ctx.close();
    }
}

执行结果:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值