在Spring里面,当一个singleton bean依赖一个prototype bean,那么,因为singleton bean是单例的,因此prototype bean在singleton bean里面也会变成单例,例如:

public class Main{

private Man man;

//这里注入一个prototype的Man实例

public void setMan(Man man) {          this.man= man;      }

public Man getMan() {          return man;      }

}

配置文件:

<bean id="man" class="test.American" scope="prototype"/>  <!-- 这里声明一个多例的bean -->

<bean id="main" class="test.Main">  <!-- 这里声明一个单例的bean -->      <property name="man" ref="man"/>     <!-- 这里依赖一个多例的bean --> </bean>

如果我为Main创建一个单例bean,那么当我注入Man实例的时候,man属性也会变成单例了,跟我预期的效果不一样。

解决方法1:放弃依赖注入,使用传统工厂思想,通过ApplicationContext的getBean方法获取Man的实例。但是缺点比较明显,跟Spring框架耦合了。

解决方法2:如果我仍然想得到依赖注入带来的好处,那么可以使用Spring提供的lookup-method来注入。

不过使用之前,我们需要下载多一个jar包,叫做cglib-full.jar,我使用的版本是cglib-full-2.0.1.jar。

现在我们只需要修改一下我们的配置文件就可以了,现在修改成:

<bean id="man" class="test.American" scope="prototype"/>  <!-- 这里声明一个多例的bean -->

<bean id="main" class="test.Main">  <!-- 这里声明一个单例的bean -->      <lookup-method name="getMan" bean="man"/>     <!-- 这里依赖一个多例的bean --> </bean>

注意红色字体部分,我们不使用原来的property标签,改用looup-method标签。name属性表示当调用指定方法的时候重新注入一个实例,这个实例对应bean属性的值。

最后,请看如下测试代码:

ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");          Main main = (Main) ctx.getBean("main");

Man man1 = main.getMan();     //这里通过Spring的注入,获取第1个Man实例

Man man2 = main.getMan();     //这里通过Spring的注入,获取第2个Man实例

super.assertFalse(man1 == man2);

//请注意,man1和man2现在已经指向两个不同的引用,因此它们的引用不相等。

使用lookup-method解决singleton bean依赖prototype bean的问题的更多相关文章

  1. 当singleton Bean依赖propotype Bean,可以使用在配置Bean添加look-method来解决

    在Spring里面,当一个singleton bean依赖一个prototype bean,因为singleton bean是单例的,因此prototype bean在singleton bean里面 ...

  2. spring lookup method 注入

           lookup method注入是spring动态改变bean里方法的实现.方法执行返回的对象,使用spring内原有的这类对象替换,通过改变方法返回值来动态改变方法.内部实现为使用cgl ...

  3. 解决Spring中singleton的Bean依赖于prototype的Bean的问题

    在spring bean的配置的时候,可能会出现一个singleton的bean依赖一个prototype的bean.因为singleton的bean只有一次初始化的机会,所以他们的依赖关系页只有在初 ...

  4. [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. Failed to introspect bean class [org.springframework.orm.hibernate5.LocalSessionFactoryBean] for lookup method metadata: could not find class that it depends on; nested exception is java.lang.NoClass

    依赖引入  错误可能版本 不对 Failed to introspect bean class [org.springframework.orm.hibernate5.LocalSessionFact ...

  6. Spring -10 -<bean>的 scope 属性 -singleton 默认值/prototype 多例 /request /session /application /global session

    1.<bean>的属性; 2.作用:控制对象有效范围(单例,多例等)3.<bean/>标签对应的对象默认是单例的. 3.1无论获取多少次,都是同一个对象 Teacher t1 ...

  7. SpringMVC“Ambiguous mapping found. Cannot map 'XXXController' bean method”解决方法

    [转 :http://www.fanfanyu.cn/news/staticpagefile/2351.html] 最近在开发项目的过程中SpringMVC抛了个"Ambiguous map ...

  8. 1.spring.net Look-up Method 查找方法的注入(方法是抽象的需要spring.net注入)

    .为什么需要查找方法的注入 当Object依赖另一个生命周期不同的Object,尤其是当singleton依赖一个non-singleton时,常会遇到不少问题,Lookup Method Injec ...

  9. Spring 循环依赖的三种方式(三级缓存解决Set循环依赖问题)

    本篇文章解决以下问题: [1] . Spring循环依赖指的是什么? [2] . Spring能解决哪种情况的循环依赖?不能解决哪种情况? [3] . Spring能解决的循环依赖原理(三级缓存) 一 ...

随机推荐

  1. BZOJ1452 [JSOI2009]Count 【树套树 (树状数组)】

    1452: [JSOI2009]Count Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 2693  Solved: 1574 [Submit][St ...

  2. shell里的getopts

    By francis_hao    Jul 5,2017   getopts是shell的一个内置命令. 概述 getopts optstring name [args]OPTIND,OPTARG,O ...

  3. python3创建目录

    感觉python3最好用的创建目录函数是os.makedirs,它可以设置在多级目录不存在时自动创建,已经存在也不抛出异常. import os os.makedirs('hello/hello1/h ...

  4. hdu 1520Anniversary party 树形dp入门

    There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The Un ...

  5. Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合

    Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合 标签: mybatisSpringbeanApplicationContextMapper 2015-12-31 1 ...

  6. Flume的安装,配置及使用

    1,上传jar包 2,解压 3,改名 4,更改配置文件 将template文件重镜像 root@Ubuntu-1:/usr/local/apache-flume/conf# cat flume-env ...

  7. centos7下yum快速安装 mariadb(mysql)

    从最新版本的centos系统开始,默认的是 Mariadb而不是mysql! 使用系统自带的repos安装很简单: yum install mariadb mariadb-server systemc ...

  8. 【Python实例一】使用minidom读取xml文件

    前言:最近刚在廖雪峰老师的网站里学习了Python的基础内容,想着循序渐进地找点实例练练手,网上看到有很多相关资料,决定针对感兴趣的内容实际编码实践一下,昨天刚好看到有关使用Python来读取XML文 ...

  9. shell脚本之正则表达和文本处理(文本处理三剑客:1、grep 2、sed 3、awk)

    文本处理三剑客:1.grep  2.sed  3.awk 一.grep:(过滤) grep的使用,主要的参数有: -n  :显示行号:-o  :只显示匹配的内容-q  :静默模式,没有任何输出,得用e ...

  10. redis有string,hash,list,sets.zsets几种数据类型

    1.string数据类型 可包含任何数据,是二进制安全的,比如图片或者序列化的对象set key valueset name hkset age 20get name 得到"hk" ...