Spring笔记(7)------方法注入
来源:互联网 发布:在家当淘宝客服靠谱吗 编辑:程序博客网 时间:2024/04/20 10:06
无状态的Bean的作用域默认是singleton单例模式,如果我们往singleton的Boss类中注入prototype的Car,并希望每次调用Bean的getCar()方法时都能返回一个新的Car实例,使用传统的注入时不行的,因为singleton的Bean注入关联Bean的动作仅发生一次,虽然Car Bean作用域是prototype类型,但Boss通过getCar()返回对象还是最开始注入的那个Car Bean。下面是实现方案:
使用lookup方法注入:
Spring IoC容器拥有复写Bean方法的能力,主要是因为cglib类包。CGlib可以在运行期动态操作Class字节码,为Bean动态创建子类或实现类,先定义一个MagicBoss接口:
public interface MagicBoss {Car getCar();}
然后我们不编写实现类,仅通过配置为该接口提供动态的实现,让getCar()接口方法每次都返回新的Car实例。
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-3.0.xsd"><bean id="car" class="com.baobaobao.Car" p:brand="奥迪" p:price="200000" scope="prototype" /><bean id="magicBoss" class="com.baobaobao.MagicBoss"><lookup-method name="getCar" bean="car"/></bean></beans>通过lookup-method元素标签为MagicBoss的getCar()提供动态实现,返回prototype的car Bean,这样,Spring为MagicBoss接口提供了动态实现,其效果等同于如下:
package com.baobaobao;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;public class MagicBossImpl implements MagicBoss,ApplicationContextAware{private ApplicationContext ac;public Car getCar() {// TODO Auto-generated method stubreturn (Car) ac.getBean("car");}public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {this.ac = applicationContext;}}
方法替换:
Spring容器还可以使用一个Bean的方法去替换另一个Bean的方法,使用方法如下:
先写一个Boss1类:
package com.baobaobao;public class Boss1 {public Car getCar(){Car car = new Car();car.setBrand("宝马Z4");return car;}}
再写一个Boss2类,用boss2的方法替换Boss1的,用于替换他人的Bean需要实现MethodReplacer:
public class Boss2 implements MethodReplacer {public Object reimplement(Object obj, Method method, Object[] args)throws Throwable {// TODO Auto-generated method stubCar car = new Car();car.setBrand("奥迪");return car;}}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-3.0.xsd"><bean id="boss1" class="com.baobaobao.Boss1"><replaced-method name="getCar" replacer="boss2" /></bean><bean id="boss2" class="com.baobaobao.Boss2" /></beans>
这篇文章介绍的这俩IOC容器的用处较少,因为书中写了,就记下来~~~
0 0
- Spring笔记(7)------方法注入
- Spring学习笔记(10)-----------方法注入
- 【Spring笔记】(二)集合注入、注入的方法
- Spring学习笔记(10)——方法注入
- Spring学习笔记—常用注入方法
- Spring学习笔记(4.Spring注入)
- 【Spring】--Spring方法注入
- 【Spring】--Spring方法注入
- spring学习笔记(5)--集合注入
- Spring学习笔记(1)---依赖注入
- Spring笔记(5)------依赖注入
- Spring笔记(6)-----------注入参数详解
- Spring学习笔记(8)-----------依赖注入
- Spring学习笔记(9)-----------注入参数
- Spring 学习笔记(1)注入Injection_type
- Spring学习笔记(三)依赖注入
- Spring学习笔记(四)注入参数
- 笔记-spring 注入
- spring quarz 整合
- 腾讯笔试题
- 并行计算学习笔记1
- C# Winform 支持Hex与ASCII输入和切换的文本框
- hdu 1847 Good Luck in CET-4 Everybody!
- Spring笔记(7)------方法注入
- Jboss+Mysql服务器启动是报错
- J2EE常见bug
- android设计模式--外观模式
- Introduction to New Renderer
- POJ训练计划1177_Picture(扫描线/线段树+离散)
- MYSQL dos命令大全
- C++,VS创建公共类
- 双向链表