Spring高级注入之属性值注入
来源:互联网 发布:翻墙软件排名 编辑:程序博客网 时间:2024/04/29 06:25
实际应用中,某个实例的属性可能是另一个对象的一个属性,Spring支持将bean实例的属性值直接赋值给一个变量
属性值的注入,是通过PropertyPathFactoryBean完成的,PropertyPathFactoryBean用来获取目标bean的属性,获得的值可以注入到其他bean,也可以定义成新的bean
实体类:
package Bean.superIOCparam;
public class Person ...{
private Son son;
private String age;
public String getAge() ...{
return age;
}
public void setAge(String age) ...{
this.age = age;
}
public Son getSon() ...{
return son;
}
public void setSon(Son son) ...{
this.son = son;
}
}
package Bean.superIOCparam;
public class Son ...{
private String age;
public String getAge() ...{
return age;
}
public void setAge(String age) ...{
this.age = age;
}
}
public class Person ...{
private Son son;
private String age;
public String getAge() ...{
return age;
}
public void setAge(String age) ...{
this.age = age;
}
public Son getSon() ...{
return son;
}
public void setSon(Son son) ...{
this.son = son;
}
}
package Bean.superIOCparam;
public class Son ...{
private String age;
public String getAge() ...{
return age;
}
public void setAge(String age) ...{
this.age = age;
}
}
配置文件:提供四种注入
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="person" class="Bean.superIOCparam.Person" singleton="false">
<property name="age">
<value>30</value>
</property>
<property name="son">
<bean class="Bean.superIOCparam.Son">
<property name="age">
<value>16</value>
</property>
</bean>
</property>
</bean>
<!--如下将会将person的属性son的属性age传入son1实例的age属性-->
<bean id="son1" class="Bean.superIOCparam.Son">
<property name="age">
<!--以下是访问bean属性的简单方式,这样可以将person这个bean的age属性赋值给son1这个bean的age属性-->
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="person" class="Bean.superIOCparam.Person" singleton="false">
<property name="age">
<value>30</value>
</property>
<property name="son">
<bean class="Bean.superIOCparam.Son">
<property name="age">
<value>16</value>
</property>
</bean>
</property>
</bean>
<!--如下将会将person的属性son的属性age传入son1实例的age属性-->
<bean id="son1" class="Bean.superIOCparam.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="Bean.superIOCparam.Person">
<property name="age"><value>30</value></property>
</bean>
</property>
<property name="propertyPath"><value>age</value></property>
</bean>
</beans>
</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="Bean.superIOCparam.Person">
<property name="age"><value>30</value></property>
</bean>
</property>
<property name="propertyPath"><value>age</value></property>
</bean>
</beans>
测试代码:
public static void main(String[] args) throws Exception ...{
String path=new Test().getClass().getResource("/").getPath();
String realpath=path.substring(1, path.length());
ApplicationContext context=new FileSystemXmlApplicationContext(realpath+"/superIOCparam.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"));
}
String path=new Test().getClass().getResource("/").getPath();
String realpath=path.substring(1, path.length());
ApplicationContext context=new FileSystemXmlApplicationContext(realpath+"/superIOCparam.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"));
}
运行结果:
person age is:16
person age is:16
16
30
- Spring高级注入之属性值注入
- Spring高级注入之属性值注入
- Spring高级注入之属性值注入
- Spring高级注入之Field值注入
- Spring高级注入之Method值注入
- Spring高级注入之Method值注入
- Spring高级注入之Field值注入
- Spring之属性注入
- spring之属性注入
- spring学习之属性注入
- 【Spring学习】之 属性注入与对象属性注入
- Spring 注入属性值细节
- Spring高级应用之注入各类集合
- Spring高级应用之注入嵌套Bean
- Spring高级应用之注入各类集合
- Spring高级应用之注入各类集合
- Spring高级应用之注入各类集合
- spring之构造函数注入属性值与set方法注入属性值
- 一个折磨了我一个星期的问题
- 剖析Windows NT/2000内核对象组织
- [转]C语言图形编程(五) -二维图形变换②
- 电路板氧化后引起接触不良从而导致交换机软故障及解决过程
- C++关键字typename
- Spring高级注入之属性值注入
- [转]C语言图形编程(六) -图形程序设计实例:零件图形的绘制
- 毕业了,物是人非了
- 关于Autorun.inf及其命令详解
- 理解Adapter模式的好文章
- 未将对象引用设置到对象的实例"异常的原因
- 本田VS雅马哈:战略之争
- GridView 72般绝技(值得收藏)
- 史上最简单的Hibernate入门简介