zoukankan      html  css  js  c++  java
  • Spring中bean注入前后的一些操作:

    InitializingBean 和 DisposableBean

    init-method 和 destroy-method

    @PostConstruct 和 @PreDestroy

    In Spring, InitializingBean and DisposableBean are two marker interfaces, a useful way for Spring to perform certain actions upon bean initialization and destruction.

    1. For bean implemented InitializingBean, it will run afterPropertiesSet() after all bean properties have been set.
    2. For bean implemented DisposableBean, it will run destroy() after Spring container is released the bean.

    In Spring, you can use init-method and destroy-method as attribute in bean configuration file for bean to perform certain actions upon initialization and destruction.

    Note
    The @PostConstruct and @PreDestroy annotation are not belong to Spring, it’s located in the J2ee library – common-annotations.jar.

    具体的使用

    对于第一个:

    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
     
    public class CustomerService implements InitializingBean, DisposableBean
    {
    	String message;
     
    	public String getMessage() {
    	  return message;
    	}
     
    	public void setMessage(String message) {
    	  this.message = message;
    	}
     
    	public void afterPropertiesSet() throws Exception {
    	  System.out.println("Init method after properties are set : " + message);
    	}
     
    	public void destroy() throws Exception {
    	  System.out.println("Spring Container is destroy! Customer clean up");
    	}
     
    }
    

      下面的例子展示了 init-method and destroy-method.

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

      

    <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 id="customerService" class="com.mkyong.customer.services.CustomerService" 
    		init-method="initIt" destroy-method="cleanUp">
     
    		<property name="message" value="i'm property message" />
    	</bean>
     
    </beans>
    

      第三种的使用:

    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");
    	}
     
    }
    

      By default, Spring will not aware of the @PostConstruct and @PreDestroy annotation. To enable it, you have to either register ‘CommonAnnotationBeanPostProcessor‘ or specify the ‘<context:annotation-config />‘ in bean configuration file,

    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>
    

      

  • 相关阅读:
    HDU1372,BFS象棋马走日
    看完一本小的算法书一个总结吧
    最小生成树Prim
    Junit单元测试的简单使用(主要是在spring框架下的项目)
    并查集
    最新最实用的公式技巧大汇总!
    这款Office密码破解工具,无坚不摧!
    有了它,友谊的船说不翻就不翻!
    Word公式装逼技巧,你绝对不会!
    为什么MathType窗口变灰色
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2835289.html
Copyright © 2011-2022 走看看