Spring学习(七)-----Spring Bean的5种作用域
- 单例(singleton) - 每个Spring IoC 容器返回一个bean实例(默认)
- 原型(prototype)- 当每次请求时返回一个新的bean实例
- 请求(request) - 返回每个HTTP请求的一个Bean实例
- 会话(session) - 返回每个HTTP会话的一个bean实例
- 全局会话(globalSession)- 返回全局HTTP会话的一个bean实例
package com.yiibai.customer.services; 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerService" class="com.yiibai.customer.services.CustomerService" /> </beans>
执行结果:
package com.yiibai.common; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yiibai.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()方法获取它,它总是返回同一个实例。
<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.yiibai.customer.services.CustomerService" scope="prototype"/>
</beans>
运行-执行
Message : Message by custA
Message : null
package com.yiibai.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.yiibai.customer" /> </beans>
request作用域:
表示该针对每一次HTTP请求都会产生一个新的bean,仅适用于WebApplicationContext环境。
<bean id="bean的id" class="bean的包名.类名" scope="request"/>
Spring1以上提供
session作用域:
表示该针对每一次HTTP请求都会产生一个新的bean,仅适用于WebApplicationContext环境。
<bean id="bean的id" class="bean的包名.类名" scope="session"/>
Spring1以上提供
globalSession作用域:
它仅仅在基于portlet的web应用中才有意义。Portlet规范定义了全局Session的概念,它被所有构成某个portlet
web应用的各种不同的portlet所共享。在global session作用域中定义的bean被限定于全局portlet
Session的生命周期范围内。如果你在web中使用global
session作用域来标识bean,那么,web会自动当成session类型来使用。
<bean id="bean的id" class="bean的包名.类名" scope="globalSession"/>
Spring1以上提供
Web环境作用域的特殊配置:
使用request作用域、request作用域、globalSession作用域还需要进行额外的配置
在低版本的Web容器中(Servlet2.3以前),需要使用过滤器进行配置
在高版本的Web容器中,可以使用监听器进行配置
<web-app>
...
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>
混合使用作用域的问题:
a Bean是Request作用域,它要被一个singleton作用域的b Bean使用(被注入)。还要使用<aop:sclped-proxy/>配置。
<beans xmlns="..." ... xmlns:aop="....." 要引入aop命名空间 > <bean id="a" class="A" scope="request"> <aop:sclped-proxy/> </bean> <bean id="b" class="B" scope="singleton"> <property name="a" ref="a"/> </bean>
最新文章
- MS SQL执行大脚本文件时,提示“内存不足”的解决办法()
- .NET Core Web 应用部署到 Docker 中运行
- Word Excel 操作总结
- 分享Kali Linux 2016.2第48周镜像文件
- 如何用 Parse 和 Swift 搭建一个像 Instagram 那样的应用?(2)
- Apache加载PHP.ini顺序
- 北大ACM(POJ1005-I Think I Need a Houseboat)
- https://lua-toolbox.com/
- [Twisted] 部署Twisted
- Jquery JSOPN在WebApi中的问题
- gridview列显示,截取其中前面的几个字显示出来,当鼠标放上去的时候显示全部——使用LinkButton的方法
- Djanto static静态文件配置
- MongoDB学习总结(三) —— 常用聚合函数
- Ubuntu 16.04 安装wine QQ
- H5分享功能
- 申请Let&#39;s Encrypt永久免费SSL证书
- 吴裕雄 python 机器学习——岭回归
- vs 调试 iis中的网站
- 关于 extern ";C";的说明
- Bootstrap学习目录