首页 > 代码库 > spring PropertyPathFactoryBean | 获取另一个Bean的某个属性值

spring PropertyPathFactoryBean | 获取另一个Bean的某个属性值

一般来说,spring中的Dependency来自于另一个Bean,但是也有一些情况我们需要另一个Bean的某个属性,此时可以使用PropertyPathFactoryBean;

eg:

继续次的Dog类:

package fuckSpring.propertyEditor;public class Dog {    private String name;    private String type;    private int age;        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getType() {        return type;    }    public void setType(String type) {        this.type = type;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }        public String toString(){        return this.name+this.type+this.age;    }}

再来个Person类:

package fuckSpring.propertyEditor;public class Person {    private String name;    private String hisDogName;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getHisDogName() {        return hisDogName;    }    public void setHisDogName(String hisDogName) {        this.hisDogName = hisDogName;    }    }

然后是我们的测试类,MyTest.java

package fuckSpring.propertyEditor;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {    private Person p;    public Person getP() {        return p;    }    public void setP(Person p) {        this.p = p;    }    public static void main(String[] args){        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");        MyTest mt=(MyTest) ac.getBean("myTest");        String hisDog=mt.p.getHisDogName();        System.out.println(hisDog);            }    }

最后是我们的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">]><beans>        <bean id="dog" class="fuckSpring.propertyEditor.Dog">        <property name="name" value="二狗子">        </property>        <property name="type" value="二ha">        </property>        <property name="age" value="110">        </property>    </bean>        <!-- 这里spring把dog的名字设置成一个Bean,用于被下面的Person获取 -->    <bean id="dogName" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">        <property name="targetObject">            <ref local="dog"/>        </property>                <property name="propertyPath">            <value>name</value>        </property>        </bean>        <bean id="person" class="fuckSpring.propertyEditor.Person">        <property name="hisDogName">            <ref local="dogName" />        </property>                <property name="name">            <value>tom</value>        </property>    </bean>        <bean id="myTest" class="fuckSpring.propertyEditor.MyTest">        <property name="p">            <ref local="person"/>        </property>    </bean></beans>

然后运行MyTest.java,,,BingGO!成功~

技术分享

spring PropertyPathFactoryBean | 获取另一个Bean的某个属性值