(默认)单例: <bean id="role" class="..." singleton="true"/> 或者 <bean id="role" class="..." scope="singleton"/>
非单例: <bean id="role" class="..." singleton="false"/> 或者 <bean id="role" class="..." scope="prototype"/>
spring Bean生命周期
Bean的作用域包括:
scope="singleton"(默认),单例,任何人获取该Bean实例的都为同一个实例;等价于 singleton=true;
scope="prototype",任何一个实例都是新的实例;等价于 singleton=false;
scope="request",在WEB应用程序中,每一个实例的作用域都为request范围;
scope="session",在WEB应用程序中,每一个实例的作用域都为session范围;
注意:在默认情况下,Bean实例在被Spring容器初始化的时候,就会被实例化,默认调用无参数的构造方法。
在其它情况下(scope的范围设置...),Bean将会在获取实例的时候才会被实例化。
Bean可以通过init-method指定初始化后执行的方法,通过destroy-method指定销毁时执行的方法。
语法:<bean .... destroy-method="close" init-method="afterPropertiesSet"/>