Spring容器中有哪几种Bean初始化方法和销毁方法呢?

戚薇 Spring 发布时间:2023-06-26 17:32:20 阅读数:13159 1
下文笔者讲述Spring容器初始化bean的方法及销毁方法的运行顺序简介说明

Spring之Bean初始化的方法

使用实现InitializingBean/DisposableBean 接口
  来实现初始化之后/销毁之前的操作方法

使用<bean>元素init-method/destroy-method属性
   指定初始化之后/销毁之前调用的操作方法

在指定方法上加上@PostConstruct或@PreDestroy注解
   来制定该方法是在初始化之后还销毁之前调用

Spring容器初始化方法的示例

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
 
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class InitAndDestroySeqBean implements InitializingBean,DisposableBean {
 
    public InitAndDestroySeqBean(){
        System.out.println("执行InitAndDestroySeqBean: 构造方法");
    }
    
    @PostConstruct
    public void postConstruct() {  
       System.out.println("执行InitAndDestroySeqBean: postConstruct");  
    }  
    
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("执行InitAndDestroySeqBean: afterPropertiesSet"); 
    }
    
    public void initMethod() {
        System.out.println("执行InitAndDestroySeqBean: init-method");
    }
 
    @PreDestroy
    public void preDestroy()  {
        System.out.println("执行InitAndDestroySeqBean: preDestroy");
    }
    
    @Override
    public void destroy() throws Exception {
        System.out.println("执行InitAndDestroySeqBean: destroy");
    }
    
    public void destroyMethod() {
        System.out.println("执行InitAndDestroySeqBean: destroy-method");
    }
    
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/chj/spring/bean.xml");
        context.close();
    }
}

Spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     xmlns:context="http://www.springframework.org/schema/context"  
     xsi:schemaLocation="http://www.springframework.org/schema/beans  
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
 http://www.springframework.org/schema/context  
 http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
 
   <context:annotation-config/>  
   
   <bean id="initAndDestroySeqBean" class="com.chj.spring.InitAndDestroySeqBean" init-method="initMethod" destroy-method="destroyMethod"/>
</beans>

运行效果如下所示

运行InitAndDestroySeqBean的main方法,结果如下:
2023-06-26 17:30:22,098 DEBUG support.DefaultlistableBeanFactory - Creating instance of bean 'initAndDestroySeqBean'
执行InitAndDestroySeqBean: 构造方法
2023-06-26 17:30:22,114 DEBUG annotation.CommonAnnotationBeanPostProcessor - Found init method on class [com.alibaba.chj.spring.InitAndDestroySeqBean]: public void com.alibaba.chj.spring.InitAndDestroySeqBean.postConstruct()
2023-06-26 17:30:22,114 DEBUG annotation.CommonAnnotationBeanPostProcessor - Found destroy method on class [com.alibaba.chj.spring.InitAndDestroySeqBean]: public void com.alibaba.chj.spring.InitAndDestroySeqBean.preDestroy()
2023-06-26 17:30:22,129 DEBUG support.DefaultListableBeanFactory - Eagerly caching bean 'initAndDestroySeqBean' to allow for resolving potential circular references
2023-06-26 17:30:22,129 DEBUG annotation.CommonAnnotationBeanPostProcessor - Invoking init method on bean 'initAndDestroySeqBean': public void com.alibaba.chj.spring.InitAndDestroySeqBean.postConstruct()
执行InitAndDestroySeqBean: postConstruct
2023-06-26 17:30:22,129 DEBUG support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'initAndDestroySeqBean'
执行InitAndDestroySeqBean: afterPropertiesSet
2023-06-26 17:30:22,129 DEBUG support.DefaultListableBeanFactory - Invoking init method  'initMethod' on bean with name 'initAndDestroySeqBean'
执行InitAndDestroySeqBean: init-method
2023-06-26 17:30:22,129 DEBUG support.DefaultListableBeanFactory - Finished creating instance of bean 'initAndDestroySeqBean'
2023-06-26 17:30:22,129 INFO  support.ClassPathXmlApplicationContext - Closing org.springframework.context.support.ClassPathXmlApplicationContext@56a499: display name [org.springframework.context.support.ClassPathXmlApplicationContext@56a499]; startup date [Sun Mar 03 17:11:17 CST 2013]; root of context hierarchy
2023-06-26 17:30:22,129 INFO  support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1292d26: defining beans [org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,initAndDestroySeqBean]; root of factory hierarchy
2023-06-26 17:30:22,129 DEBUG annotation.CommonAnnotationBeanPostProcessor - Invoking destroy method on bean 'initAndDestroySeqBean': public void com.alibaba.chj.spring.InitAndDestroySeqBean.preDestroy()
执行InitAndDestroySeqBean: preDestroy
2023-06-26 17:30:22,145 DEBUG support.DisposableBeanAdapter - Invoking destroy() on bean with name 'initAndDestroySeqBean'
执行InitAndDestroySeqBean: destroy
2023-06-26 17:30:22,145 DEBUG support.DisposableBeanAdapter - Invoking destroy method 'destroyMethod' on bean with name 'initAndDestroySeqBean'
执行InitAndDestroySeqBean: destroy-method
从执行结果可以看出:
  Bean在实例化的过程中:
     Constructor > @PostConstruct >InitializingBean > init-method

Bean在销毁的过程中:
     @PreDestroy > DisposableBean > destroy-method
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/Spring/202306/6904.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者