spring容器在初始化Bean时前和后的操作

Posted Bwz_Learning

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring容器在初始化Bean时前和后的操作相关的知识,希望对你有一定的参考价值。

在某些情况下,Spring容器在初始化Bean的时候,希望在初始化bean前和销毁bean前进行一些资源的加载和释放的操作。可以通过一下三种方式完成。

  • Bean的方法加上@PostConstruct和@PreDestroy注解
  • 在xml中定义init-method和destory-method方法
  • Bean实现InitializingBean和DisposableBean接口

@PostConstruct和@PreDestroy注解

JavaBean代码

@Component
public class PersonService 
    private String message;

    public String getMessage() 
        return message;
    

    public void setMessage(String message) 
        this.message = message;
    

    @PostConstruct
    public void init() 
        System.out.println("PersonService.class init method ...");
    

    @PreDestroy
    public void cleanUp() 
        System.out.println("PersonService.class cleanUp method ...");
    

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"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd        
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

    <!-- spring扫描的路径 -->
    <context:component-scan base-package="spring.zhujie" />

</beans>

测试代码和结果

  • 测试代码
public static void main(String[] args) 
     AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-zhujie.xml");
     context.registerShutdownHook();
  • 运行结果
PersonService.class init method ...
PersonService.class cleanUp method ...

在XML中定义init-method和destory-method方法

JavaBean代码

public class PersonService 
    private String message;

    public String getMessage() 
        return message;
    

    public void setMessage(String message) 
        this.message = message;
    

    public void init() 
        System.out.println("PersonService.class init method ...");
    

    public void cleanUp() 
        System.out.println("PersonService.class cleanUp method ...");
    

spring配置文件

<bean class="spring.zhujie.PersonService" init-method="init" destroy-method="cleanUp"/>

测试代码和结果

  • 测试代码
public static void main(String[] args) 
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-xml.xml");
        context.registerShutdownHook();
  • 运行结果
PersonService.class init method ...
六月 23, 2017 9:42:06 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7a94c5e7: startup date [Fri Jun 23 21:42:06 CST 2017]; root of context hierarchy
PersonService.class cleanUp method ...

Bean实现InitializingBean和DisposableBean接口

JavaBean代码

public class PersonService implements InitializingBean, DisposableBean 
    private String message;

    public String getMessage() 
        return message;
    

    public void setMessage(String message) 
        this.message = message;
    

    @Override
    public void afterPropertiesSet() throws Exception 
        System.out.println("PersonService.class init method ...");
    

    @Override
    public void destroy() throws Exception 
        System.out.println("PersonService.class cleanUp method ...");
    

spring配置文件

<bean id="personService" class="spring.zhujie.PersonService" />

测试代码和结果

  • 测试代码
public static void main(String[] args) 
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-interface.xml");
        context.registerShutdownHook();
    
  • 运行结果
PersonService.class init method ...
PersonService.class cleanUp method ...

以上是关于spring容器在初始化Bean时前和后的操作的主要内容,如果未能解决你的问题,请参考以下文章

Spring核心技术——Spring中Bean的生命周期

在spring容器中定义初始化和销毁bean前所做的操作,有三种方式

spring 容器初始化 bean 和销毁前所做的操作

Spring Bean 的生命周期

Spring Bean 的生命周期

玩转Spring生命周期之Lifecycle