filed数值(也就是静态常量)注入是通过fieldRetrievingFactoryBean完成的,这个类用来获取目标bean的filed数值
packageBean.superIOCfield;

publicclassField...{
publicstaticfinalStringTEST_FIELD="welcom";
}
实体类:
packageBean.superIOCfield;

publicclassPerson...{
privateSonson;
privateStringage;
publicStringgetAge()...{
returnage;
}
publicvoidsetAge(Stringage)...{
this.age=age;
}
publicSongetSon()...{
returnson;
}
publicvoidsetSon(Sonson)...{
this.son=son;
}
}

packageBean.superIOCfield;

publicclassSon...{
privateStringage;

publicStringgetAge()...{
returnage;
}

publicvoidsetAge(Stringage)...{
this.age=age;
}
}
配置文件:
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<beanid="son"class="Bean.superIOCfield.Son">
<propertyname="age">
<beanid="Bean.superIOCfield.Field.TEST_FIELD"class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"></bean>
</property>
</bean>
</beans>
测试代码:

publicstaticvoidmain(String[]args)throwsException...{
Stringpath=newTest().getClass().getResource("/").getPath();
Stringrealpath=path.substring(1,path.length());
ApplicationContextcontext=newFileSystemXmlApplicationContext(realpath+"/superIOCfield.xml");
Sonson1=(Son)context.getBean("son");

System.out.println("personageis:"+son1.getAge());
}
运行结果:
person age is:welcom
2587

被折叠的 条评论
为什么被折叠?



