对于Spring的多数用户而言,主要的Bean存在形式都是单例,当一个单例需要结合另一个单例协作或者一个非单例与另一个非单例协作时,典型的做法是通过属性的形式注入,但是当两个Bean的声明周期不同时候这会存在一个问题。例如单例A依赖一个非单例B,而对外提供的服务是通过A暴露的,这样的话每一次调用A的方法时候都不会更新实例B,这将会导致问题(因为A的属性只在初始化时候被设置一次,之后不再被设置)。对于这个问题有如下解决方案:

1:不再使用以来注入,而是实现BeanFactoryAware接口之后每一次通过getBean的方法获取一个新的实例B,但是这通常不是一个理想的解决方案,因为bean代码耦合到Spring中。

2:Method Injection是BeanFactory的一个高级特性,以一种优雅的方式解决此问题。

  1. public abstract class UserService {
  2.  
  3. public abstract WalletService createWalletService() ;
  4. public UserService() {
  5. System.out.println("UserService 正在实例化!");
  6. }
  7.  
  8. public void login(String userName, String passWord) {
  9. createWalletService().run();
  10. System.out.println(userName + "正在登陆!");
  11. }
  12. }

  

  1. public class WalletService {
  2. public WalletService(){
  3. System.out.println("CarService");
  4. }
  5. public void run() {
  6. System.out.println("this = " + this);
  7. }
  8. }

walletService是一个非单例bean,每一次用户调用时候都必须获取一个新的walletService进行请求,这种需求配置如下:

  1. <bean id="walletService" class="com.daxin.service.WalletService" singleton="false"/>
  2. <bean id="userService" class="com.daxin.service.UserService">
  3. <lookup-method name="createWalletService" bean="walletService"></lookup-method>
  4. </bean>  

测试代码:

  1. public static void main(String[] args) throws CloneNotSupportedException {
  2. ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  3. for (int i = 0; i < 5; i++) {
  4. UserService userService = (UserService) ctx.getBean("userService");
  5. userService.login("daxin", "root");
  6. }
  7. ctx.close();
  8. }

  

测试输出:

  1. daxin正在登陆!
  2. CarService
  3. this = com.daxin.service.WalletService@40e6dfe1
  4. daxin正在登陆!
  5. CarService
  6. this = com.daxin.service.WalletService@1b083826
  7. daxin正在登陆!
  8. CarService
  9. this = com.daxin.service.WalletService@105fece7
  10. daxin正在登陆!
  11. CarService
  12. this = com.daxin.service.WalletService@3ec300f1
  13. daxin正在登陆!

  

Spring之Method Injection的更多相关文章

  1. .NET手记-Autofac进阶(属性和方法注入 Property and Method Injection)

    尽管构造函数参数注入是传递参数值给当前构造的组件的优先方式,但是你也可以使用属性或者方法注入来提供参数值. 属性注入使用可写入的变量而不是构造函数参数来完成注入.方法注入则通过方法来设置依赖项. 属性 ...

  2. Autofac Property Injection and Method Injection

    While constructor parameter injection is the preferred method of passing values to a component being ...

  3. 使用Mybatis整合spring时报错Injection of autowired dependencies failed;

    这是无法自动注入,通过查找,我出错的原因在于配置文件的路径没有写对,在applicationContext.xml中是这样写的. <bean id="sqlSessionFactory ...

  4. spring security method security

    参考 Spring Security 官方文档 http://www.concretepage.com/spring/spring-security/preauthorize-postauthoriz ...

  5. Spring EL method invocation example

    In Spring EL, you can reference a bean, and nested properties using a 'dot (.)' symbol. For example, ...

  6. [Spring] 04 Denpendency Injection

    DI Dependency Injection 依赖注入:从程序代码中移除依赖关系的一种设计模式. 这样就可以更容易地管理和测试应用程序. DI使我们的程序编码 loosely coupled.松耦合 ...

  7. spring init method destroy method

    在java的实际开发过程中,我们可能常常需要使用到init method和destroy method,比如初始化一个对象(bean)后立即初始化(加载)一些数据,在销毁一个对象之前进行垃圾回收等等. ...

  8. Benefits of Using the Spring Framework Dependency Injection 依赖注入 控制反转

    小结: 1. Dependency Injection is merely one concrete example of Inversion of Control. 依赖注入是仅仅是控制反转的一个具 ...

  9. spring replaced method 注入

           replaced method注入是spring动态改变bean里方法的实现.需要改变的方法,使用spring内原有其他类(需要继承接口org.springframework.beans ...

随机推荐

  1. groovy编程注意事点

    集合中一些方法工作在集合的一个副本上并且完成的时候返回这个副本,而另外一些方法直接操作这个集合对象. 和Java一样不能通过迭代移除元素. list和map遵循java规则限制,但通过附加的方法降低了 ...

  2. 【学习笔记】--- 老男孩学Python,day5 列表 元祖

    今日主要内容1. list(增删改查) 列表可以装大量的数据. 不限制数据类型. 表示方式:[] 方括号中的每一项用逗号隔开 列表和字符串一样.也有索引和切片 常用的功能: 1. 增: append( ...

  3. 集合框架二(Collection接口实现类常用遍历方法)

    四种常用遍历方式 Collection coll = new ArrayList(); coll.add("123"); coll.add("456"); co ...

  4. mongodb ISODate问题(大量数据update优化)

    问题描述: 上周有个需求,把mongodb中birthday (ISO日期格式) 转换成北京时间,并保存成string类型. 最初思路: 遍历查找出的结果,逐个加8小时,然后通过_id逐个去updat ...

  5. AJAX的一些基础和AJAX的状态

    大佬们,我又回来了,最近好几天都没写博客了,别问我干啥去了,我只是去围观奶茶妹变成抹茶妹而已 前几天我们一起封装了一个AJAX,那今天我们来说说AJAX的一些基础和AJAX的状态码 首先,啥是AJAX ...

  6. JetBrains PhpStorm 2017.2 x64 激活

    使用方法:激活时选择License server 填入http://idea.imsxm.com 点击Active即可

  7. SQLServer 2005客户端远程连接sql2008 数据库服务器

    SQL2005客户端远程连接sql2008 数据库服务器 by:授客 QQ:1033553122 准备工作: 客户端所在pc机配置: 配置数据源 控制面板-管理工具-ODBC数据源-系统DSN-添加- ...

  8. DrawerLayout建立侧滑时,显示侧滑页面,底层页面仍可以有点击响应,解决办法。

    第一感觉是下层仍有焦点,解决办法应该是侧方页面出现后,下层页面的焦点改为false,应该是动态去改变焦点的状态,但是不知道如何去实现. 然后再网上找到实现方法,感谢:http://blog.csdn. ...

  9. [Android] 修图工具Draw9patch使用小结(附ubuntu快捷截图方法)

    做项目的时候,素材图遇到点问题,然后老大大概给我讲了讲android下面图片格式.9.png和draw 9-patch的用法,感觉很清楚也很有用,所以记录一下. 原文地址请保留http://www.c ...

  10. LeetCode题解之Number of Segments in a String

    1.题目描述 2.题目分析 找到字符串中的空格即可 3.代码 int countSegments(string s) { ){ ; } vector<string> v; ; i < ...