Spring学习-----Spring @PostConstruct和@PreDestroy实例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring学习-----Spring @PostConstruct和@PreDestroy实例相关的知识,希望对你有一定的参考价值。

实现 初始化方法和销毁方法3种方式:
  1. 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高)
  2. 设置bean属性 Init-method destroy-method
  3. 使用注释配置后,调用@PostConstruct和@PreDestroy注解

注:@PostConstruct和@PreDestroy 标注不属于 Spring,它是在J2EE库- common-annotations.jar。

@PostConstruct 和 @PreDestroy

一个 CustomerService Bean使用 @PostConstruct 和 @PreDestroy 注释
package com.yiibai.customer.services;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class CustomerService
{
    String message;
    
    public String getMessage() {
      return message;
    }

    public void setMessage(String message) {
      this.message = message;
    }
    
    @PostConstruct
    public void initIt() throws Exception {
      System.out.println("Init method after properties are set : " + message);
    }
    
    @PreDestroy
    public void cleanUp() throws Exception {
      System.out.println("Spring Container is destroy! Customer clean up");
    }
    
}

 

默认情况下,Spring不会意识到@PostConstruct和@PreDestroy注解。要启用它,要么注册“CommonAnnotationBeanPostProcessor”,要么在bean配置文件的<context:annotation-config />‘ 指定,

1. CommonAnnotationBeanPostProcessor

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

    <bean id="customerService" class="com.yiibai.customer.services.CustomerService">
        <property name="message" value="i‘m property message" />
    </bean>
        
</beans>

 

2. <context:annotation-config />

<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="customerService" class="com.yiibai.customer.services.CustomerService">
        <property name="message" value="i‘m property message" />
    </bean>
        
</beans>

 

执行结果

package com.yiibai.common;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yiibai.customer.services.CustomerService;

public class App 
{
    public static void main( String[] args )
    {
        ConfigurableApplicationContext context = 
          new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
    
        CustomerService cust = (CustomerService)context.getBean("customerService");
        
        System.out.println(cust);
        
        context.close();
    }
}

输出结果

Init method after properties are set : im property message
[email protected]
...
INFO: Destroying singletons in org.springframework.beans.factory.
[email protected]: 
defining beans [customerService]; root of factory hierarchy
Spring Container is destroy! Customer clean up

 

initIt()方法(@PostConstruct)被调用时,消息属性设置后 cleanUp() 方法(@PreDestroy)是在context.close()执行后被调用;

下载源代码 – http://pan.baidu.com/s/1qX2W6xI

以上是关于Spring学习-----Spring @PostConstruct和@PreDestroy实例的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 尝试访问 Post 请求 URL,但显示 GET 不支持

spring 学习 requestMapping

AngularJS HTTP POST 未找到预期的 CSRF 令牌

发送 post 请求时出现 CORS 错误,但在 Spring Boot 和 Reactjs 中未获取请求

spring学习十二 application/x-www-form-urlencoded还是application/json

Spring mvc-文件上传与JSON-学习笔记