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

原文  http://blog.csdn.net/gaotong2055/article/details/8245036

 

(转载)spring单例和多例详解。如何在单例中调用多例对象的更多相关文章

  1. Native Application 开发详解(直接在程序中调用 ntdll.dll 中的 Native API,有内存小、速度快、安全、API丰富等8大优点)

    文章目录:                   1. 引子: 2. Native Application Demo 展示: 3. Native Application 简介: 4. Native Ap ...

  2. 使用Squid做代理服务器,Squid单网卡透明代理配置详解(转)

    使用Squid做代理服务器 说到代理服务器,我们最先想到的可能是一些专门的代理服务器网站,某些情况下,通过它们能加快访问互联网的速度.其实,在需要访问外部的局域网中,我们自己就能设置代理,把访问次数较 ...

  3. (转载)完成端口(Completion Port, I/OCP)详解

    http://www.cnblogs.com/lancidie/archive/2011/12/19/2293773.html 手把手叫你玩转网络编程系列之三    完成端口(Completion P ...

  4. 2017.2.13 开涛shiro教程-第十二章-与Spring集成(一)配置文件详解

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十二章-与Spring集成(一)配置文件详解 1.pom.xml ...

  5. 【Spring】——声明式事务配置详解

    项目中用到了spring的事务: @Transactional(rollbackFor = Exception.class, transactionManager = "zebraTrans ...

  6. Python的Django框架中forms表单类的使用方法详解

    用户表单是Web端的一项基本功能,大而全的Django框架中自然带有现成的基础form对象,本文就Python的Django框架中forms表单类的使用方法详解. Form表单的功能 自动生成HTML ...

  7. Spring Boot的每个模块包详解

    Spring Boot的每个模块包详解,具体如下: 1.spring-boot-starter 这是Spring Boot的核心启动器,包含了自动配置.日志和YAML. 2.spring-boot-s ...

  8. idea spring+springmvc+mybatis环境配置整合详解

    idea spring+springmvc+mybatis环境配置整合详解 1.配置整合前所需准备的环境: 1.1:jdk1.8 1.2:idea2017.1.5 1.3:Maven 3.5.2 2. ...

  9. spring boot 配置文件properties和YAML详解

    spring boot 配置文件properties和YAML详解 properties中配置信息并获取值. 1:在application.properties配置文件中添加: 根据提示创建直接创建. ...

  10. Spring全家桶——SpringBoot之AOP详解

    Spring全家桶--SpringBoot之AOP详解 面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关键单元是类,而在AOP中,模块化单元是方 ...

随机推荐

  1. aspectj 注解

    aspectj是一个面向切面编程的框架,即实现了aop,这不是spring,它本身很小,方便简洁,spring将其整合成自己的. 与spring本身对aop的支持不同,顾问采用正则表达式或者方法名或通 ...

  2. weblogic启动项目,设置内容、设置的数据源链接不生效

    昨天坑自己了一把,把weblogic的数据库连接方式由jdbc改成了jndi,然后不生效,还是走之前jdbc的连接地址. 因为数据库用户之前权限有问题,所以一直纠结于这个地方,忘记了缓存的原因. 后来 ...

  3. 如何打jar包 学习笔记

    jar包是由.class文件压缩而成.要查看jar包中的内容,使用压缩工具 解压缩即可.也可以做修改,并重新打成jar包.总结一下最近学到的一些打jar包的方法: 一.DOS下使用jar命令 打jar ...

  4. 在eclipse中导入hadoop jar包,和必要时导入源码包。

    1. 解药hadoop包 1, C:\hadoop-2.7.2\share\hadoop  提取出所有的 jar 包, 到 _lib 文件夹下 2,将有含有source 名称的jar包 剪切出来 3, ...

  5. POJ 1451 - T9 - [字典树]

    题目链接:http://bailian.openjudge.cn/practice/1451/ 总时间限制: 1000ms 内存限制: 65536kB 描述 Background A while ag ...

  6. plsql developer 安装

    PLSQL Developer 安装分以下几步: 一.下载Oracle客户端 PLSQL Developer是通过oracle client连上Oracle server的. http://www.o ...

  7. en-zh(科学技术)science and technology

    S Korea to roll out 5G韩国正式推5G商用服务 South Korea will become the first country to commercially launch f ...

  8. 内部排序->归并排序->2-路归并排序

    文字描述 假设初始序列有n个记录,则可看成是n个有序的字序列,每个字序列的长度为1,然后两两归并,得到[n/2]个长度为2或1的有序子序列:再两两归并,…, 如此重复,直到得到一个长度为n的有序序列为 ...

  9. 洛谷P3311 [SDOI2014]数数 AC自动机+dp

    正解:AC自动机+dp 解题报告: 传送门! 首先看到多串匹配balabala显然想到建个AC自动机? 然后可以用一点儿数位dp的思想地想下(,,,其实并不算QAQ 幸运数可以分为两类:位数<n ...

  10. 【PyQt5-Qt Designer】按钮系列

    [PyQt5-Qt Designer]按钮系列 复选框(QCheckBox) 效果如下: 参考: https://zhuanlan.zhihu.com/p/30509947 完整代码: from Py ...