filed数值(也就是静态常量) 注入是通过fieldRetrievingFactoryBean完成的,这个类用来获取目标bean的filed数值
package
Bean.superIOCfield;
public class Field ... {
public static final String TEST_FIELD="welcom";
}
public class Field ... {
public static final String TEST_FIELD="welcom";
}
实体类:
package
Bean.superIOCfield;
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.superIOCfield;
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.superIOCfield;
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 ="son" class ="Bean.superIOCfield.Son" >
< property name ="age" >
< bean id ="Bean.superIOCfield.Field.TEST_FIELD" class ="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" ></ bean >
</ property >
</ bean >
</ beans >
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="son" class ="Bean.superIOCfield.Son" >
< property name ="age" >
< bean id ="Bean.superIOCfield.Field.TEST_FIELD" class ="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" ></ bean >
</ 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+"/superIOCfield.xml");
Son son1=(Son)context.getBean("son");
System.out.println("person age is:"+son1.getAge());
}
String path=new Test().getClass().getResource("/").getPath();
String realpath=path.substring(1, path.length());
ApplicationContext context=new FileSystemXmlApplicationContext(realpath+"/superIOCfield.xml");
Son son1=(Son)context.getBean("son");
System.out.println("person age is:"+son1.getAge());
}
运行结果:
person age is:welcom