Spring v3.0.2 Learning Note 4 - Scope of Beans
来源:互联网 发布:游戏王巅峰知乎 编辑:程序博客网 时间:2024/05/18 02:37
Scope of Spring Beans
- singleton
在每个Spring IOC容器中一个Bean有且仅有一个对象实例。
默认情况下,在容器启动时初始化Bean。可以指定bean节点的lazy-init="true"来延迟初始化bean,此时只有在第一次获取Bean才会初始化bean。 如:
<bean id="myid" class="xxx" lazy-init="true"/>
如果想对所有Bean都应用延迟初始化,可以在根节点beans设置default-lazy-init="true",如:
<beans default-lazy-init="true" ...>
- prototype
每次从容器获取的bean都是新的对象。
- request
- session
- global session
后面的3个scope类型应用于web环境。
简单验证一下singleton和prototype作用域。借用前面的例子,
<bean id="personService" class="com.spring.test.manager.impl.PersonManager" />
不特别指定scope,则默认情况下scope="singleton"
package com.spring.test.junit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.test.manager.IPersonService;
public class PersonServiceTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Test
public void test() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring.xml");
IPersonService personService = (IPersonService) ctx
.getBean("personService");
IPersonService personService2 = (IPersonService) ctx
.getBean("personService");
System.out.println(personService == personService2);
}
}
运行此junit test class,控制台打印true,证明即便多次获取同一个bean,这些bean仍指向内存中同一地址,为同一对象实例。
现在,将上面的bean的作用域修改为prototype。
<bean id="personService" class="com.spring.test.manager.impl.PersonManager" scope="prototype"/>
再次运行上面的junit test class,控制台返回false,证明多次获取同一个bean,容器会生成新的对象实例返回。
- Spring v3.0.2 Learning Note 4 - Scope of Beans
- Spring v3.0.2 Learning Note 3 - Initialisation of Beans
- Spring v3.0.2 Learning Note 5 - Lifecycle of Beans
- Spring v3.0.2 Learning Note 1 - Introduction to Spring Framework
- Spring v3.0.2 Learning Note 14 - Send Email with Spring
- Spring v3.0.2 Learning Note 2 - A Sample Example
- Spring v3.0.2 Learning Note 6 - Inject Components
- Spring v3.0.2 Learning Note 7 - Constructor-based dependency injection
- Spring v3.0.2 Learning Note 8 - Annotation-based dependency injection
- Spring v3.0.2 Learning Note 9 - Classpath Scanning
- Spring v3.0.2 Learning Note 10 - Annotation-based Transaction Management
- Spring v3.0.2 Learning Note 11 - XML-based Transaction Management
- Spring v3.0.2 Learning Note 12 - Integrate with Hiberate
- Spring v3.0.2 Learning Note 13 - AOP Example
- Spring v3.0.2 Learning Note 16 - Integrate With Hessian
- Spring v3.0.2 Learning Note 15 - Use Velocity Email Template with Spring
- Spring v3.0.2 Learning Note 16 (Cont.)- Resolve NullPointerException on weblogic v10.3
- The NOTE of learning ASP.NET [4]
- Select sql的效率
- Spring v3.0.2 Learning Note 2 - A Sample Example
- 日语学习
- Spring v3.0.2 Learning Note 3 - Initialisation of Beans
- 移动硬盘出现乱码文件夹的解决方法
- Spring v3.0.2 Learning Note 4 - Scope of Beans
- 访问资源文件方式与访问工作目录
- 在SM中4种调试的方法
- Spring v3.0.2 Learning Note 5 - Lifecycle of Beans
- MyEclipse快捷键全
- Oracle 连接
- Spring v3.0.2 Learning Note 6 - Inject Components
- Reporting Service参数化报表
- 为谁写代码