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

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

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

public abstract class UserService {

    public abstract WalletService createWalletService() ;
public UserService() {
System.out.println("UserService 正在实例化!");
} public void login(String userName, String passWord) {
createWalletService().run();
System.out.println(userName + "正在登陆!");
}
}

  

public  class WalletService {
public WalletService(){
System.out.println("CarService");
}
public void run() {
System.out.println("this = " + this);
}
}

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

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

测试代码:

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

  

测试输出:

daxin正在登陆!
CarService
this = com.daxin.service.WalletService@40e6dfe1
daxin正在登陆!
CarService
this = com.daxin.service.WalletService@1b083826
daxin正在登陆!
CarService
this = com.daxin.service.WalletService@105fece7
daxin正在登陆!
CarService
this = com.daxin.service.WalletService@3ec300f1
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. 乱糟unity整理

    当Canvas上的UI元素变化时,会重新生成网格并向GPU发起绘图调用,从而显示UI.划分画布:1.每块画布上的元素都与其他画布的元素相隔离,使用?工具来切分画布?,从而解决ui的批处理问题.2.也可 ...

  2. leetcode树专题894.897,919,951

    满二叉树是一类二叉树,其中每个结点恰好有 0 或 2 个子结点. 返回包含 N 个结点的所有可能满二叉树的列表. 答案的每个元素都是一个可能树的根结点. 答案中每个树的每个结点都必须有 node.va ...

  3. Oracle总结之plsql编程(基础八)

    原创作品,转自请注明出处:https://www.cnblogs.com/sunshine5683/p/10328524.html 一.函数 1.函数是可以返回一个特定的数据,函数的创建中必须包含re ...

  4. Java - 生产者消费者问题

    Java多线程系列--“基础篇”11之 生产消费者问题 概要 本章,会对“生产/消费者问题”进行讨论.涉及到的内容包括:1. 生产/消费者模型2. 生产/消费者实现 转载请注明出处:http://ww ...

  5. 4.Factory Pattern(工厂模式)

    工厂模式(Factory Pattern)定义: 定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个.工厂方法让类把实例化推迟到子类. 针对实现编程,但是当我们每次使用new时候,不正是在针对 ...

  6. JS中冒泡排序,选择排序,快速排序

        var arr = [1,4,2,9,7,6,5,4,7,5];     // 冒泡排序(通俗的说就是j 和 j+1打,谁赢了谁去后面)       for(var i = 1;i<ar ...

  7. Component Interface相关面试题

    下面的列表中的问题很常见,Component Interface是很重要的.你知道基本知识比知道答案更重要. Q:以下陈述是错误的. A:一个Component Interface 可以map多个Pe ...

  8. ArcGIS三种方式打断相交线------Planarize Lines工具

    1. 只有一个layer图层时,我们只需要选择”Planarize Lines“工具即可. (1)选择工具栏”Customize“选项: (2)选择Customize工具栏中的”Toolbars“选项 ...

  9. MySQL之UNDO及MVCC、崩溃恢复

      UNDO特性:避免脏读.事务回滚.非阻塞读.MVCC.崩溃恢复 事务工作流程(图2) MVCC原理机制 崩溃恢复:redo前滚.undo回滚 长事务.大事务:危害.判断.处理 UNDO优化:实现u ...

  10. Redis缓存穿透、缓存雪崩、redis并发问题分析

    把redis作为缓存使用已经是司空见惯,但是使用redis后也可能会碰到一系列的问题,尤其是数据量很大的时候,经典的几个问题如下: (一)缓存和数据库间数据一致性问题分布式环境下(单机就不用说了)非常 ...