广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Spring中Bean初始化和销毁的方式总结
  • 714
分享到

Spring中Bean初始化和销毁的方式总结

Spring Bean初始化Spring Bean销毁Spring Bean 2023-05-16 14:05:11 714人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录一、Bean的多种初始化方式1.PostConstruct注解2.实现InitializingBean接口3.声明init-method方法二、Bean的多种销毁方式1.PreD

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声明的初始化方法");
    }
}

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

可以看到他们的顺序是固定的即: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内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Spring中Bean初始化和销毁的方式总结

本文链接: https://www.lsjlt.com/news/209977.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作