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. Proof of Elapsed Time--Hyperledger Sawtooth 共识算法

    这一片文章中我们介绍一下Hyperledger Sawtooth项目中所提出的PoET共识算法, 现有的区块链共识算法大概可以分为两种: Nakamoto consensus:通过乐透的方式选择出一个 ...

  2. Android异常分析(转)

    关于异常 异常? 异常就是一种程序中没有预料到的问题,既然是没有预料到的,就可能不在原有逻辑处理范围内,脱离了代码控制,软件可能会出现各种奇怪的现象.比如:android系统常见异常现象有应用无响应. ...

  3. while求1+2+3+4+5+6....100的和

    #求1到100的和. #声明i初始变量 i = 1 #声明y初始变量 y = 1 #开始循环条件为i不等于100,则执行while代码块 while i != 100: #给i加1 i += 1 #y ...

  4. LeetCode第[16]题(Java):3Sum Closest 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, find three integers in S such that the sum is closes ...

  5. [转]python json.dumps 中的ensure_ascii 参数引起的中文编码

    本文转自: 梁小白博客(http://biangbiang.cnblogs.com) 在使用json.dumps时要注意一个问题 >>> import json >>&g ...

  6. [整理]Linux Crontab命令总结

    一.crontab命令的基本介绍 crontab命令常见于Unix和类Unix的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于"crontab" ...

  7. C++拷贝构造函数(深拷贝与浅拷贝)

    转自http://blog.csdn.net/lwbeyond/article/details/6202256/ 一. 什么是拷贝构造函数 对于普通类型的对象来说,它们之间的复制是很简单的,例如:in ...

  8. github免密登陆

    import requests import re # 一:先获取登陆页面,拿到authenticity_token: # 1 请求的url:https://github.com/login # 2 ...

  9. (亲测)1分钟破解IDM权限,傻瓜都能学会的破解方法(番外篇:利用破解工具直接破解IDM)

    Internet Download Manager (IDM)是比较好用的一款下载工具~ 上一节我讲到一种利用修改防火墙来进行阻止访问服务器,请参看这篇文章(亲测)躺着破解IDM下载权限,治疗不用破解 ...

  10. NYoj_104最大和

    最大和 时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描述 给定一个由整数组成二维矩阵(r*c),现在需要找出它的一个子矩阵,使得这个子矩阵内的所有元素之和最大,并把这个子矩 ...