嘿嘿,今天偷懒,就记一点点内容,然后去看PPLIVE去!
注入field值
FieldRetrievingFactoryBean用来获得目标bean的field值。获得的值可注入给其他bean,也可直接定义成新的bean.
for example:
--------------------------------------------------------------------------------------------
package org.viking.spring.imp;
public class Son
{
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = 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="son" class="org.viking.spring.imp.Son">
<property name="age">
<!-- FiledRetrievingFactoryBean用来获取目标类的field值。id属性确定获取哪个类哪个field值。 -->
<bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/>
</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 TestInjectField extends TestCase
{
public void testInjectField()
{
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
Son son = (Son)app.getBean("son");
System.out.println("系统获取son的age属性: " + son.getAge());
}
}
--------------------------------------------------------------------------------------------
下面这个是field值定义成bean实例。只需修改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="son" class="org.viking.spring.imp.Son">
<property name="age">
<!-- FiledRetrievingFactoryBean用来获取目标类的field值。id属性确定获取哪个类哪个field值。 -->
<bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/>
</property>
</bean>
<bean id="theAge" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField">
<value>java.sql.Connection.TRANSACTION_SERIALIZABLE</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 TestInjectField extends TestCase
{
public void testInjectField()
{
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
Son son = (Son)app.getBean("son");
System.out.println("系统获取son的age属性: " + son.getAge());
System.out.println("系统获取theAge的值: " +app.getBean("theAge"));
}
}
--------------------------------------------------------------------------------------------
-------以上的文字以及代码摘录于李刚写的《轻量级J2EE开发企业应用实战》,代码有少许变动!
Identify the aspects of your application that vary and separate them from what stays the same.