标签:
一 初始化bean
1 使用bean的init-method属性调用相应的方法初始化
<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
public class ExampleBean {
public void init() {
// do some initialization work
}
}
2 使用org.springframework.beans.factory.InitializingBean接口的void afterPropertiesSet() throws Exception;方法初始化
<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
public class AnotherExampleBean implements InitializingBean {
public void afterPropertiesSet() {
// do some initialization work
}
}
二 销毁bean
1 使用bean的destroy-method属性调用相应的方法销毁
<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>
public class ExampleBean {
public void cleanup() {
// do some destruction work (like releasing pooled connections)
}
}
2 使用org.springframework.beans.factory.DisposableBean接口的void destroy() throws Exception;方法销毁
<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
public class AnotherExampleBean implements DisposableBean {
public void destroy() {
// do some destruction work (like releasing pooled connections)
}
}
说明:使用bean的属性调用方法的时候将增加代码的耦合度
标签:
原文地址:http://www.cnblogs.com/JavaTWW/p/4808070.html