Posted on 2009-09-14 10:30
cyantide 閱讀(609)
評論(0) 編輯 收藏 所屬分類:
spring
singleton:SpringIoc容器只會創建該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();
}
}