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常用依赖汇总
Routes 路由配置 Router 路由跳转 ActivatedRoute 路由参数 FormsModule 表单配置(在app.module.ts中注入在imports下) EventEmitte ...
- vue-父子组件嵌套的示例
组件注册: // 注册组件 Vue.component('my-component', { template: '<div>A custom component!</div>' ...
- Mac appium.dmg. Xcode Command Line Tools
You need to install the command line tools as marked in your message: ✖ Xcode Command Line Tools are ...
- 【数论】洛谷P1372又是毕业季
题目背景 "叮铃铃铃",随着高考最后一科结考铃声的敲响,三年青春时光顿时凝固于此刻.毕业的欣喜怎敌那离别的不舍,憧憬着未来仍毋忘逝去的歌.1000多个日夜的欢笑和泪水,全凝聚在毕业 ...
- CSS背景-background
复合属性-background 如果同时设置了background-color和background-image时,背景颜色会被图片覆盖. background-image: 用作背景的图片,back ...
- [转] Web 开发模式演变历史和趋势
文章转自梦想天空--前端文摘:Web 开发模式演变历史和趋势 一.简单明快的早期时代 可称之为 Web 1.0 时代,非常适合创业型小项目,不分前后端,经常 3-5 人搞定所有开发.页面由 JSP.P ...
- 嵌入式Tomcat容器的参数(maxParameterCount)设定
背景 昨天同事遇到了error一起看了一下感觉比较重要在这记录一下 基本情况是页面上选中9K+的数据向后台发送请求,然后系统就崩了... error信息如下 More than the maximum ...
- python基本数据类型学习
python是极其简洁的一门高级语言,在python里面没有真正意义上的常量,只是用大写的标定表示常量(python中的常量是可以修改的),单行注释用#开始,.并且python不用定义数据类型,因为p ...
- ssm工程集成mybatis分页插件pagehelper
1 首先需要在mybatis的配置文件SqlMapConfig.xml文件中配置pagehelper插件 <plugins> <plugin interceptor=" ...
- 激活IDEA
1 先跳过提示 2 点击regist 3 输入注册码 : 我的注册码在这里找的 - >> http://hw1287789687.iteye.com/blog/2153894