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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的某个属性值的主要内容,如果未能解决你的问题,请参考以下文章

Spring全家桶笔记:Spring+Spring Boot+Spring Cloud+Spring MVC

学习笔记——Spring简介;Spring搭建步骤;Spring的特性;Spring中getBean三种方式;Spring中的标签

Spring--Spring入门

Spring框架--Spring事务管理和Spring事务传播行为

Spring框架--Spring事务管理和Spring事务传播行为

Spring框架--Spring JDBC