spring单例和多例详解。如何在单例中调用多例对象

来源:互联网 发布:ubuntu安装mysql失败 编辑:程序博客网 时间:2024/05/16 15:33

spring生成对象默认是单例的。通过scope属性可以更改为多例。

[html] view plaincopy
  1. <bean id="user" class="modle.User" scope="prototype">  
  2.     </bean>  

现在又这么一种情况.

User类调用一个service, 这个service又调用一个tool。

有时我们希望User是多例的,service是单例的,而tool又是多例的。

很自然地想法是配置文件这些写

[html] view plaincopy
  1. <bean id="user" class="modle.User" scope="prototype">  
  2.     <property name="service" ref="userservice"></property>  
  3. </bean>  
  4.   
  5. <bean id="userservice" class="service.userService" >  
  6.     <property name="tool" ref="tool"></property>  
  7. </bean>  
  8.   
  9. <bean id="tool" class="service.ToolImpl" scope="prototype"></bean>  


但是这种写法是错误的! 不能使用spring的自动注入!

由于service是单例的,所以这种方法的结果是:User多例,service和tool都是单例。(为什么?)


官网文档:

[html] view plaincopy
  1. 4.5.3 Singleton beans with prototype-bean dependencies  
  2.   
  3. 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.  
  4.   
  5. 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

[java] view plaincopy
  1. public class User {  
  2.   
  3.     private userService service;  
  4.     private int age;  
  5.     private Date date;  
  6.     private String name;  
  7.     public int getAge() {  
  8.         return age;  
  9.     }  
  10.     public void setAge(int age) {  
  11.         this.age = age;  
  12.     }  
  13.     public Date getDate() {  
  14.         return date;  
  15.     }  
  16.     public void setDate(Date date) {  
  17.         this.date = date;  
  18.     }  
  19.     public String getName() {  
  20.         return name;  
  21.     }  
  22.     public void setName(String name) {  
  23.         this.name = name;  
  24.     }  
  25.     public userService getService() {  
  26.         return service;  
  27.     }  
  28.     public void setService(userService service) {  
  29.         this.service = service;  
  30.     }  
  31.       
  32. }  

UserService 通过实现 BeanFactoryAware 接口来获得factory

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

[java] view plaincopy
  1. public class userService implements BeanFactoryAware{  
  2.       
  3.     private Tool tool;  
  4.     private BeanFactory factory;  
  5.     public void service(){  
  6.         this.tool = (Tool)factory.getBean("tool");  
  7.         System.out.println(this+":service");  
  8.         tool.work();  
  9.     }  
  10.     public Tool getTool() {  
  11.           
  12.         return tool;  
  13.     }  
  14. //  public void setTool(Tool tool) {  
  15. //        
  16. //      this.tool = (Tool)factory.getBean("tool");  
  17. //  }  
  18.     public void setBeanFactory(BeanFactory f) throws BeansException {  
  19.         factory = f;  
  20.     }  
  21.       
  22. }  

配置文件,不能再使用注入。因此要把tool对象的注入去掉!

[html] view plaincopy
  1. <bean id="user" class="modle.User" scope="prototype">  
  2.     <property name="service" ref="userservice"></property>  
  3. </bean>  
  4.   
  5. <bean id="userservice" class="service.userService" >  
  6. </bean>  
  7.   
  8. <bean id="tool" class="service.ToolImpl" scope="prototype"></bean>  

[java] view plaincopy
  1. public interface Tool {  
  2.     public void work();  
  3. }  


[java] view plaincopy
  1. public class ToolImpl implements Tool{  
  2.   
  3.     public void work() {  
  4.         System.out.println(this+":Tool Work");  
  5.     }  
  6.       
  7. }  

测试类:

[java] view plaincopy
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         ClassPathResource res = new ClassPathResource("applicationContext.xml");  
  4.         XmlBeanFactory factory = new XmlBeanFactory(res);  
  5.         User user = (User)factory.getBean("user");  
  6.         User user2 =  (User)factory.getBean("user");  
  7.           
  8.         System.out.println(user);  
  9.         user.getService().service();  
  10.         System.out.println();  
  11.         System.out.println(user2);  
  12.         user2.getService().service();  
  13.     }  
  14. }  

Output:

[html] view plaincopy
  1. modle.User@42552c  
  2. service.userService@19e15c:service  
  3. service.ToolImpl@11a75a2:Tool Work  
[html] view plaincopy
  1. modle.User@210b5b  
  2. service.userService@19e15c:service  
  3. service.ToolImpl@170888e:Tool Work  
0 0
原创粉丝点击