Spring高级注入之Field值注入

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

package Bean.superIOCfield;

public class Field ... {
publicstaticfinalStringTEST_FIELD="welcom";
}


实体类:

package Bean.superIOCfield;

public class Person ... {
privateSonson;
privateStringage;
publicStringgetAge()...{
returnage;
}

publicvoidsetAge(Stringage)...{
this.age=age;
}

publicSongetSon()...{
returnson;
}

publicvoidsetSon(Sonson)...{
this.son=son;
}

}



package Bean.superIOCfield;

public class Son ... {
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 >

< 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 ... {

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

你可能感兴趣的:(spring,bean,xml)