lookup-method

示例:

步骤一:定义一个Car类

package org.hope.spring.bean.lookup;

public class Car {
private String brand;
private String corp;
private double price; getter()&setter()....
}

步骤二:定义一个Boss接口

package org.hope.spring.bean.lookup;

public interface Boss {
Car haveCar();
}

步骤三:在spring的配置文件bean.xml中定义三个bean

<bean id="honeqi" class="org.hope.spring.bean.lookup.Car"
p:brand="红旗"
p:price="400000"
scope="prototype" /> <bean id="bmw" class="org.hope.spring.bean.lookup.Car"
p:brand="奔驰GLC260"
p:price="500000"
scope="prototype" /> <bean id="boss" class="org.hope.spring.bean.lookup.Boss">
<lookup-method name="haveCar" bean="bmw"/>
</bean>

步骤四:写单元测试测试

package org.hope.spring.bean.lookup;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:bean.xml"})
public class LookUpTest {
@Autowired
private Boss boss; @Test
public void testBoss() {
Car car = boss.haveCar();
System.out.println(car.getBrand());
}
}

输出:

奔驰GLC260

结论:

  1、通过lookup-method元素标签可以为Boss的haveCar()提供动态实现,返回prototype类型的car Bean。

  2、lookup方法注入是有一定使用范围的,一般在希望通过一个singleton Bean获取一个prototypeBean时使用

  3、lookup方法注入需要用到CGLib类包


replaced-method(方法替换)

  可以再运行时用新的方法替换久的方法

步骤一:新建一个Player.java

package org.hope.spring.bean.model;

public class Player {
private int id;
private String name; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "Player{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}

步骤二:新建一个将要被替换的类

package org.hope.spring.bean.replacedmethod;

import org.hope.spring.bean.model.Car;
import org.hope.spring.bean.model.Player; public class YiJianLian {
public Player onPlace() {
Player player = new Player();
player.setName("易建联");
return player;
}
}

步骤三:新建一个替换类,实现 MethodReplacer

package org.hope.spring.bean.replacedmethod;

import org.hope.spring.bean.model.Player;
import org.springframework.beans.factory.support.MethodReplacer; import java.lang.reflect.Method; /**
*  @Description: 用于替换他人的bean,必须实现MethodReplacer
*/
public class Yao implements MethodReplacer {
public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
Player player = new Player();
player.setName("姚明");
return player;
}
}

步骤四:在bean2.xml中配置bean关系

<bean id="yao" class="org.hope.spring.bean.replacedmethod.Yao">
</bean>
<bean id="yiJianLian" class="org.hope.spring.bean.replacedmethod.YiJianLian">
<replaced-method name="onPlace" replacer="yao"/>
</bean>

步骤五:测试

package org.hope.spring.bean.replacedmethod;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:bean2.xml"})
public class ReplacedTest {
@Autowired
private YiJianLian yiJianLian; @Test
public void test() {
System.out.println(yiJianLian.onPlace());
}
}

输出:

Player{id=0, name='姚明'}

结论:

  1、可以在运行时用新的方法替换旧的方法。

  2、用于替换他人的Bean必须实现MethodReplacer接口。

参考:

[1] 《精通Spring4.x企业应用开发实战》,电子工业出版社,陈雄华

