spring单例和多例详解的实验,以及如何使用多例模式

来源:互联网 发布:golang cgo windows 编辑:程序博客网 时间:2024/06/06 03:27

原文:http://www.tuicool.com/articles/RzIvAj


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 dependenciesWhen 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@42552cservice.userService@19e15c:serviceservice.ToolImpl@11a75a2:Tool Work
modle.User@210b5bservice.userService@19e15c:serviceservice.ToolImpl@170888e:Tool Work

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 香炉里面放的小米生虫子了怎么办 肉肉上长满了白色的小虫子怎么办 多肉上面有白色的虫子怎么办 多肉植物根部长白色虫子怎么办 朋友玩期货把我钱赔了怎么办 宝宝床上虫子咬了肿大包怎么办 喷药的杀虫剂的喷头坏了怎么办 宅e经营贷个人没有公司怎么办 浏览器下载完插件安装不好用怎么办 要求评审专家复核他不来怎么办 微博国际版不能用微博号登录怎么办 云南省特岗登录名忘了怎么办 人行登录名忘了再怎么办 生源地贷款登录名忘了怎么办 举报19楼帖子但是不给删除怎么办 新浪微博手机号被注册了怎么办 忘记新浪微博绑定的邮箱账号怎么办 申诉找回微信账号密码失败怎么办 注册微信号时验证码错误怎么办 老板让写的报道没有当天写完怎么办 洛奇英雄传镶嵌有微章的时装怎么办 上午12点用24小时制怎么办 三分钟看懂捷信个人贷款怎么办 精神不正常的父亲到单位闹怎么办 母亲和父亲一直和我闹怎么办 儿子拿了父亲的钱买手机怎么办 电动车骑的慢的时候车头打漂怎么办 二晓啊相公太爱我了怎么办全文免费 车牌被套牌了又有违章怎么办 高中生只学好主课副科学不好怎么办 母泰迪怀孕23天了不吃饭怎么办 媳妇一再触碰我的底线怎么办 发票商品编码好多选错了分类怎么办 吃了用福尔马林泡的食物怎么办 没大没小说话不尊重人的孩子怎么办 升级安卓8.0后app闪退怎么办 业主装门占用消防通道物业怎么办? 想改名字派出所不给改怎么办 物业不给地热打压影响装修怎么办 pos机pin效码验证错误怎么办 苹果下载东西要发验证码怎么办