Spring 专题
专题目录
您的位置:java > Spring专题 > Spring Bean作用域实例
Spring Bean作用域实例
作者:--    发布时间:2019-11-20
在spring中,bean作用域用于确定哪种类型的 bean 实例应该从spring容器中返回给调用者。bean支持的5种范围域:
  1. 单例 - 每个spring ioc 容器返回一个bean实例
  2. 原型- 当每次请求时返回一个新的bean实例
  3. 请求 - 返回每个http请求的一个bean实例
  4. 会话 - 返回每个http会话的一个bean实例
  5. 全局会话- 返回全局http会话的一个bean实例
在大多数情况下,可能只处理了 spring 的核心作用域 - 单例和原型,默认作用域是单例。
注:意味着只有在一个基于web的spring applicationcontext情形下有效!

单例vs原型

这里有一个例子来说明,bean的作用域单例和原型之间的不同:
package com.h3.customer.services;

public class customerservice 
{
	string message;
	
	public string getmessage() {
		return message;
	}

	public void setmessage(string message) {
		this.message = message;
	}
}

1.单例例子

如果 bean 配置文件中没有指定 bean 的范围,默认为单例。
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
	xsi:schemalocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

       <bean id="customerservice" 
            class="com.h3.customer.services.customerservice" />
		
</beans>

执行结果:

package com.h3.common;

import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;

import com.h3.customer.services.customerservice;

public class app 
{
    public static void main( string[] args )
    {
    	applicationcontext context = 
    	 new classpathxmlapplicationcontext(new string[] {"spring-customer.xml"});

    	customerservice custa = (customerservice)context.getbean("customerservice");
    	custa.setmessage("message by custa");
    	system.out.println("message : " + custa.getmessage());
    	
    	//retrieve it again
    	customerservice custb = (customerservice)context.getbean("customerservice");
    	system.out.println("message : " + custb.getmessage());
    }
}

输出结果

message : message by custa
message : message by custa 

由于 bean 的 “customerservice' 是单例作用域,第二个通过提取”custb“将显示消息由 ”custa' 设置,即使它是由一个新的 getbean()方法来提取。在单例中,每个spring ioc容器只有一个实例,无论多少次调用 getbean()方法获取它,它总是返回同一个实例。

2.原型例子

如果想有一个新的 “customerservice”bean 实例,每次调用它的时候,需要使用原型(prototype)来代替。
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
	xsi:schemalocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

   <bean id="customerservice" class="com.h3.customer.services.customerservice" 
         scope="prototype"/>
		
</beans>

运行-执行

message : message by custa
message : null
在原型作用域,必须为每个 getbean()方法中调用返回一个新的实例。

3. bean作用域注释

还可以使用注释来定义 bean 的作用域。
package com.h3.customer.services;

import org.springframework.context.annotation.scope;
import org.springframework.stereotype.service;

@service
@scope("prototype")
public class customerservice 
{
	string message;
	
	public string getmessage() {
		return message;
	}

	public void setmessage(string message) {
		this.message = message;
	}
}
启用自动组件扫描
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemalocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">

       <context:component-scan base-package="com.h3.customer" />
		
</beans>

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册