Spring Bean Scopejava
简单的解释一下bean 的scopespring
当一个bean的做用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,而且全部对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。换言之,当把一个bean定义设置为singleton做用域时,Spring IOC容器只会建立该bean定义的惟一实例。这个单一实例会被存储到单例缓存(singleton cache)中,而且全部针对该bean的后续请求和引用都将返回被缓存的对象实例缓存
prototype做用域部署的bean,每一次请求(将其注入到另外一个bean中,或者以程序的方式调用容器的getBean()方法)都会产生一个新的bean实例,至关与一个new的操做,对于prototype做用域的bean,有一点很是重要,那就是Spring不能对一个prototype bean的整个生命周期负责,容器在初始化、配置、装饰或者是装配完一个prototype实例后,将它交给客户端,随后就对该prototype实例漠不关心了。无论何种做用域,容器都会调用全部对象的初始化生命周期回调方法,而对prototype而言,任何配置好的析构生命周期回调方法都将不会被调用。清除prototype做用域的对象并释听任何prototype bean所持有的昂贵资源,都是客户端代码的职责。(让Spring容器释放被singleton做用域bean占用资源的一种可行方式是,经过使用bean的后置处理器,该处理器持有要被清除的bean的引用。)app
以上引用:http://blog.csdn.net/mastermind/article/details/1932787测试
咱们这里来作测试,以下配置文件:spa
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="user" class="com.lyx.batch3.User"> <property name="name" value="lyx" /> <property name="age" value="12" /> <property name="role" ref="role" /> </bean> <bean id="role" class="com.lyx.batch3.Role"> <property name="roleName" value="admin" /> <property name="description" value="desc" /> </bean> </beans>
这里没有指定 bean scope,singleton即是容器默认的scope。.net
启动spring app,以下:TestBeanScope.javaprototype
package com.lyx.batch; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lyx.batch3.Role; import com.lyx.batch3.User; /** * 测试bean 的scope * * @author Lenovo * */ public class TestBeanScope { public static void main(String args[]) { @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "classpath:spring-bean-scope.xml" }); User user = (User) context.getBean("user"); System.out.println("user hashcode=" + user.hashCode()); System.out.println("role hashcode=" + user.getRole().hashCode()); Role role = (Role) context.getBean("role"); System.out.println("direct role hashcode=" + role.hashCode()); } }
看打印结果:code
user hashcode=2025864991xml
role hashcode=1589683045
direct role hashcode=1589683045
能够看到从user 中引用的bean 和 从spring ioc 容器直接获得的bean 的hashcode 是同样的,说明默认就是singleton。
那么更改bean的scope 为prototype如何呢?以下配置文件:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="user" class="com.lyx.batch3.User"> <property name="name" value="lyx" /> <property name="age" value="12" /> <property name="role" ref="role" /> </bean> <bean id="role" class="com.lyx.batch3.Role" scope="prototype"> <property name="roleName" value="admin" /> <property name="description" value="desc" /> </bean> </beans>
测试的打印结果:
user hashcode=2025864991
role hashcode=1589683045
direct role hashcode=1340328248
role hashcode=1589683045 和 direct role hashcode=1340328248 的hashcode 值是不同的,那么说明这个 prototype 的做用。
===================END===================