007. Spring 方法替换

来源:互联网 发布:java链表删除 编辑:程序博客网 时间:2024/06/11 04:23

1、创建Java项目:File -> New -> Java Project

2、引入必要jar包,项目结构如下
这里写图片描述

3、创建Animal实体类Animal.java

package com.spring.model;public class Animal {    private String species = null;    public Animal() {        super();    }    public Animal(String species) {        super();        this.species = species;    }    public String getSpecies() {        return species;    }    public void setSpecies(String species) {        this.species = species;    }    @Override    public String toString() {        return "Animal [species=" + species + "]";    }}

4、创建People实体类People.java

package com.spring.model;import java.lang.reflect.Method;import org.springframework.beans.factory.support.MethodReplacer;public class People implements MethodReplacer {    private int id;    private String name;    private Animal animal = null;    public People() {        super();    }    public People(int id, String name, Animal animal) {        super();        this.id = id;        this.name = name;        this.animal = animal;    }    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;    }    public Animal getAnimal() {        return animal;    }    public void setAnimal(Animal animal) {        this.animal = animal;    }    @Override    public String toString() {        return "People [id=" + id + ", name=" + name + ", animal=" + animal + "]";    }    @Override    public Object reimplement(Object arg0, Method arg1, Object[] arg2) throws Throwable {        // 替换函数        return this.getAnimal();    }}

5、创建spring配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd">    <bean id="peopleHaveCat" class="com.spring.model.People">        <property name="id" value="0"></property>        <property name="name" value="peopleHaveCat"></property>        <property name="animal">            <bean class="com.spring.model.Animal">                <property name="species" value="cat"></property>            </bean>        </property>        <replaced-method name="getAnimal" replacer="peopleHaveDog"></replaced-method>    </bean>    <bean id="peopleHaveDog" class="com.spring.model.People">        <property name="id" value="0"></property>        <property name="name" value="peopleHaveDog"></property>        <property name="animal">            <bean class="com.spring.model.Animal">                <property name="species" value="dog"></property>            </bean>        </property>    </bean></beans>

6、创建Spring测试类SpringUnit.java

package com.spring.junit;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.spring.model.People;public class SpringUnit {    ClassPathXmlApplicationContext ctx = null;    @Before    public void setUp() throws Exception {        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");    }    @Test    public void test() {        People people = (People) ctx.getBean("peopleHaveCat");        System.out.println(people.getAnimal());        System.out.println(people.toString());    }    @After    public void tearDown() throws Exception {        ctx.close();    }}

7、测试结果

... 省略Spring日志信息 ...Animal [species=dog]People [id=0, name=peopleHaveCat, animal=Animal [species=cat]]... 省略Spring日志信息 ...
阅读全文
0 0