(转载)spring单例和多例详解。如何在单例中调用多例对象
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单例和多例详解。如何在单例中调用多例对象的更多相关文章
- Native Application 开发详解(直接在程序中调用 ntdll.dll 中的 Native API,有内存小、速度快、安全、API丰富等8大优点)
文章目录: 1. 引子: 2. Native Application Demo 展示: 3. Native Application 简介: 4. Native Ap ...
- 使用Squid做代理服务器,Squid单网卡透明代理配置详解(转)
使用Squid做代理服务器 说到代理服务器,我们最先想到的可能是一些专门的代理服务器网站,某些情况下,通过它们能加快访问互联网的速度.其实,在需要访问外部的局域网中,我们自己就能设置代理,把访问次数较 ...
- (转载)完成端口(Completion Port, I/OCP)详解
http://www.cnblogs.com/lancidie/archive/2011/12/19/2293773.html 手把手叫你玩转网络编程系列之三 完成端口(Completion P ...
- 2017.2.13 开涛shiro教程-第十二章-与Spring集成(一)配置文件详解
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十二章-与Spring集成(一)配置文件详解 1.pom.xml ...
- 【Spring】——声明式事务配置详解
项目中用到了spring的事务: @Transactional(rollbackFor = Exception.class, transactionManager = "zebraTrans ...
- Python的Django框架中forms表单类的使用方法详解
用户表单是Web端的一项基本功能,大而全的Django框架中自然带有现成的基础form对象,本文就Python的Django框架中forms表单类的使用方法详解. Form表单的功能 自动生成HTML ...
- Spring Boot的每个模块包详解
Spring Boot的每个模块包详解,具体如下: 1.spring-boot-starter 这是Spring Boot的核心启动器,包含了自动配置.日志和YAML. 2.spring-boot-s ...
- 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. ...
- spring boot 配置文件properties和YAML详解
spring boot 配置文件properties和YAML详解 properties中配置信息并获取值. 1:在application.properties配置文件中添加: 根据提示创建直接创建. ...
- Spring全家桶——SpringBoot之AOP详解
Spring全家桶--SpringBoot之AOP详解 面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关键单元是类,而在AOP中,模块化单元是方 ...
随机推荐
- Maven知识点积累一
配置maven变量,变量名可以是:MAVEN_HOME 或 M2_HOME settings.xml配置本地仓库地址: <localRepository>G:/.m2/repository ...
- .NET Core开发日志——Entity Framework与PostgreSQL
Entity Framework在.NET Core中被命名为Entity Framework Core.虽然一般会用于对SQL Server数据库进行数据操作,但其实它还支持其它数据库,这里就以Po ...
- 泡泡一分钟:Towards real-time unsupervised monocular depth estimation on CPU
Towards real-time unsupervised monocular depth estimation on CPU Matteo Poggi , Filippo Aleotti , Fa ...
- git bash 常用命令
1. cd : 切换到哪个目录下, 如 cd e:\fff 切换 E 盘下面的fff 目录. 当我们用cd 进入文件夹时,我们可以使用 通配符*, cd f*, 如果E盘下只有一个f开头的文件 ...
- Improved SEO with mod_rewrite
PHP Advanced and Object-Oriented Programming Third Edition <?php //D:\wamp64\www\0613pm\w_wwwroot ...
- innodb表锁情况
MySQL InnoDB默认行级锁.行级锁都是基于索引的 行级锁变为表级锁情况如下: 1.如果一条SQL语句用不到索引是不会使用行级锁的,会使用表级锁把整张表锁住. 2.表字段进行变更. 3.进行整表 ...
- jmeter发送https请求
- android实现手势锁
通过简单的设置后即可实现简单的手势锁: setLineVisible方法设置是否显示手势路径: setLineWidth方法设置手势路径连线的粗细: setLineColor方法设置常规状态手势路径连 ...
- shiro入门实例,基于ini配置
基于ini或者关系数据库的,其实都是一样的,重要的是思想. # ==================================================================== ...
- Es6 的类(class)
首先根据es5的类(原型对象)的基本点做参照. 序号 基本点 es5 >es6 1 实例属性(方法) √ √ 2 原型属性(方法) 或 公共属性(方法) √ √ 3 es5的私有变量 或 私有属 ...