package com.h3.customer.services; 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"); } }
file : applicationcontext.xml
<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.h3.customer.services.customerservice"> <property name="message" value="i'm property message" /> </bean> </beans>
执行它
package com.h3.common; import org.springframework.context.configurableapplicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import com.h3.customer.services.customerservice; public class app { public static void main( string[] args ) { configurableapplicationcontext context = new classpathxmlapplicationcontext(new string[] {"applicationcontext.xml"}); customerservice cust = (customerservice)context.getbean("customerservice"); system.out.println(cust); context.close(); } }
输出结果
init method after properties are set : i'm property message com.h3.customer.services.customerservice@4090c06f spring container is destroy! customer clean up