标签:上下文 tty class 方法 示例 执行 cleanup port row
package com.yiibai.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.yiibai.customer.services.CustomerService" init-method="initIt" destroy-method="cleanUp"> <property name="message" value="i‘m property message" /> </bean> </beans>
执行下面的程序代码:
package com.yiibai.common; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yiibai.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.yiibai.customer.services.CustomerService@5f49d886 Spring Container is destroy! Customer clean up
Spring Bean init-method 和 destroy-method实例
标签:上下文 tty class 方法 示例 执行 cleanup port row
原文地址:http://www.cnblogs.com/soundcode/p/6367412.html