- 嗯。。。就是在配置文件中一个bean把别人的属性拿过来用
- 通过将class指定为PropertyFactoryBean来实现获取其他bean的属性
- 在代码里边说吧,总而言之呢就是把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》后总结出来的。
Spring(20) 获取别的bean的属性
最新推荐文章于 2024-06-06 16:36:11 发布