spring bean中子元素lookup-method和replaced-method
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的更多相关文章
- spring replaced method 注入
replaced method注入是spring动态改变bean里方法的实现.需要改变的方法,使用spring内原有其他类(需要继承接口org.springframework.beans ...
- Spring bean中的properties元素内的name 和 ref都代表什么意思啊?
<bean id="userAction" class="com.neusoft.gmsbs.gms.user.action.UserAction" sc ...
- spring bean属性及子元素使用总结
spring bean属性及子元素使用总结 2016-08-03 00:00 97人阅读 评论(0) 收藏 举报 分类: Spring&SpringMVC(17) 版权声明:本文为博主原创 ...
- spring bean中的properties元素内的ref和value的区别;* 和 ** 的区别
spring bean中的properties元素内的ref和value的区别 至于使用哪个是依据你所用的属性类型决定的. <bean id="sqlSessionFactory&qu ...
- 0003 - 基于xml的Spring Bean 的创建过程
一.目录 前言 创建 Bean 容器 加载 Bean 定义 创建 Bean Spring Bean 创建过程中的设计模式 总结 二.前言 2.1 Spring 使用配置 ApplicationCont ...
- Spring JMX之一:使用JMX管理Spring Bean
spring中关于jmx包括几个概念: MBeanExporter: 从字面上很容易理解, 用来将一些spring的bean作为MBean暴露给MBEanServer.MBeanServerFacto ...
- 第20章-使用JMX管理Spring Bean
Spring对DI的支持是通过在应用中配置bean属性,这是一种非常不错的方法.不过,一旦应用已经部署并且正在运行,单独使用DI并不能帮助我们改变应用的配置.假设我们希望深入了解正在运行的应用并要在运 ...
- Spring bean的bean的三种实例化方式
Bean 定义 被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的.bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象.这些 bean ...
- Spring Bean详细讲解
什么是Bean? Spring Bean是被实例的,组装的及被Spring 容器管理的Java对象. Spring 容器会自动完成@bean对象的实例化. 创建应用对象之间的协作关系的行为称为:装配( ...
随机推荐
- angular4.0运行在微信端的坑坑洼洼
最近的一个项目,我用ng4操刀,踩了超多的坑: 坑1:项目build后,刷新后404错误: 解决方案:<angular4.0项目build发布后,刷新页面报错404> 坑2:微信分享: 运 ...
- css布局--水平垂直居中
1. 使用text-align 和 vertical-align 和 inline-block实现水平垂直居中 html <div class="parent"> &l ...
- python3之内置函数
1.abs() 取数字的绝对值 >>> print(abs(-28)) 28 >>> print(abs(-2.34)) 2.34 >>> pri ...
- C函数原理
C语言作为面向过程的语言,函数是其中最重要的部分,同时函数也是C种的一个难点,这篇文章希望通过汇编的方式说明函数的实现原理. 栈结构与相关的寄存器 在计算中,栈是十分重要的一种数据结构,同时也是CPU ...
- AI时代:推荐引擎正在塑造人类
We shape our tools and afterwards our tools shape us. ------Marshall McLuhan 麦克卢汉说:"我们塑造了工具,反过来 ...
- 【转载】在Linux下,一个文件也有三种时间,分别是:访问时间、修改时间、状态改动时间
在windows下,一个文件有:创建时间.修改时间.访问时间.而在Linux下,一个文件也有三种时间,分别是:访问时间.修改时间.状态改动时间. 两者有此不同,在Linux下没有创建时间的概念,也就是 ...
- (7拾遗)从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练
从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练 1综述http://www.cnblogs.com/jsxyhelu/p/7907241.html2环境架设http://www.cn ...
- file中private_data
这个是Linux下连接VFS文件系统框架和不同文件/文件系统底层实现之间的一个核心数据结构,虽然它只是一个指针,但是一个指针可以解决所有问题,有了它,妈妈再也不用担心我的学习.我们回想一下用户态线程的 ...
- MySQL中MyISAM 和 InnoDB 的基本区别
有以下5点 1.innodb支持事务,myisam不支持 2.innodb支持mvcc,myisam不支持 3.innodb支持外键,myisam不支持 4.innodb不支持 FULLTEXT类型的 ...
- Effective Java 第三版——21. 为后代设计接口
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...