对于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. JS DOM操作(三) Window.docunment对象——操作属性

    属性:是对象的性质与对象之间关系的统称.HTML中标签可以拥有属性,属性为 HTML 元素提供附加信. 属性总是以名称/值对的形式出现,比如:name="value". 属性值始终 ...

  2. IDEA@Data注释使用

    @Data注解主要是帮助解决Setter 和 Getter以及 toString这种重复的无脑工作 加入@Data注解可以直接帮助我们添加实体类相应的Setter 和 Getter以及 toStrin ...

  3. 【ZeroMQ】1、ZeroMQ(java)入门之Requerst/Response模式

    自己最开始是在cloud foundry中接触过消息服务器(nats),或者说是消息中间件,也算是初步知道了一个消息服务器对于分布式的网络系统的重要性,后来自己也曾想过在一些项目中使用它,尤其是在一些 ...

  4. Proud Merchants(01背包变形)hdu3466

    I - Proud Merchants Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  5. POJ1611(KB2-B)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 39211   Accepted: 18981 De ...

  6. POJ3468(KB7-C 线段树)

    A Simple Problem with Integers Time Limit: 5000MS  Memory Limit: 131072K Total Submissions: 108903   ...

  7. Linux 安装mysql,mariadb,mysql主从同步

    myariadb安装 centos7 mariadb的学习 在企业里面,多半不会使用阿里云的mariadb版本,因为版本太低,安全性太低,公司会配置myariadb官方的yum仓库 1.手动创建mar ...

  8. python深拷贝浅拷贝

    python深拷贝和浅拷贝问题: 什么是深拷贝? (个人理解)深拷贝就是将原有的数据一模一样的拷贝一份,然后存到另一个地址中,而不是引用地址 什么是浅拷贝? (个人理解)就是引用地址 (1)用等于号的 ...

  9. java.lang.IllegalArgumentException: findUserById is ambiguous in Mapped Statements collection

    这是由于mybatis的mapper  xml文件中的id 名字和mybatis内置的方法可能有冲突导致的,  更改xml 的id  名字就可以了!

  10. git 报错:error: failed to push some refs to 'https://github.com/Anderson-An/******.git'(已解决)

    提交push 报错: $ git push origin masterTo https://github.com/Anderson-An/******.git ! [rejected] master ...