Spring中Bean初始化和销毁的方法是什么

发布时间:2023-04-17 11:15:46 作者:iii
来源:亿速云 阅读:108

今天小编给大家分享一下Spring中Bean初始化和销毁的方法是什么的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

Spring中支持在Bean的加载时声明初始化方法,该方法会在Bean对象完成初始化之前进行执行,可以为对象指定一些特定的行为,同样的Bean销毁时,也是支持这个动作的。其中因为对象的作用域不同,销毁的表现形式略有区别。初始化都没有区别,无论是单例、原型、request、session、global session等他们的创建时初始化都没啥区别,但是销毁会略有区别,单例模式默认不会销毁,只有在Spring容器被销毁时才会执行Bean的销毁,从而执行他的销毁方法。session、request等他们是作用范围到了就会被销毁,并不会长期存在,所以他们的销毁方法是在作用范围执行之后来调用的。

一、Bean的多种初始化方式

Spring中总共支持了三种方式对Bean进行初始化,依次是在方法上使用PostConstruct注解、实现InitializtingBean接口重写对应方法、声明init-method方法来实现,且他们三个支持并行。也就是说我们可以三个都是用,当三个都是用时就是按照下面的顺序执行的,即限制性PostConstruct注解的方法,再执行InitializingBean的方法,最终执行init-method的方法。

1.PostConstruct注解

如下所示,这里使用配置类的方式进行注入,因为一会延时init-method必须使用配置类才可以实现,启动容器当加载TestA这个Bean时,他的初始化方法就会被执行。

@Configuration
public class TestInitmestond {

    @Bean
    public TestA getBeanA(){
        return new TestA();
    }
}

class TestA{
    @PostConstruct
    public void postConstructor(){
        System.out.println("这是使用了PostConstruct注解的初始化方法");
    }

}

2.实现InitializingBean接口

下面是结合了第一种和第二种的初始化方式:

@Configuration
public class TestInitmestond {

    @Bean
    public TestA getBeanA(){
        return new TestA();
    }
}

class TestA implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("这是实现InitializingBean的初始化方法");
    }

    @PostConstruct
    public void postConstructor(){
        System.out.println("这是使用了PostConstruct注解的初始化方法");
    }

}

3.声明init-method方法

init-method方法必须使用配置类进行加载Bean才可以配置,因为该属性是Bean标签的属性,在注解中也就是Bean注解的属性,所以我们使用Component等其他IOC注解时是无法指定的。

@Configuration
public class TestInitmestond {

    @Bean(initMethod = "initMethod")
    public TestA getBeanA(){
        return new TestA();
    }
}

class TestA implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("这是实现InitializingBean的初始化方法");
    }

    @PostConstruct
    public void postConstructor(){
        System.out.println("这是使用了PostConstruct注解的初始化方法");
    }

    public void initMethod(){
        System.out.println("这是使用了init-method声明的初始化方法");
    }
}

下面启动下容器展示下他们的执行顺序,如下:

Spring中Bean初始化和销毁的方法是什么

可以看到他们的顺序是固定的即:PostConstruct—>initializingBean—>init-method.

二、Bean的多种销毁方式

同样的Spring也支持了三种销毁的方式,且这三种销毁方式与三种创建方式是完全对应的。同时与初始化方法一样Spring也是支持三种销毁方法的并行的。且他们并行时顺序是固定的:执行PreDestroy–>DisposableBean–>destroy-method.

1.PreDestroy注解

这里容器采用手动启动的方式进行创建,然后为容器设置一个销毁的钩子,这样当容器销毁时我们就可以去执行销毁方法了,对于单例模式的销毁方法只能通过这种测试了,若是我们直接停止IDEA的服务是不会执行销毁方法的。不过对于scope不是singleton的Bean来说,比如request在正常服务里是可以体现销毁动作的。

public class TestDestroyMethod {
    //手动启动容器,模拟关闭
    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(TestConfig.class);
        annotationConfigApplicationContext.start();
        annotationConfigApplicationContext.registerShutdownHook();
    }
}
@Configuration
class TestConfig{

    @Bean
    public TestB getBean(){
        return new TestB();
    }
}
class TestB{

    @PreDestroy
    public void preDestroy(){
        System.out.println("这是使用PreDestroy注解的销毁方法");
    }
}

2.实现DisposableBean接口

这种就是直接实现接口重写destroy方法即可

public class TestDestroyMethod {
    //手动启动容器,模拟关闭
    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(TestConfig.class);
        annotationConfigApplicationContext.start();
        annotationConfigApplicationContext.registerShutdownHook();
    }
}
@Configuration
class TestConfig{

    @Bean
    public TestB getBean(){
        return new TestB();
    }
}
class TestB implements DisposableBean {

    @PreDestroy
    public void preDestroy(){
        System.out.println("这是使用PreDestroy注解的销毁方法");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("这是实现DisposableBean的方法");
    }
}

3.声明destroy-method方法

下面是结合了三种销毁方法的代码

public class TestDestroyMethod {
    //手动启动容器,模拟关闭
    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(TestConfig.class);
        annotationConfigApplicationContext.start();
        annotationConfigApplicationContext.registerShutdownHook();
    }
}
@Configuration
class TestConfig{

    @Bean(destroyMethod = "destroyMethod")
    public TestB getBean(){
        return new TestB();
    }
}
class TestB implements DisposableBean {

    @PreDestroy
    public void preDestroy(){
        System.out.println("这是使用PreDestroy注解的销毁方法");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("这是实现DisposableBean的方法");
    }

    public void destroyMethod(){
        System.out.println("这是制定了destroy-method的销毁方法");
    }
}

下面是执行的截图,可以看到三种销毁方式与初始化方式一样都是有固定顺序的,事实上初始化方式与销毁方式他们是有对应关系的。PostConstruct与PreDestroy是一组,InitializingBean与DisposableBean是一组,init-method与destroy-method是一组。

Spring中Bean初始化和销毁的方法是什么

以上就是“Spring中Bean初始化和销毁的方法是什么”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

推荐阅读:
  1. SpringBoot之RabbitMQ的使用方法
  2. SpringBoot 使用jwt进行身份验证的方法示例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring bean

上一篇:如何用VS code快速搭建一个Golang项目

下一篇:Vue移动端下拉刷新组件如何使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》