Spring中如何使用PropertyPathFactoryBean进行属性值注入呢?

戚薇 Spring 发布时间:2023-06-26 17:25:23 阅读数:6039 1
在日常开发中,我们经常使用bean实例的属性值直接赋值给一个变量的方法,那么如何实现这一需求呢?
使用PropertyPathFactoryBean获取目标bean的属性
  然后将获得的值注入到其他bean中
例:
实体类

public class Person {
    private Son son;
    private String age;
    
    public Son getSon() {
        return son;
    }
    
    public void setSon(Son son) {
        this.son = son;
    }
    
    public String getAge() {
        return age;
    }
    
    public void setAge(String age) {
        this.age = age;
    }
}

public class Son {
    private String age;
 
    public String getAge() {
        return age;
    }
 
    public void setAge(String age) {
        this.age = age;
    }
}

配置文件
提供四种注入

<?xml version="1.0" encoding="GB2312"?>
<beans default-autowire="byName"
	xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/util http://localhost:8080/schema/www.springframework.org/schema/util/spring-util-2.0.xsd ">
 
 <bean id="person" class="com.java265.factorybean.Person" scope="prototype">
     <property name="age">
        <value>30</value>
     </property>
     <property name="son">
        <bean class="com.java265.factorybean.Son">
           <property name="age">
              <value>16</value>
           </property>
        </bean>
     </property>
  </bean>
  
  <!--如下将会将person的属性son的属性age传入son1实例的age属性-->
    <bean id="son1" class="com.java265.factorybean.Son">
        <property name="age">
          <!--以下是访问bean属性的简单方式,这样可以将person这个bean的age属性赋值给son1这个bean的age属性-->           
         <bean id="person.son.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
        </property>
    </bean>
    
    <!-- 以下将会获得结果son,它将是person bean的son的数值-->
    <bean id="son2" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
       <property name="targetBeanName">
         <value>person</value>
       </property>
       <property name="propertyPath">
         <value>son</value>
       </property>
    </bean>
    
     <!-- 以下将会获得结果16,它将是person bean的son的age属性-->
    <bean id="son3" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
       <property name="targetBeanName">
         <value>person</value>
       </property>
       <property name="propertyPath">
         <value>son.age</value>
       </property>
    </bean>
    
    <!-- 以下会获得结果为30 ,它将是获得该bean的内部bean的age属性-->
    <bean id="son4" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
        <property name="targetObject">
            <bean class="com.java265.factorybean.Person">
                <property name="age"><value>30</value></property>
            </bean>
        </property>
        <property name="propertyPath"><value>age</value></property>
    </bean>
</beans>

测试代码

public class PropertyPathTest {
    public static void main(String[] args) {
        ApplicationContext context 
		= new ClassPathXmlApplicationContext("com/java265/factorybean/propertyPathFactoryBean.xml");
        Son son1 = (Son) context.getBean("son1");
        Son son2 = (Son) context.getBean("son2");
 
        System.out.println("person age is:" + son1.getAge());
        System.out.println("person age is:" + son2.getAge());
        System.out.println(context.getBean("son3"));
        System.out.println(context.getBean("son4"));
    }
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/Spring/202306/6903.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者