Bean 生命周期:定义,初始化,使用,销毁
Bean 初始化:
方法1.实现org.springframework.beans.foctory.InitializingBean接口,覆盖afterPropertiesSet方法。系统会自动查找afterPropertiesSet方法,执行其中的初始化操作
public class ExampleInitializingBean implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
>
<bean id="exampleInitMethodBean" class="com.xiang.bean.ExampleInitMethodBean"></bean>
</beans>
方法2.配置Bean时配置init-method属性
例如设置bean中init-method=”init”那么在初始化过程中就会调用相应class指定类的init()方法进行初始化工作
public class ExampleInitMethodBean {
public void init(){
System.out.println("Init-method");
}
}
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
>
<bean id="exampleInitMethodBean" class="com.xiang.bean.ExampleInitMethodBean" init-method="init"></bean>
</beans>
若两者同时使用:则将优先执行InitializingBean接口的afterPropertiesSet重写方法,其次执行init-method指定所需执行方法
Bean 销毁:
方法1.实现org.springframework.beans.foctory.DisposableBean接口,覆盖destory方法。
public class ExampleDisposableBean implements DisposableBean {
@Override
public void destroy() throws Exception {
System.out.println("DisposableBean");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
>
<bean id="exampleDisposableBean" class="com.xiang.bean.ExampleDisposableBean"></bean>
</beans>
方法2.配置Bean时配置destory-method属性
public class ExampleDestroyMethodBean {
public void destroy(){
System.out.println("Destroy-method");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
>
<bean id="exampleDestroyMethodBean" class="com.xiang.bean.ExampleDestroyMethodBean" destroy-method="destroy"></bean>
</beans>
若两者同时使用:则将优先执行DisposableBean接口destory重写方法,其次执行destory-method指定所需执行方法
Bean 默认全局初始化 销毁
在spring beans标签配置default-init-method和default-destroy-method属性,分别指定初始化和销毁所执行方法
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
default-init-method="init" default-destroy-method="destroy">
</beans>
注意:若该Bean中没有默认全局指定的初始化和销毁所需执行的方法,并不影响Bean的实例化
即:有该方法则,执行该方法,没有就不执行
一旦Bean设置了init-method和estroy-method则default-init-method和default-destroy-method则不起作用,两者无法共存。类似于隐式构造器和显式构造器的关系
若Bean实现org.springframework.beans.foctory.InitializingBean接口,覆盖afterPropertiesSet方法。同时设置了全局默认初始化,则将优先执行DisposableBean接口destory重写方法,其次执行default-init-method所需执行方法
同理若Bean实现org.springframework.beans.foctory.DisposableBean接口,覆盖destory方法。同时设置了默认全局销毁方法,则将优先执行DisposableBean接口destory重写方法,其次执行destory-method指定所需执行方法
以上针对的都是Singleton作用域,一旦Bean的作用域是Prototype,则并不会执行任何销毁方法。
对于prototype作用域的bean,有一点非常重要,那就是Spring不能对一个prototype bean的整个生命周期负责:容器在初始化、配置、装饰或者是装配完一个prototype实例后,将它交给客户端,随后就对该prototype实例不闻不问了。不管何种作用域,容器都会调用所有对象的初始化生命周期回调方法,而对prototype而言,任何配置好的析构生命周期回调方法都将不会被调用。清除prototype作用域的对象并释放任何prototype bean所持有的昂贵资源,都是客户端代码的职责