1. 程式人生 > >spring單例和多例詳解。如何在單例中呼叫多例物件

spring單例和多例詳解。如何在單例中呼叫多例物件


spring生成物件預設是單例的。通過scope屬性可以更改為多例。

<bean id="user" class="modle.User" scope="prototype">
</bean>

在使用Spring3對控制器Controller進行bean管理時,如果要對控制器是否單例進行管理。

有兩種方式配置多例模式:

1.springXML

2.註解本身的控制器類

[java] view plaincopyprint?
  1. @Controller
  2. @Scope("prototype")  
  3. publicclass HelloContorller {  
  4.     privateint index=
    0;  
  5.     Logger logger=Logger.getLogger(HelloContorller.class.getName());  
  6.     //hello world例子
  7.     @RequestMapping(value="/hello")  
  8.     public String hello(){  
  9.         logger.info("spring mvc hello world!"+index++);  
  10.         return"hello";  
  11.     }  
  12. }  

這裡有個困惑就是當index變數為靜態時,那麼儘管是多例模式下,對於每次請求訪問,index變數都會累積相加。所以可以初步斷定,多例的產生原理不簡簡單單是重新new一個控制器。


現在又這麼一種情況.

User類呼叫一個service, 這個service又呼叫一個tool。

有時我們希望User是多例的,service是單例的,而tool又是多例的。

很自然地想法是配置檔案這些寫

<bean id="user" class="modle.User" scope="prototype">
<property name="service" ref="userservice"></property>
</bean>

<bean id="userservice" class="service.userService"
>
<property name="tool" ref="tool"></property> </bean> <bean id="tool" class="service.ToolImpl" scope="prototype"></bean>


但是這種寫法是錯誤的! 不能使用spring的自動注入!

由於service是單例的,所以這種方法的結果是:User多例,service和tool都是單例。(為什麼?)

官網文件:

4.5.3 Singleton beans with prototype-bean dependencies

When you use singleton-scoped beans with dependencies on prototype beans, be aware that dependencies are resolved at instantiation time. Thus if you dependency-inject a prototype-scoped bean into a singleton-scoped bean, a new prototype bean is instantiated and then dependency-injected into the singleton bean. The prototype instance is the sole instance that is ever supplied to the singleton-scoped bean.

However, suppose you want the singleton-scoped bean to acquire a new instance of the prototype-scoped bean repeatedly at runtime. You cannot dependency-inject a prototype-scoped bean into your singleton bean, because that injection occurs only once, when the Spring container is instantiating the singleton bean and resolving and injecting its dependencies. If you need a new instance of a prototype bean at runtime more than once, see Section 4.4.6, “Method injection”


正確的寫法是,是每次呼叫tool時都生成一個新的tool物件。但是我們又不能手動new一個,要藉助BeanFactory

public class User {

private userService service;
private int age;
private Date date;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public userService getService() {
return service;
}
public void setService(userService service) {
this.service = service;
}

}

UserService 通過實現 BeanFactoryAware 介面來獲得factory

由於不使用spring的自動注入,set方法要去掉!

public class userService implements BeanFactoryAware{

private Tool tool;
private BeanFactory factory;
public void service(){
this.tool = (Tool)factory.getBean("tool");
System.out.println(this+":service");
tool.work();
}
public Tool getTool() {

return tool;
}
//	public void setTool(Tool tool) {
//		
//		this.tool = (Tool)factory.getBean("tool");
//	}
public void setBeanFactory(BeanFactory f) throws BeansException {
factory = f;
}

}

配置檔案,不能再使用注入。因此要把tool物件的注入去掉!
<bean id="user" class="modle.User" scope="prototype">
<property name="service" ref="userservice"></property>
</bean>

<bean id="userservice" class="service.userService" >
</bean>

<bean id="tool" class="service.ToolImpl" scope="prototype"></bean>
public interface Tool {
public void work();
}


public class ToolImpl implements Tool{

public void work() {
System.out.println(this+":Tool Work");
}

}

測試類:
public class Test {
public static void main(String[] args) {
ClassPathResource res = new ClassPathResource("applicationContext.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
User user = (User)factory.getBean("user");
User user2 =  (User)factory.getBean("user");

System.out.println(user);
user.getService().service();
System.out.println();
System.out.println(user2);
user2.getService().service();
}
}

Output:
modle.User@42552c
service.userService@19e15c:service
service.ToolImpl@11a75a2:Tool Work
modle.User@210b5b
service.userService@19e15c:service
service.ToolImpl@170888e:Tool Work