Spring Bean Scope

Spring Bean Scopejava

簡單的解釋一下bean 的scopespring

singleton

當一個bean的做用域設置爲singleton, 那麼Spring IOC容器中只會存在一個共享的bean實例,而且全部對bean的請求,只要id與該bean定義相匹配,則只會返回bean的同一實例。換言之,當把一個bean定義設置爲singleton做用域時,Spring IOC容器只會建立該bean定義的惟一實例。這個單一實例會被存儲到單例緩存(singleton cache)中,而且全部針對該bean的後續請求和引用都將返回被緩存的對象實例緩存

 

prototype

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===================

相關文章
相關標籤/搜索