SPRING中BEAN的作用域?
2011-02-17 16:30
477 查看
文章分类:Java编程
Spring
Bean
作用域介绍:
singleton
:
Spring
Ioc
容器只会创建该
Bean
的唯一实例,所有的请求和引用都只使用这个实例
Property:
每次请求都创建一个新实例
request:
在一次
Http
请求中,容器会返回该
Bean
的同一个实例,而对于不同的用户请求,会返回不同的实例。需要注意的是,该作用域仅在基于
Web
的
Spring
ApplicationContext
情形下有效,以下的
session
和
global
Session
也是如此
session
:同上,唯一的区别是请求的作用域变为了
session
global session
:全局的
HttpSession
中,容器会返回该
bean
的同一个实例,典型为在是使用
portlet
context
的时候有效(这个概念本人也不懂)
注意:如果要用到
request
,
session
,
global
session
时需要配置
servlet2.4
及以上:
在
web.xml
中添加:
<listener>
<listener-class>org.springframework.web.context.scope.RequestContextListener
/>
</listener>
servlet2.4
以下:
需要配置一个过滤器
<filter>
<filter-name>XXXX</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
<filter-mapping>
<filter-name>XXXX</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
另外,从
2.0
开始,可以自己定义作用域,但需要实现
scope
,并重写
get
和
remove
方法
特别要引起注意的是:
一般情况下前面两种作用域是够用的,但如果有这样一种情况:
singleton
类型的
bean
引用一个
prototype
的
bean
时会出现问题,因为
singleton
只初始化一次,但
prototype
每请求一次都会有一个新的对象,但
prototype
类型的
bean
是
singleton
类型
bean
的一个属性,理所当然不可能有新
prototpye
的
bean
产生,与我们的要求不符
解决方法:
1.
放弃
Ioc
,这与设计初衷不符,并代码间会有耦合
2
,
Lookup
方法注入,推荐
但在用
Lookup
方法注入时也需要注意一点:需要在引用的
Bean
中定一个一个抽象地返回被引用对象的方法
package com.huyong.lookup;
import java.util.Calendar;
/**
* @author HuYong Email:yate7571@hotmail.com
*/
public class CurrentTime {
private Calendar now = Calendar.getInstance();
public void printCurrentTime() {
System.out.println("Current Time:" + now.getTime());
}
}
package com.huyong.lookup;
/**
* @author HuYong Email:yate7571@hotmail.com
*/
public abstract class LookupBean {
private CurrentTime currentTime;
public CurrentTime getCurrentTime() {
return currentTime;
}
public void setCurrentTime(CurrentTime currentTime) {
this.currentTime = currentTime;
}
public abstract CurrentTime createCurrentTime();
}
<?xml version="1.0" encoding="UTF-8"?>
<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.0.xsd">
<bean id="currentTime"
class="com.huyong.lookup.CurrentTime"
scope="prototype
">
</bean>
<bean id="lookupBean"
class="com.huyong.lookup.LookupBean"
scope="singleton
">
<lookup-method name="createCurrentTime"
bean="currentTime" />
<property name="currentTime"
ref="currentTime"></property>
</bean>
</beans>
Main Test
:
package com.huyong.lookup;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
/**
* @author HuYong Email:yate7571@hotmail.com
*/
public class LookupMain {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ClassPathResource resource = new ClassPathResource(
"applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
LookupBean lookupBean = (LookupBean) factory.getBean("lookupBean");
System.out.println("----------first time---------");
System.out.println("getCurrentTime:");
lookupBean.getCurrentTime().printCurrentTime();
System.out.println("createCurrentTime:");
lookupBean.createCurrentTime().printCurrentTime();
Thread.sleep(12345);
System.out.println("---------second time---------");
System.out.println("getCurrentTime:");
LookupBean lookupBean02 = (LookupBean) factory.getBean("lookupBean");
lookupBean02.getCurrentTime().printCurrentTime();
System.out.println("createCurrentTime:");
lookupBean02.createCurrentTime().printCurrentTime();
}
}
感觉
Spring
的东西比较杂,学好
spring
一定要明白反射和代理是怎么回事!
渐渐的也挺会到了
Spring
的好处!
简单就是美!!
参考
本文来自百度文库,
http://wenku.baidu.com/view/424326270722192e4536f650.html
关于bean的作用域,还可以参见 http://blueram.javaeye.com/blog/352846
Spring
Bean
作用域介绍:
singleton
:
Spring
Ioc
容器只会创建该
Bean
的唯一实例,所有的请求和引用都只使用这个实例
Property:
每次请求都创建一个新实例
request:
在一次
Http
请求中,容器会返回该
Bean
的同一个实例,而对于不同的用户请求,会返回不同的实例。需要注意的是,该作用域仅在基于
Web
的
Spring
ApplicationContext
情形下有效,以下的
session
和
global
Session
也是如此
session
:同上,唯一的区别是请求的作用域变为了
session
global session
:全局的
HttpSession
中,容器会返回该
bean
的同一个实例,典型为在是使用
portlet
context
的时候有效(这个概念本人也不懂)
注意:如果要用到
request
,
session
,
global
session
时需要配置
servlet2.4
及以上:
在
web.xml
中添加:
<listener>
<listener-class>org.springframework.web.context.scope.RequestContextListener
/>
</listener>
servlet2.4
以下:
需要配置一个过滤器
<filter>
<filter-name>XXXX</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
<filter-mapping>
<filter-name>XXXX</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
另外,从
2.0
开始,可以自己定义作用域,但需要实现
scope
,并重写
get
和
remove
方法
特别要引起注意的是:
一般情况下前面两种作用域是够用的,但如果有这样一种情况:
singleton
类型的
bean
引用一个
prototype
的
bean
时会出现问题,因为
singleton
只初始化一次,但
prototype
每请求一次都会有一个新的对象,但
prototype
类型的
bean
是
singleton
类型
bean
的一个属性,理所当然不可能有新
prototpye
的
bean
产生,与我们的要求不符
解决方法:
1.
放弃
Ioc
,这与设计初衷不符,并代码间会有耦合
2
,
Lookup
方法注入,推荐
但在用
Lookup
方法注入时也需要注意一点:需要在引用的
Bean
中定一个一个抽象地返回被引用对象的方法
package com.huyong.lookup;
import java.util.Calendar;
/**
* @author HuYong Email:yate7571@hotmail.com
*/
public class CurrentTime {
private Calendar now = Calendar.getInstance();
public void printCurrentTime() {
System.out.println("Current Time:" + now.getTime());
}
}
package com.huyong.lookup;
/**
* @author HuYong Email:yate7571@hotmail.com
*/
public abstract class LookupBean {
private CurrentTime currentTime;
public CurrentTime getCurrentTime() {
return currentTime;
}
public void setCurrentTime(CurrentTime currentTime) {
this.currentTime = currentTime;
}
public abstract CurrentTime createCurrentTime();
}
<?xml version="1.0" encoding="UTF-8"?>
<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.0.xsd">
<bean id="currentTime"
class="com.huyong.lookup.CurrentTime"
scope="prototype
">
</bean>
<bean id="lookupBean"
class="com.huyong.lookup.LookupBean"
scope="singleton
">
<lookup-method name="createCurrentTime"
bean="currentTime" />
<property name="currentTime"
ref="currentTime"></property>
</bean>
</beans>
Main Test
:
package com.huyong.lookup;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
/**
* @author HuYong Email:yate7571@hotmail.com
*/
public class LookupMain {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ClassPathResource resource = new ClassPathResource(
"applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
LookupBean lookupBean = (LookupBean) factory.getBean("lookupBean");
System.out.println("----------first time---------");
System.out.println("getCurrentTime:");
lookupBean.getCurrentTime().printCurrentTime();
System.out.println("createCurrentTime:");
lookupBean.createCurrentTime().printCurrentTime();
Thread.sleep(12345);
System.out.println("---------second time---------");
System.out.println("getCurrentTime:");
LookupBean lookupBean02 = (LookupBean) factory.getBean("lookupBean");
lookupBean02.getCurrentTime().printCurrentTime();
System.out.println("createCurrentTime:");
lookupBean02.createCurrentTime().printCurrentTime();
}
}
感觉
Spring
的东西比较杂,学好
spring
一定要明白反射和代理是怎么回事!
渐渐的也挺会到了
Spring
的好处!
简单就是美!!
参考
本文来自百度文库,
http://wenku.baidu.com/view/424326270722192e4536f650.html
关于bean的作用域,还可以参见 http://blueram.javaeye.com/blog/352846
相关文章推荐
- spring中bean的作用域
- 详解Spring中bean的作用域
- Spring系列:第三篇-Spring容器三种实例化bean的方式以及Bean的作用域
- ITCAST视频-Spring学习笔记(配置Spring管理的bean的作用域)
- Spring第五弹—–配置Spring管理的bean的作用域和生命周期
- Spring Bean装配之Bean的作用域举例
- Spring、Bean 的作用域
- Spring之旅--Spring管理bean的作用域
- Spring中bean的作用域
- Spring 4.0 学习日记(4)--- bean的作用域
- spring之bean的作用域scope的值的详解
- Spring学习--Bean的作用域
- Spring容器中Bean的作用域
- Spring中Bean的作用域
- Spring Bean的5种作用域--主要是org.springframework.web.context.request.RequestContextListener
- spring中Bean的作用域
- Spring中Bean的五个作用域
- Spring中Bean的五个作用域
- spring中bean的作用域
- Spring整理系列(11)——@Configuration注解、@Bean注解以及配置自动扫描、bean作用域 发表于2016/8/11 15:16:16 10628人阅读 分类: Spring