spring bean中子元素lookup-method和replaced-method的更多相关文章

  1. spring replaced method 注入

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

  2. Spring bean中的properties元素内的name 和 ref都代表什么意思啊?

    <bean id="userAction" class="com.neusoft.gmsbs.gms.user.action.UserAction" sc ...

  3. spring bean属性及子元素使用总结

    spring bean属性及子元素使用总结 2016-08-03 00:00 97人阅读 评论(0) 收藏 举报  分类: Spring&SpringMVC(17)  版权声明:本文为博主原创 ...

  4. spring bean中的properties元素内的ref和value的区别;* 和 ** 的区别

    spring bean中的properties元素内的ref和value的区别 至于使用哪个是依据你所用的属性类型决定的. <bean id="sqlSessionFactory&qu ...

  5. 0003 - 基于xml的Spring Bean 的创建过程

    一.目录 前言 创建 Bean 容器 加载 Bean 定义 创建 Bean Spring Bean 创建过程中的设计模式 总结 二.前言 2.1 Spring 使用配置 ApplicationCont ...

  6. Spring JMX之一:使用JMX管理Spring Bean

    spring中关于jmx包括几个概念: MBeanExporter: 从字面上很容易理解, 用来将一些spring的bean作为MBean暴露给MBEanServer.MBeanServerFacto ...

  7. 第20章-使用JMX管理Spring Bean

    Spring对DI的支持是通过在应用中配置bean属性,这是一种非常不错的方法.不过,一旦应用已经部署并且正在运行,单独使用DI并不能帮助我们改变应用的配置.假设我们希望深入了解正在运行的应用并要在运 ...

  8. Spring bean的bean的三种实例化方式

     Bean 定义 被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的.bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象.这些 bean ...

  9. Spring Bean详细讲解

    什么是Bean? Spring Bean是被实例的,组装的及被Spring 容器管理的Java对象. Spring 容器会自动完成@bean对象的实例化. 创建应用对象之间的协作关系的行为称为:装配( ...

随机推荐

  1. Wechat 微信端正确播放audio、video的姿势

    在开发微信项目时,有在项目中播放音频(audio)和视频(video)的需求: 在开发中,我们会遇到的问题 audio.video在Android和IOS系统上的兼容性: video播放完成后,跳出浏 ...

  2. QCanvasItem介绍-QT3

    QCanvasItem类提供一个在QCanvas上的抽象图形对象. 各种QCanvasItem子类提供立即可用的行为.这个类是一个纯粹的抽象超类,它提供了在所有具体的canvas项目类中共享的行为.Q ...

  3. 小白的Python之路 day5 shutil模块

    shutil模块 一.主要用途 高级的文件.文件夹.压缩包 等处理模块 二.常用方法详解 1.shutil.copyfileobj(fsrc, fdst) 功能:把一个文件的内容拷贝到另外一个文件中, ...

  4. 【树链剖分】洛谷P3384树剖模板

    题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节点的值都加上z 操作2: 格式 ...

  5. Centos7(Linux)网络配置,自动获取ip地址

    Centos7.0 Vmware 网络桥接配置,利用DHCP自动获取ip地址 首先要将Vmware10.0.3设置为桥接模式. CentOS 7.0默认安装好之后是没有自动开启网络连接的! cd  / ...

  6. 【读书笔记】【深入理解ES6】#13-用模块封装代码

    什么是模块 模块是自动运行在严格模式下并且没有办法退出运行的 JavaScript 代码. 在模块顶部创建的变量不会自动被添加到全局变量作用域,这个变量仅在模块的顶级作用域中存在,而且模块必须导出一些 ...

  7. SP3精密星历简介

    IGS精密星历采用sp3格式,其存储方式为ASCII文本文件,内容包括表头信息以及文件体,文件体中每隔15 min给出1个卫星的位置,有时还给出卫星的速度.它的特点就是提供卫星精确的轨道位置.采样率为 ...

  8. Jeecg踩坑不完全指南

    公司用了这个叫做jeecg的快速开发框架,我不知道有多少公司在用这个框架,园子里有的可以吱一声.个人觉得这框架唯一优势就是可以让不会ssh的人也能进行开发,只要你会J2SE,有web后台发开经验即可. ...

  9. VFS四大对象之四-struct file

    继上一篇文章: http://www.cnblogs.com/linhaostudy/p/7428971.html 四.file结构体 文件对象:注意文件对象描述的是进程已经打开的文件.因为一个文件可 ...

  10. Android基础_一次上传多张图片

    获取权限 <uses-permission android:name="android.permission.CAMERA"/> <uses-permission ...