Spring Propertypathfactorybean | Get a property value for another bean

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.