我們可以使用Spring InitializingBean和DisposableBean以對(duì)bean初始化和銷毀執(zhí)行操作。
對(duì)于來自Spring的Bean實(shí)現(xiàn)的InitializingBean,Spring IoC容器將運(yùn)行 afterPropertiesSet()
方法所有的bean屬性都已設(shè)置。
對(duì)于來自Spring的Bean實(shí)現(xiàn)的DisposableBean,它會(huì)調(diào)用destroy()在Spring容器釋放bean之后。
這里是一個(gè)例子,顯示如何使用InitializingBean和DisposableBean。
在下面的代碼中定義了一個(gè)PrinterHelper bean,它實(shí)現(xiàn)了兩者InitializingBean和DisposableBean接口。
它還有兩個(gè)方法一個(gè)是 afterPropertiesSet()
方法和另一個(gè)是 destroy()
方法。
package com.www.o2fo.common; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class PrinterHelper 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! JavaBean clean up"); } @Override public String toString() { return message ; } }
這里是Spring-Customer.xml for Java bean配置。
<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="printerService" class="com.www.o2fo.common.PrinterHelper"> <property name="message" value="i"m property message" /> </bean> </beans>
這里是代碼顯示如何運(yùn)行上面的代碼。
ConfigurableApplicationContext.close()調(diào)用將關(guān)閉應(yīng)用程序上下文,它會(huì)調(diào)用destroy()方法。
package com.www.o2fo.common; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"}); PrinterHelper cust = (PrinterHelper)context.getBean("printerService"); System.out.println(cust); context.close(); } }
上面的代碼生成以下結(jié)果。
從輸出我們可以看到 afterPropertiesSet()
方法被調(diào)用,之后的message屬性設(shè)置和 destroy()
方法是在context.close()之后調(diào)用的;
下面的代碼顯示了調(diào)用自定義方法的另一種方法在Java Bean中初始化和銷毀Java Bean時(shí)。
我們可以使用init-method和destroy-method屬性在bean配置文件中標(biāo)記init方法和destroy方法在Java Bean中。
在初始化期間調(diào)用init-methoddestroy方法在銷毀期間被調(diào)用。
下面的代碼顯示了如何在Java Bean中添加方法來分別做init和destroy。
package com.www.o2fo.common; public class PrinterHelper { 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"); } @Override public String toString() { return message; } }
在下面的xml配置文件中使用 init-method
和 destroy-method
屬性進(jìn)行標(biāo)記init方法和destroy方法。
<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="printerService" class="com.www.o2fo.common.PrinterHelper" init-method="initIt" destroy-method="cleanUp"> <property name="message" value="i"m property message" /> </bean> </beans>
Spring還支持標(biāo)記為Java Bean的@PostConstruct和@PreDestroy注釋。
我們使用這兩個(gè)注釋來標(biāo)記init-method和destroy-method。
@PostConstruct和@PreDestroy注釋來自J2ee common-annotations.jar。
在下面的代碼中,我們使用@PostConstruct和@PreDestroy注釋以標(biāo)記PrinterHelper類。
package com.java2s.customer.services; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class PrinterHelper { 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"); } }
在下面的代碼中,我們使用@PostConstruct和@PreDestroy注釋以標(biāo)記PrinterHelper類。...
以下xml配置文件調(diào)用CommonAnnotationBeanPostProcessorJava Bean。
<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="printerService" class="com.www.o2fo.common.PrinterHelper"> <property name="message" value="i"m property message" /> </bean> </beans>
以下xml配置文件調(diào)用CommonAnnotationBeanPostProcessorJava Bean。...
<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="printerService" class="com.www.o2fo.common.PrinterHelper"> <property name="message" value="i"m property message" /> </bean> </beans>
更多建議: