spring :bean的初始化和销毁方法

通过初始化和销毁方法可以在bean刚刚被创建时和销毁时对其进行一些我们需要的处理。
spring下实现初始化和销毁方法的方式主要有以下:

  1. 自定义初始化和销毁函数,声明bean时通过initMethod 、destroyMethod指定
  2. 实现InitializingBean,、DisposableBean接口
  3. 使用@PostConstruct、 @PreDestroy
  4. 实现BeanPostProcessor接口

注:

  1. 以上四种方式优先级逐渐升高,即对象创建后最先调用BeanPostProcessor接口的postProcessBeforeInitialization方法。最后调用自定义的通过initMethod声明的初始化方法。初始化结束后,最先调用BeanPostProcessor接口的postProcessAfterInitialization最后调用自定义的通过destroyMethod声明的初始化方法。

  2. 前三种方式只针对某个具体的类,BeanPostProcessor会拦截容器中所有的对象。

  3. 单例模式下在spring容器关闭时,会销毁对象。但是在原型模式下,spring容器不会再管理这个bean,如果需要,要自己调用销毁方法

自定义初始化和销毁函数

初始化和销毁方法名随意

public class Person {
    private int id;
    private String name;
    private int age;
    private String sex;

    public void personInit(){
        System.out.println("Person初始化");
    }

    public void personDestroy(){
        System.out.println("Person销毁");
    }
    public Person() {
    }

    public Person(int id, String name, int age, String sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
        System.out.println("创建person对象。");
    }

    //getters、、setters
}

在配置类中通过initMethod 、destroyMethod指定初始化和销毁方法

 @Bean(value="zs",initMethod = "personInit" ,destroyMethod = "personDestroy")  //id=zs ,方法名;指定初始化和销毁方法。
    @Lazy
    public Person zs(){
        return new Person(22,"张三",20,"male");
    }

测试:

public static void testBeanInitAndDestroy(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        Person zs = (Person)context.getBean("zs");
        context.close();
    }

运行结果
在这里插入图片描述
注意这里可以看出,对象是先被创建出来后再执行初始化方法的。

实现InitializingBean,、DisposableBean接口

@Service
public class PersonService implements InitializingBean, DisposableBean {
    @Override
    public void destroy() throws Exception {
        System.out.println("personService销毁");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("personService初始化");
    }
}

使用@PostConstruct、 @PreDestroy

@Repository
public class PersonDao {
    @PostConstruct
    public void personDaoInit(){
        System.out.println("personDaoInit");
    }
    @PreDestroy
    public void personDaoDestroy(){
        System.out.println("personDaoDestroy");
    }
}

BeanPostProcessor接口

@Controller
public class MyController implements BeanPostProcessor {
    public  Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof Person){
            Person p = (Person)bean;
            p.setName("zzzzzzs");
            return bean;
        }
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}

看参数,bean即为拦截到的bean对象。在spring容器开启之后,MyController便会开始在每个对象创建后对其进行拦截并进行相应处理。

测试:

public static void testBeanPostProcessor(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        Person zs = (Person)context.getBean("zs");
        System.out.println(zs.getName());
        context.close();

    }

在配置类中zs的名字设置为张三
在这里插入图片描述运行结果:名字已经被改为zzzzzs.
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值