package com.h3.customer.services; 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"); } }
file : applicationcontext.xml, 在bean中定义了init-method和destroy-method属性。
<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" init-method="initit" destroy-method="cleanup"> <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@5f49d886 spring container is destroy! customer clean up