注入属性值
通过PropertyPathFactoryBean类,可以注入某个实例的属性值。PropertyPathFactoryBean用来获取目标bean的属性值。获得的值可注入给其他bean,也可以直接定义成新的bean.
看例子:
-----------------------------------------------------------------------------------------
package org.viking.spring.imp;
public class Person
{
private int age;
private Son son;
public void setAge(int age) {
this.age = age;
}
public void setSon(Son son) {
this.son = son;
}
public int getAge() {
return age;
}
public Son getSon() {
return son;
}
}
-----------------------------------------------------------------------------------------
package org.viking.spring.imp;
public class Son
{
private int age;
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
-----------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="person" class="org.viking.spring.imp.Person" scope="prototype">
<property name="age">
<value>30</value>
</property>
<property name="son">
<bean class="org.viking.spring.imp.Son">
<property name="age">
<value>11</value>
</property>
</bean>
</property>
</bean>
<bean id="son2" class="org.viking.spring.imp.Son">
<property name="age">
<bean id="person.son.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
</bean>
</property>
</bean>
</beans>
-----------------------------------------------------------------------------------------
package org.viking.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.viking.spring.imp.Son;
import junit.framework.TestCase;
public class testInject extends TestCase
{
public void testInjectAttribute()
{
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("son2的age属性值:" + ((Son)app.getBean("son2")).getAge());
}
}
-----------------------------------------------------------------------------------------
一个bean实例的属性值,不仅可以注入另一个bean,还可将bean实例的属性值直接定义成bean实例,也是通过PropertyPathFactoryBean完成的。对上面ApplicationContext.xml文件做下修改!
-----------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="person" class="org.viking.spring.imp.Person" scope="prototype">
<property name="age">
<value>30</value>
</property>
<property name="son">
<bean class="org.viking.spring.imp.Son">
<property name="age">
<value>11</value>
</property>
</bean>
</property>
</bean>
<bean id="son2" class="org.viking.spring.imp.Son">
<property name="age">
<bean id="person.son.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
</bean>
</property>
</bean>
<bean id="son1" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetBeanName">
<value>person</value>
</property>
<property name="propertyPath">
<value>son</value>
</property>
</bean>
</beans>
-----------------------------------------------------------------------------------------
package org.viking.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.viking.spring.imp.Son;
import junit.framework.TestCase;
public class testInject extends TestCase
{
public void testInjectAttribute()
{
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("son2的age属性值:" + ((Son)app.getBean("son2")).getAge());
System.out.println("系统获取的SON1:" + app.getBean("son1"));
}
}
-----------------------------------------------------------------------------------------
使用PropertyPathFactoryBean必须指定以下两个属性。
1.targetBeanName:用于指定目标bean,确定获取哪个bean的属性值。
2.propertyPath:用于指定属性,确定获取目标bean的哪个属性值,此处的属性可直接使用属性表达式。
可以将基本数据类型的属性值定义成bean实例。在配置文件中再增加如下代码:
-----------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="person" class="org.viking.spring.imp.Person" scope="prototype">
<property name="age">
<value>30</value>
</property>
<property name="son">
<bean class="org.viking.spring.imp.Son">
<property name="age">
<value>11</value>
</property>
</bean>
</property>
</bean>
<bean id="son2" class="org.viking.spring.imp.Son">
<property name="age">
<bean id="person.son.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
</bean>
</property>
</bean>
<bean id="son1" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetBeanName">
<value>person</value>
</property>
<property name="propertyPath">
<value>son</value>
</property>
</bean>
<bean id="theAge" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetBeanName">
<value>person</value>
</property>
<property name="propertyPath">
<value>son.age</value>
</property>
</bean>
</beans>
-----------------------------------------------------------------------------------------
package org.viking.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.viking.spring.imp.Son;
import junit.framework.TestCase;
public class testInject extends TestCase
{
public void testInjectAttribute()
{
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("son2的age属性值:" + ((Son)app.getBean("son2")).getAge());
System.out.println("系统获取的SON1:" + app.getBean("son1"));
System.out.println("系统获取的theAge的值:" + app.getBean("theAge"));
}
}
-----------------------------------------------------------------------------------------
目标bean既可以是容器中已有的bean实例,也可以是嵌套bean实例。
-----------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="person" class="org.viking.spring.imp.Person" scope="prototype">
<property name="age">
<value>30</value>
</property>
<property name="son">
<bean class="org.viking.spring.imp.Son">
<property name="age">
<value>11</value>
</property>
</bean>
</property>
</bean>
<bean id="son2" class="org.viking.spring.imp.Son">
<property name="age">
<bean id="person.son.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
</bean>
</property>
</bean>
<bean id="son1" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetBeanName">
<value>person</value>
</property>
<property name="propertyPath">
<value>son</value>
</property>
</bean>
<bean id="theAge" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetBeanName">
<value>person</value>
</property>
<property name="propertyPath">
<value>son.age</value>
</property>
</bean>
<bean id="theAge2" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetObject"><!-- 要注意,这里不是targetBeanName了噢! -->
<bean class="org.viking.spring.imp.Person">
<property name="age">
<value>12</value>
</property>
</bean>
</property>
<property name="propertyPath">
<value>age</value>
</property>
</bean>
</beans>
-----------------------------------------------------------------------------------------
package org.viking.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.viking.spring.imp.Son;
import junit.framework.TestCase;
public class testInject extends TestCase
{
public void testInjectAttribute()
{
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("son2的age属性值:" + ((Son)app.getBean("son2")).getAge());
System.out.println("系统获取的SON1:" + app.getBean("son1"));
System.out.println("系统获取的theAge的值:" + app.getBean("theAge"));
System.out.println("系统获取的theAge2的值:" + app.getBean("theAge2"));
}
}
-----------------------------------------------------------------------------------------
注意上面配置文件,定义嵌套bean实例时采用的时targetObject,而不时targetBeanName,如果这填错了,可是要报异常的噢!
-------以上的文字以及代码摘录于李刚写的《轻量级J2EE开发企业应用实战》,代码有少许变动!
Identify the aspects of your application that vary and separate them from what stays the same.