spring单例和多例详解。如何在单例中调用多例对象
2015-04-11 21:50
134 查看
spring生成对象默认是单例的。通过scope属性可以更改为多例。
[html] view
plaincopy
<bean id="user" class="modle.User" scope="prototype">
</bean>
现在又这么一种情况.
User类调用一个service, 这个service又调用一个tool。
有时我们希望User是多例的,service是单例的,而tool又是多例的。
很自然地想法是配置文件这些写
[html] view
plaincopy
<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都是单例。(为什么?)
官网文档:
[html] view
plaincopy
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
[java] view
plaincopy
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方法要去掉!
[java] view
plaincopy
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对象的注入去掉!
[html] view
plaincopy
<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>
[java] view
plaincopy
public interface Tool {
public void work();
}
[java] view
plaincopy
public class ToolImpl implements Tool{
public void work() {
System.out.println(this+":Tool Work");
}
}
测试类:
[java] view
plaincopy
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:
[html] view
plaincopy
modle.User@42552c
service.userService@19e15c:service
service.ToolImpl@11a75a2:Tool Work
[html] view
plaincopy
modle.User@210b5b
service.userService@19e15c:service
service.ToolImpl@170888e:Tool Work
[html] view
plaincopy
<bean id="user" class="modle.User" scope="prototype">
</bean>
现在又这么一种情况.
User类调用一个service, 这个service又调用一个tool。
有时我们希望User是多例的,service是单例的,而tool又是多例的。
很自然地想法是配置文件这些写
[html] view
plaincopy
<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都是单例。(为什么?)
官网文档:
[html] view
plaincopy
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
[java] view
plaincopy
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方法要去掉!
[java] view
plaincopy
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对象的注入去掉!
[html] view
plaincopy
<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>
[java] view
plaincopy
public interface Tool {
public void work();
}
[java] view
plaincopy
public class ToolImpl implements Tool{
public void work() {
System.out.println(this+":Tool Work");
}
}
测试类:
[java] view
plaincopy
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:
[html] view
plaincopy
modle.User@42552c
service.userService@19e15c:service
service.ToolImpl@11a75a2:Tool Work
[html] view
plaincopy
modle.User@210b5b
service.userService@19e15c:service
service.ToolImpl@170888e:Tool Work
相关文章推荐
- spring单例和多例详解。如何在单例中调用多例对象
- spring单例和多例详解。如何在单例中调用多例对象
- spring单例和多例详解。如何在单例中调用多例对象
- spring单例和多例详解。如何在单例中调用多例对象
- spring单例和多例详解。如何在单例中调用多例对象
- C#如何调用原生C++ COM对象详解
- servlet中如何调用spring中注入的对象
- Spring IOC 容器如何调用 对象的 set 和构造方法的
- 详解如何动态调用WebService传递复杂对象的实现
- 详解JVM中堆、栈、方法区(对象、值)是如何调用执行的
- spring 如何在普通类中调用注入的对象
- Spring 之 普通类如何调用Spring bean对象
- 关于Spring管理的类如何创建对象。
- Spring注入对象调用时为空的异常
- JSP页面调用spring容器中的对象
- 如何在JSP页面中调用Spring容器注入的Bean?
- [iphone 开发学习笔记]Object-C和C语言最大的区别也是最大的扩展-----消息(即如何调用一个对象中的函数)
- JavaScript函数调用时的作用域链和调用对象是如何形成的及与闭包的关系
- liferay5.2.2中的action如何调用Spring中的bean?
- DCOM 示例:演示如何远程调用 COM 对象