lookup method注入是spring动态改变bean里方法的实现。方法执行返回的对象,使用spring内原有的这类对象替换,通过改变方法返回值来动态改变方法。内部实现为使用cglib方法,重新生成子类,重写配置的方法和返回对象,达到动态改变的效果。 
 
     实例如下:
package fiona.apple;

// no more Spring imports!

public abstract class CommandManager {

    public Object process(Object commandState) {
// grab a new instance of the appropriate Command interface
Command command = createCommand();
// set the state on the (hopefully brand new) Command instance
command.setState(commandState);
return command.execute();
} // okay... but where is the implementation of this method?
protected abstract Command createCommand();
}
配置:
<!-- a stateful bean deployed as a prototype (non-singleton) -->
<bean id="command" class="fiona.apple.AsyncCommand" scope="prototype">
<!-- inject dependencies here as required -->
</bean> <!-- commandProcessor uses statefulCommandHelper -->
<bean id="commandManager" class="fiona.apple.CommandManager">
<lookup-method name="createCommand" bean="command"/>
</bean>
注意:由于采用cglib生成之类的方式,所以需要用来动态注入的类,不能是final修饰的;需要动态注入的方法,也不能是final修饰的。
 
同时,还得注意command的scope的配置,如果scope配置为singleton,则每次调用方法createCommand,返回的对象都是相同的;如果scope配置为prototype,则每次调用,返回都不同。
 
 

spring lookup method 注入的更多相关文章

  1. spring replaced method 注入

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

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

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

  3. Spring中的注入方式 和使用的注解 详解

    注解:http://www.cnblogs.com/liangxiaofeng/p/6390868.html 注入方式:http://www.cnblogs.com/java-class/p/4727 ...

  4. 在ABAP里模拟实现Java Spring的依赖注入

    Dependency Injection- 依赖注入,在Java Spring框架中有着广泛地应用.通过依赖注入,我们不必在应用代码里繁琐地初始化依赖的资源,非常方便. 那么ABAP能否从语言层面上也 ...

  5. spring中构造函数注入

    spring中构造函数注入,简单来说,就是通过beans.xml中,设置对应的值.而且通过bean类中的构造函数进行注入这些值. 文件结构 watermark/2/text/aHR0cDovL2Jsb ...

  6. Spring系列.依赖注入配置

    依赖注入的配置 Spring的依赖注入分为基于构造函数的依赖注入和基于setter方法的依赖注入. 基于构造函数的依赖注入 <!-- 通过构造器参数索引方式依赖注入 --> <bea ...

  7. (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)

    Spring要把xml配置中bean的属性实例化为具体的bean,"依赖注入"是关卡.所谓的"依赖注入",就是把应用程序对bean的属性依赖都注入到spring ...

  8. Spring的依赖注入(DI)三种方式

    Spring依赖注入(DI)的三种方式,分别为: 1.  接口注入 2.  Setter方法注入 3.  构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...

  9. spring的依赖注入的最常见的两种方法

    package com.lsz.spring.action; public class User { /** * set注入 */ private String username; public vo ...

随机推荐

  1. 最新 东方网java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.东方网等10家互联网公司的校招Offer,因为某些自身原因最终选择了东方网.6.7月主要是做系统复习.项目复盘.LeetCo ...

  2. Docker准备

    1. 引言 Docker是目前最流行的容器技术,是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.是开发人员和系统管理员使用容器开发,部署和运行应用程序的平台.也许我们 ...

  3. Linux中查找最耗CPU的Java代码问题

    第一步: 查看消耗CPU最高的进程PID [lolaage@web2 tomcat-ns]$ top top - 13:23:32 up 42 days, 19:11,  3 users,  load ...

  4. db2 数据库配置HADR+TSA添加集群节点

    Db2配置HADR高可用+TSA添加集群节点 一.服务器资源 Master IP:10.78.10.1 数据库:dbclassSlave IP:10.78.10.2 数据库:dbclassVIP:10 ...

  5. Centos安装 k8s

    ### 一. 安装docker 1.安装依赖包```shellsudo yum install -y yum-utils device-mapper-persistent-data lvm2 ``` ...

  6. 汉字在unicode中的位置

    在www.unicode.org中查找汉字.china找不到,后来查资料才明白,应该查CJK,为什么内? unicode这个组织吧中国日本韩国的字合并了   中日韩统一表意文字(CJK Unified ...

  7. golang使用注意事项

    1.可以给类型取别名,但是该类型和别名是两个不同的类型: type myInt int 2.go支持可变参数:args... 0个或多个参数:func sum(args... int) sum int ...

  8. java实现带过期时间的缓存

    private static ScheduledExecutorService swapExpiredPool = new ScheduledThreadPoolExecutor(10); priva ...

  9. TCP协议和UDP协议的对比【转】

    原文:https://blog.csdn.net/lzj2504476514/article/details/81454754 一.TCP协议的主要特点(1)TCP是面向连接的运输层协议:(2)每一条 ...

  10. Redis 测试 数据类型