14 Spring @PostConstruct和@PreDe

作者: 笑Skr人啊 | 来源:发表于2017-07-17 17:47 被阅读294次

在Spring中,实现 InitializingBean和DisposableBean接口或在bean配置文件中指定 init-method 和 destroy-method 都可以初始化和销毁回调函数。在这篇文章中,我们将介绍如何使用 @PostConstruct 和 @PreDestroy 注解来做同样的事情。

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

@PostConstruct 和 @PreDestroy

一个Bean使用 @PostConstruct 和 @PreDestroy 注释

package com.gp6.PostConstructAndPreDestroy;

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

public class PostConstructAndPreDestroy {
    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"
    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">


    <!-- 第一种启用方法 -->
    <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
    
    <bean id="postConstructAndPreDestroy" class="com.gp6.PostConstructAndPreDestroy.PostConstructAndPreDestroy">
        <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="postConstructAndPreDestroy" class="com.gp6.PostConstructAndPreDestroy.PostConstructAndPreDestroy">
        <property name="message" value="i'm property message" />
    </bean>
        
</beans>
测试文件
package com.gp6.PostConstructAndPreDestroy;

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

public class Test {
     public static void main( String[] args ){
            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"com/gp6/PostConstructAndPreDestroy/PostConstructAndPreDestroy.xml"});
        
            PostConstructAndPreDestroy cust = (PostConstructAndPreDestroy)context.getBean("postConstructAndPreDestroy");
            
            System.out.println(cust);
            
            context.close();
        }
}

输出结果
Init method after properties are set : i'm property message
com.gp6.PostConstructAndPreDestroy.PostConstructAndPreDestroy@5a02c35e
Spring Container is destroy! Customer clean up

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

相关文章

网友评论

  • JacksonWen:不知道楼主还在更新吗,我想问一下:我要怎么做才能使用javax.animation包下的注解.我现在用不了

本文标题:14 Spring @PostConstruct和@PreDe

本文链接:https://www.haomeiwen.com/subject/iqrqkxtx.html