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对象的实例化. 创建应用对象之间的协作关系的行为称为:装配( ...
随机推荐
- 初用MssqlOnLinux 【1】
https://docs.microsoft.com/zh-cn/sql/linux/quickstart-install-connect-red-hat 使用 Centos7,NetCore2.0, ...
- 【转】Mac端包管理工具——Homebrew简介及安装
Homebrew官网 http://brew.sh/index_zh-cn.html Homebrew是神马 linux系统有个让人蛋疼的通病,软件包依赖,好在当前主流的两大发行版本都自带了解决方案, ...
- mysql foreign key(外键) 说明与实例
一,什么是foreign key,及其完整性 个人觉得,foreign key就是表与表之间的某种约定的关系,由于这种关系的存在,我们能够让表与表之间的数据,更加的完整,关连性更强.关于完整性,关连性 ...
- mydumper
Mydumper介绍 Mydumper是一个针对MySQL和Drizzle的高性能多线程备份和恢复工具.开发人员主要来自MySQL,Facebook,SkySQL公司.目前已经在一些线上使用了Mydu ...
- 关于 AutomationProperties.Name 的一些总结
在 XAML 代码中,我们偶尔会看到 AutomationProperies 的代码,如 AutomationProperties.Name="xxxxx", Automation ...
- cookie/session(过时的写法)
cookie存在客户端的浏览器中,不太安全,容易被窃取,,session被存在服务器中(类似于字典中的value,),服务器会给浏览器返回这个value的key值,下次进来直接根据key取value. ...
- Matlab与C++混合编程(依赖OpenCV)
Matlab与C++混合编程实际上就是通过Matlab的Mex工具将C++的代码编译成Matlab支持调用的可执行文件和函数接口.这样一方面可以在Matlab中利用已经编写好的函数,尽管这个函数是用C ...
- 前端 IoC 理念入门
背景 近几年,前端应用(WebApp)正朝着大规模方向发展,在这个过程中我们会对项目拆解成多个模块/组件来组合使用,以此提高我们代码的复用性,最终提高研发效率. 在编写一个复杂组件的时候,总会依赖其他 ...
- BC#64 4.Tree
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5589 对于u,v的xor和就是u到根的xor和 xor上 v到根的xor和.看到n<=5w,考虑莫队 ...
- Linux使用Public Key方式远程登录
一.前言: ssh远程登录密码认证的方式有三种,password.Keyboard Interactive.Public Key 前面两种方式就是密码认证,含义都是一样大同小异.第三种是登录方式最安全 ...