Spring(20) 获取别的bean的属性

本文介绍如何在Spring框架中使用PropertyPathFactoryBean进行属性注入,通过实例演示从一个bean获取属性并赋值给另一个bean的过程,重点讲解配置文件的设置与代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 嗯。。。就是在配置文件中一个bean把别人的属性拿过来用
  2. 通过将class指定为PropertyFactoryBean来实现获取其他bean的属性
  3. 在代码里边说吧,总而言之呢就是把class指定为ProperFactory,然后再指定是要获取那个bean的哪个属性
    package InstancePackage;
    
    public class Person {
        int age;
    
        Son son;
    
        public Son getSon() {
            return son;
        }
    
        public void setSon(Son son) {
            this.son = son;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    
    package InstancePackage;
    
    public class Son {
        int age;
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    
    package TestPackage;
    
    import InstancePackage.Person;
    import InstancePackage.Son;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class SpringTest {
        public static void main(String []args){
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
            Son son = applicationContext.getBean("son1", Son.class);
            System.out.println(son.getAge());
    
    
            Son son1 = applicationContext.getBean("son2", Son.class);
            System.out.println(son1.getAge());
    
            int  age1 = applicationContext.getBean("theAge", Integer.class);
            System.out.println(age1);
    
            int age2 = applicationContext.getBean("theAge2", Integer.class);
            System.out.println(age2);
        }
    }
    
    <?xml version="1.0" encoding="GBK"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
    
    
        <!--这个是一个普通的bean-->
        <bean id="person" class="InstancePackage.Person">
            <property name="age" value="31"/>
            <property name="son">
                <bean class="InstancePackage.Son">
                    <property name="age" value="10"/>
                </bean>
            </property>
        </bean>
    
    <!--这里将class指定为PropertyPathFactoryBean,然后呢,用targetBeanName指定要获取哪一个bean的属性
    用propertyPath指定要获取它的哪一个属性,就ok-->
        <bean id="son1" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
            <property name="targetBeanName" value="person"/>
            <property name="propertyPath" value="son"/>
        </bean>
    
    <!--这个呢它的实现类是一个son,但是它的age属性id为person.son.age,表示是要获取person的son的
    age属性值,当然啦class是PropertyPathFactoryBean-->
     <bean id="son2" class="InstancePackage.Son">
         <property name="age" >
             <bean id="person.son.age"
                   class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
         </property>
     </bean>
    
    <!--这里呢是最终是一个int,因为它的目标是person下的son的age-->
        <bean id="theAge" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
            <property name="targetBeanName" value="person"/>
            <property name="propertyPath" value="son.age"/>
        </bean>
    
    
        <!--这里是自己定义了一个目标bean,然后获取它的age-->
        <bean id="theAge2" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
            <property name="targetObject">
                <bean class="InstancePackage.Person">
                    <property name="age" value="30"/>
                </bean>
            </property>
    
            <property name="propertyPath" value="age"/>
        </bean>
    
    </beans>

    这是我看李刚编著的《轻量级javaEE企业应用实战(第五版)-Struts2+Spring5+Hibernate5/JAP2》后总结出来的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值