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;
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
分享到:
相关推荐
单例多例
如果只用@scope(“prototype”),单例对象中的多例对象会失效,导致@scope(“prototype”)失效原因因为单例对象加载时只会初始化一次,
多例:一个类能产生多个对象(对应到spring中,注入的对象永远是新的) @Scope("prototype") @Scope("singleton") 可以使用@Scope注解,标记这个类是单例还是多例,默认是单例。 二、使用单例引起线程安全问题的...
Spring In Action-3.2@Scope单例、多例Bean,Spring In Action-3.2@Scope单例、多例Bean,Spring In Action-3.2@Scope单例、多例Bean,Spring In Action-3.2@Scope单例、多例Bean
主要介绍了浅谈Spring单例Bean与单例模式的区别,具有一定借鉴价值,需要的朋友可以参考下
主要介绍了Spring Boot和Hazelcast使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了浅谈Spring 的Controller 是单例or多例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
主要介绍了Spring bean为什么默认是单例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
spring框架,技术详解及使用指导spring框架,技术详解及使用指导spring框架,技术详解及使用指导spring框架,技术详解及使用指导spring框架,技术详解及使用指导spring框架,技术详解及使用指导spring框架,技术详解及使用...
假定两个Controller都在同一个工程中。 如果有比较合理的分层设计,这样的需求应该是非常...因为Controller B的实例中,Spring Boot为其进行配置和对象注入,这些准备工作不是简单通过实例化Controller B可以完成。
spring3 注解 详解spring3 注解 详解spring3 注解 详解
spring几种事务配置,spring几种事务配置详解【精】
【ASP.NET编程知识】详解Spring Boot 中使用 Java API 调用 lucene.docx
Spring MVC 框架搭建及详解 适合郁闷程序员 超级有用
Spring中事务的传播属性详解,Spring中事务的传播属性详解
利用spring实现的简单的缓存的例子,代码解释:http://blog.csdn.net/maoyeqiu/article/details/50238035
spring boot配置文件参数详解,spring boot配置文件参数详解,spring boot配置文件参数详解,spring boot配置文件参数详解,
主要介绍了Spring Security UserDetails实现原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下