在Spring中,Bean的Scope参数值用于决定访问者的Bean示例应该以哪种方式返回Spring容器调用方法
Bean的Scope参数支持五种类型
- singleton--按照Spring的IOC容器返回一个单Bean实例
- prototype--每当请求的时候返回一个新的Bean实例
- request--按照HTTP Request返回一个单一的Bean实例
- session--按照HTTP Session返回一个单一的Bean实例
- 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