SPRING注入FIELD值

本文详细介绍了Spring中使用FieldRetrievingFactoryBean注入field值的方法,并通过具体案例展示了其应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring注入field值
嘿嘿,今天偷懒,就记一点点内容,然后去看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"));
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值