Spring入门篇 学习笔记
定义 --> 初始化 --> 使用 --> 销毁
实现 org.springframework.beans.factory.InitializingBean 接口,覆盖 afterPropertiesSet 方法
public class ExampleBean implements InitializingBean{
public void afterPropertiesSet() throws Exception{
// do some initialization work
}
}
配置 init-method:
<bean id="exampleInitBean" class="example.ExampleBean" init-method="start"/>
public class ExampleBean{
public void start(){
// do some initialization work
}
}
实现 org.springframework.beans.factory.DisposableBean 接口,覆盖 destory 方法
public class ExampleBean implements DisposableBean{
public void destory() throws Exception{
// do some destruction work
}
}
配置 destory-method
<bean id="exampleInitBean" class="example.ExampleBean" destory-method="stop"/>
public class ExampleBean{
public void stop(){
// do some destruction work
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd"
default-init-method="init" default-destory-method="destory">
</beans>
原文:https://www.cnblogs.com/victorbu/p/10419153.html