Spring 注入bean時的初始化和銷燬操做

實現bean的初始化和銷燬操做有三種方法java

一、採用@PostConstruct和 @PreDestroy註解實現</a> 二、實現InitializingBean和DisposableBean接口 三、注入bean時設置init-method和destroy-method指定相應方法web

一、採用@PostConstruct和 @PreDestroy註解實現spring

定義實現類express

/**
 * 
 */
package com.zhu.example.PreExcute;

import javax.annotation.PostConstruct;  
import javax.annotation.PreDestroy;  
/**
 * @author zhukai
 *
 */
public class BeanOperation {

	@PostConstruct
    public void  init(){  
        System.out.println("init  BeanOperation by @PostConstrut");  
    }  

    @PreDestroy  
    public void  destory(){  
        System.out.println("destory BeanOperation  by  @PreDestroy");  
    }  
}

Spring配置文件spring-mvc

<?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:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
	xmlns:mongo="http://www.springframework.org/schema/data/mongo" 
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
	    http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd"
        default-lazy-init="true">

	<description>Spring公共配置 </description>

		<context:component-scan base-package="com.zhu.example">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
	</context:component-scan> 

	<!-- pre excute test -->
	<bean id ="AnnotationMethod" class="com.zhu.example.PreExcute.BeanOperation"  lazy-init="false"/> 

</beans>

二、實現InitializingBean和DisposableBean接口mvc

定義實現類app

/**
 * 
 */
package com.zhu.example.PreExcute;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

/**
 * @author zhukai
 *
 */
public class BeanItfInit implements InitializingBean,DisposableBean{

	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.DisposableBean#destroy()
	 */
	public void destroy() throws Exception {
		System.out.println("destory BeanItfInit  by  DisposableBean"); 
	}

	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	public void afterPropertiesSet() throws Exception {
		System.out.println("init  BeanItfInit by InitializingBean");  

	}

}

Spring配置文件測試

<bean id ="ItfMethod" class="com.zhu.example.PreExcute.BeanItfInit"  lazy-init="false"/>

三、注入bean時設置init-method和destroy-method指定相應方法url

定義實現類spa

/**
 * 

Title: BeanXmlinit.java

 * 

Description: 

 * @author zhukai
 * @date 2016年6月12日
 */
package com.zhu.example.PreExcute;

/**
 * 

Title: BeanXmlinit

 * 

Description: 

 * @author zhukai
 */
public class BeanXmlinit {

    public void init(){  
    	 System.out.println("init  BeanXmlinit by xml");  
    }  
    //  how  validate the  destory method is  a question  
    public void  destory(){  
    	System.out.println("destory BeanXmlinit  by  xml");    
    }  

}

Spring配置文件

<bean id ="XmlMethod" class="com.zhu.example.PreExcute.BeanXmlinit"  lazy-init="false" init-method="init"  destroy-method="destory"/>

初始化效果

六月 12, 2016 4:44:40 下午 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
init  BeanOperation by @PostConstrut
init  BeanItfInit by InitializingBean
init  BeanXmlinit by xml
六月 12, 2016 4:44:40 下午 org.springframework.web.servlet.DispatcherServlet initServletBean
信息: FrameworkServlet 'springServlet': initialization completed in 339 ms
2016-06-12 16:44:40.814:WARN:oejsh.RequestLogHandler:!RequestLog
2016-06-12 16:44:40.830:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server

其餘問題

在測試過程當中還出現過一些問題

@PostConstruct初始化不起做用

這個是Spring配置文件中 default-lazy-init="true"這個屬性引發的,這個屬性使得bean沒有使用時就不會加載,天然就不會進行初始化。解決方法是將該類改成false或者在bean注入時加入屬性,lazy-init="false"。

初始化進行兩次

這個問題是由於ContextLoaderListener與DispatcherServlet都會對Spring的配置進行解析形成bean重複初始化,能夠使用兩個配置文件,只在其中一個文件中進行bean的聲明 ContextLoaderListener用配置文件

<!--prevent Repeated init bean   -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
	  /WEB-INF/spring-mvc.xml
	</param-value>
  </context-param>
<?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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

</beans>

DispatcherServlet用配置文件

<servlet>
    <servlet-name>springServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
<?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:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
	xmlns:mongo="http://www.springframework.org/schema/data/mongo" 
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
	    http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd"
        default-lazy-init="true">

	<description>Spring公共配置 </description>

		<context:component-scan base-package="com.zhu.example">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
	</context:component-scan> 

	<!-- pre excute test -->
	<bean id ="AnnotationMethod" class="com.zhu.example.PreExcute.BeanOperation"  lazy-init="false"/> 

	<bean id ="ItfMethod" class="com.zhu.example.PreExcute.BeanItfInit"  lazy-init="false"/> 

	<bean id ="XmlMethod" class="com.zhu.example.PreExcute.BeanXmlinit"  lazy-init="false" init-method="init"  destroy-method="destory"/> 

</beans>
相關文章
相關標籤/搜索