Spring使用PropertyPathFactoryBean进行属性值注入
来源:互联网 发布:建e全景软件 编辑:程序博客网 时间:2024/06/11 18:47
本文转自:http://www.blogjava.net/wangxinsh55/archive/2010/08/10/328427.html
实际应用中,某个实例的属性可能是另一个对象的一个属性,Spring支持将bean实例的属性值直接赋值给一个变量
属性值的注入,是通过PropertyPathFactoryBean完成的,PropertyPathFactoryBean用来获取目标bean的属性,获得的值可以注入到其他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.alibaba.chj.factorybean.Person" scope="prototype"> <property name="age"> <value>30</value> </property> <property name="son"> <bean class="com.alibaba.chj.factorybean.Son"> <property name="age"> <value>16</value> </property> </bean> </property> </bean> <!--如下将会将person的属性son的属性age传入son1实例的age属性--> <bean id="son1" class="com.alibaba.chj.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.alibaba.chj.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/alibaba/chj/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")); }}
- Spring使用PropertyPathFactoryBean进行属性值注入
- Spring学习之获取其他Bean的属性值(PropertyPathFactoryBean)
- Spring学习之获取其他Bean的属性值(PropertyPathFactoryBean)--踩坑
- SPRING.NET 1.3.2 学习21--使用对象或类的属性值进行注入
- 使用Spring进行依赖注入
- 使用Spring注解注入属性
- spring使用案例 属性注入
- 使用spring注解注入属性
- 使用spring进行属性注入的实例解析(spring面向切面和控制反转)。
- Spring高级注入之属性值注入
- Spring高级注入之属性值注入
- Spring高级注入之属性值注入
- spring 使用get/set注入对象的属性值
- Spring 注入属性值细节
- Spring @Value注解使用${}进行注入
- 使用Spring注解来注入属性
- 使用Spring注解来注入属性
- 使用Spring注解来注入属性
- win7 64位操作 SQLlite +VS2010
- 各种框架(Struts、Spring、Hibernate)及应用服务器(Tomcat、WebSphere)对应的开发环境(JDK、Servlet、JSP)
- 理解 COM 套间
- linux下vi命令 修改文件及保存的使用方法
- CCD
- Spring使用PropertyPathFactoryBean进行属性值注入
- 公司里从员工到经理,不同层级应该关注的事情
- Andoroid学习笔记05——Activity布局管理
- 在线升级Fedora 15 到Fedora 16
- 当wp7的程序有在手机上存储的数据后,在vs中一Clear Solution 完了,所有手机上的数据都没了.
- 小议JNDI之java:comp/env
- 近期规划
- C++ 中 vector 如何实现内存分配
- 黑马程序员------面向对象之类