文章出處


單例多例需要搞明白這些問題:
     1. 什么是單例多例;
     2. 如何產生單例多例;
     3. 為什么要用單例多例
     4. 什么時候用單例,什么時候用多例;
 
1. 什么是單例、多例:
     所謂單例就是所有的請求都用一個對象來處理,比如我們常用的service和dao層的對象通常都是單例的,而多例則指每個請求用一個新的對象來處理,比如action;
 
     單例模式和多例模式說明:
     1. 單例模式和多例模式屬于對象模式。
     2. 單例模式的對象在整個系統中只有一份,多例模式可以有多個實例。
     3. 它們都不對外提供構造方法,即構造方法都為私有。
 
2. 如何產生單例、多例:
    在通用的SSH中,單例在spring中是默認的,如果要產生多例,則在配置文件的bean中添加scope="prototype";
 
3. 為什么用單例、多例:
    之所以用單例,是因為沒必要每個請求都新建一個對象,這樣子既浪費CPU又浪費內存;
   之所以用多例,是為了防止并發問題;即一個請求改變了對象的狀態,此時對象又處理另一個請求,而之前請求對對象狀態的改變導致了對象對另一個請求做了錯誤的處理;
    用單例和多例的標準只有一個:
    當對象含有可改變的狀態時(更精確的說就是在實際應用中該狀態會改變),則多例,否則單例;
 
4. 何時用單例?何時用多例?
    對于struts2來說,action必須用多例,因為action本身含有請求參數的值,即可改變的狀態;
    而對于STRUTS1來說,action則可用單例,因為請求參數的值是放在actionForm中,而非action中的;
    另外要說一下,并不是說service或dao一定是單例,標準同第3點所講的,就曾見過有的service中也包含了可改變的狀態,同時執行方法也依賴該狀態,但一樣用的單例,這樣就會出現隱藏的BUG,而并發的BUG通常很難重現和查找;
 
      spring生成對象默認是單例的。通過scope屬性可以更改為多例
 
<bean id="user" class="modle.User" scope="prototype">  </bean>
 
現在又這么一種情況.

 

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;
 
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

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()