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企业应用开发实战》,电子工业出版社,陈雄华

posted @ 2017-12-17 11:14  寻找风口的猪  阅读(1306)  评论(0编辑  收藏  举报