Spring

Spring - @PostConstruct and @Pre

2020-01-15  本文已影响0人  HRocky

原文地址:https://mkyong.com/spring/spring-postconstruct-and-predestroy-example/

在Spring中,您可以实现InitializingBean和DisposableBean接口,或者在bean配置文件中为初始化和销毁回调函数指定init-method和destroy-method。在本文中,我们将向您展示如何使用基于注解的@PostConstruct和@PreDestroy来完成相同的工作。

@PostConstruct and @PreDestroy

下面是有@PostConstruct和@PreDestroy注解的CustomerService类:

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注释。要启用它,您必须在bean配置文件中注册“CommonAnnotationBeanPostProcessor”或指定“<context:annotationconfig />”,

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.mkyong.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.mkyong.customer.services.CustomerService">
        <property name="message" value="i'm property message" />
    </bean>
        
</beans>

运行上面的例子:

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

import com.mkyong.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
com.mkyong.customer.services.CustomerService@47393f
...
INFO: Destroying singletons in org.springframework.beans.factory.
support.DefaultListableBeanFactory@77158a: 
defining beans [customerService]; root of factory hierarchy
Spring Container is destroy! Customer clean up

在设置消息属性之后调用initIt()方法(@PostConstruct),在context.close()之后调用cleanUp()方法(@PreDestroy)。

上一篇下一篇

猜你喜欢

热点阅读