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;}}


上面的方法主要是用来希望通过一个singleton的Bean获取一个prototype Bean使用。
方法替换:

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