SpringAnnotation 中的Scope参数

在Spring中,Bean的Scope参数值用于决定访问者的Bean示例应该以哪种方式返回Spring容器调用方法

Bean的Scope参数支持五种类型

  1. singleton--按照Spring的IOC容器返回一个单Bean实例
  2. prototype--每当请求的时候返回一个新的Bean实例
  3. request--按照HTTP Request返回一个单一的Bean实例
  4. session--按照HTTP Session返回一个单一的Bean实例
  5. globalSession--按照Global HTTP Session 返回一个单一的Bean实例

在大多数情况下 ,只需要处理Spring的核心的Scpoe singleton(单例模式)和prototype(原型模式)

Singleton和prototype的区别

 

package com.mkyong.customer.services;
 
public class CustomerService 
{
	String message;
 
	public String getMessage() {
		return message;
	}
 
	public void setMessage(String message) {
		this.message = message;
	}
}


如果不在Scop中指定范围默认的是singleton

<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.mkyong.customer.services.CustomerService" />
 
</beans>

测试

package com.mkyong.common;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.mkyong.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'是Singleton的范围,第二检索通过'custB'将显示所设置的消息'custA'还有,甚至getBean新方法检索。 lleton,仅为单个实例SpringIoC容器,无论多少时间你检索getBean,它将始终返回相同的实例。


Prototype

如果你想要一个新的'customerService'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.mkyong.customer.services.CustomerService" 
         scope="prototype"/>
 
</beans>


输出

Message : Message by custA
Message : null

在原型范围,您必须为每个getBean一个新实例调用的方法。

 

Annotation定义scop参数

package com.mkyong.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.mkyong.customer" />
 
</beans



 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值