Peak Spring4 Learning (2) several ways to rely on injection

Source: Internet
Author: User

One, assemble a bean

Ii. several ways of dependency injection; Public classpeople {Private intID; PrivateString name; Private intAge ;  Publicpeople () {Super(); //TODO auto-generated Constructor stub    }     PublicPeople (intID, String name,intAge ) {        Super();  This. ID =ID;  This. Name =name;  This. Age =Age ; }     Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; } @Override PublicString toString () {return"People [id=" + ID + ", name=" + name + ", age=" + Age + "]"; }        }
View Code

Spring Management Bean's xml:beans.xml:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://"Xmlns:xsi= "Http://"xsi:schemalocation= "Http://" >    <BeanID= "HelloWorld"class= ""></Bean>      <!--Assemble a bean -      <BeanID= "People"class= ""></Bean>            <!--Dependency Injection -      <!--Attribute Injection -     <BeanID= "People2"class= "">         < Propertyname= "id"value= "1"></ Property>         < Propertyname= "Name"value= "Zhang San"></ Property>         < Propertyname= "Age"value= "One"></ Property>     </Bean>          <!--constructor injection by type -     <BeanID= "People3"class= "">         <Constructor-argtype= "int"value= "2"></Constructor-arg>         <Constructor-argtype= "String"value= "John Doe"></Constructor-arg>         <Constructor-argtype= "int"value= "All"></Constructor-arg>     </Bean>     <!--constructor injection by index -     <BeanID= "People4"class= "">        <Constructor-argIndex= "0"value= "3"></Constructor-arg>        <Constructor-argIndex= "1"value= "Harry"></Constructor-arg>        <Constructor-argIndex= "2"value= " on"></Constructor-arg>    </Bean>    <!--Constructor Injection Type index federated use -    <BeanID= "People5"class= "">        <Constructor-argIndex= "0"type= "int"value= "4"></Constructor-arg>        <Constructor-argIndex= "1"type= "String"value= "Recruit Six"></Constructor-arg>        <Constructor-argIndex= "2"type= "int"value= "The "></Constructor-arg>    </Bean>        <!--Factory methods inject non-static plants -    <BeanID= "Peoplefactory"class= ""></Bean>    <BeanID= "People6"Factory-bean= "Peoplefactory"Factory-method= "Createpeople"></Bean>        <!--factory methods inject static plant -    <BeanID= "People7"class= ""Factory-method= "Createpeople"></Bean></Beans> under the

Static Factory:

 Package; Import;  Public class PeopleFactory2 {    publicstatic  People Createpeople () {        new  people ();        People.setid (6);        People.setname ("Xiao Ba");        People.setage (N);         return people;}    }
View Code

Non-static factory:

 Package; Import;  Public class peoplefactory {    public  People createpeople () {        new  people ();        People.setid (5);        People.setname ("small Seven");        People.setage (a);         return people;}    }
View Code

Test code:;ImportOrg.springframework.context.ApplicationContext;;; Public classTest {@SuppressWarnings ("Resource")     Public Static voidMain (string[] args) {ApplicationContext ac=NewClasspathxmlapplicationcontext ("Beans.xml"); People people= (people) Ac.getbean ("People");                System.out.println (people); //Attribute InjectionPeople people2 = (people) Ac.getbean ("People2");                System.out.println (People2); //constructor injection by typePeople people3 = (people) Ac.getbean ("People3");                System.out.println (PEOPLE3); //Factory methods inject non-static plantsPeople people6 = (people) Ac.getbean ("People6");                System.out.println (PEOPLE6); //factory methods inject static plantPeople people7 = (people) Ac.getbean ("People7");            System.out.println (PEOPLE7); }}
View Code

Console print:

Peak Spring4 Learning (2) several ways to rely on injection

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