spring bean 生命周期

1 singleton

spring 默认的模式,下面两种方式等价

<bean id="accountService" class="com.something.DefaultAccountService"/>

 <bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>

每个spring容器只有一个实例

2prototype

每一个请求就容器就会创造一个实例

注意: Thus, although initialization lifecycle callback methods are called on all objects regardless of scope, in the case of prototypes, configured destruction lifecycle callbacks are not called.

bean的初始化方法会被调用,但是销毁时的disposal方法不会被调用,

  

. Singleton Beans with Prototype-bean Dependencies

即使一个prototype的bean被注入到单例的bean中他依然是prototype的

 

org.springframework.beans.factory.DisposableBean

destroy方法

实现该接口后

在关闭容器时会自动调用(需要调用context.close())

 

org.springframework.beans.factory.InitializingBean

void afterPropertiesSet() throws Exception;

该方法会在创建bean,并且bean的属性被设置之后调用

 

posted @ 2021-05-13 22:22  codeNothing  阅读(43)  评论(0)    收藏  举报