In general, the dependency in spring comes from another bean, but there are situations where we need a property of another bean, and we can use Propertypathfactorybean.
eg
Continuation of the Dog class:
PackageFuckspring.propertyeditor; Public classDog {PrivateString name; PrivateString type; Private intAge ; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString GetType () {returntype; } Public voidsetType (String type) { This. Type =type; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicString toString () {return This. name+ This. type+ This. Age; }}
Another person class:
PackageFuckspring.propertyeditor; Public classPerson {PrivateString name; PrivateString Hisdogname; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString Gethisdogname () {returnHisdogname; } Public voidsethisdogname (String hisdogname) { This. Hisdogname =Hisdogname; } }
And then our test class, Mytest.java.
PackageFuckspring.propertyeditor;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMyTest {PrivatePerson p; PublicPerson Getp () {returnp; } Public voidSETP (person p) { This. P =p; } Public Static voidMain (string[] args) {ApplicationContext ac=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); MyTest MT= (MyTest) ac.getbean ("MyTest"); String Hisdog=Mt.p.gethisdogname (); System.out.println (Hisdog); } }
And finally, our applicationcontext.xml.
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Beans Public "-//spring//dtd BEAN 2.0//en" "Http://www.springframework.org/dtd/spring-beans-2.0.dtd" [ <! ENTITY contextinclude SYSTEM "Org/springframework/web/context/web-inf/contextinclude.xml">]><Beans> <BeanID= "Dog"class= "FuckSpring.propertyEditor.Dog"> < Propertyname= "Name"value= "Two dog son"> </ Property> < Propertyname= "type"value= "Two ha"> </ Property> < Propertyname= "Age"value= " the"> </ Property> </Bean> <!--here Spring sets the name of dog as a bean, which is used by the person below to get - <BeanID= "Dogname"class= "Org.springframework.beans.factory.config.PropertyPathFactoryBean"> < Propertyname= "TargetObject"> <refLocal= "Dog"/> </ Property> < Propertyname= "PropertyPath"> <value>Name</value> </ Property> </Bean> <BeanID= "Person"class= "FuckSpring.propertyEditor.Person"> < Propertyname= "Hisdogname"> <refLocal= "Dogname" /> </ Property> < Propertyname= "Name"> <value>Tom</value> </ Property> </Bean> <BeanID= "MyTest"class= "FuckSpring.propertyEditor.MyTest"> < Propertyname= "P"> <refLocal= "Person"/> </ Property> </Bean></Beans>
Then run Mytest.java,,, binggo! success ~
Spring Propertypathfactorybean | Get a property value for another bean