Spring Learning -2-spring IOC container

Source: Internet
Author: User

Role: Creating Objects & Working with Object dependencies

How the container creates objects

1) Call parameterless constructor

    class= "Com.cx.createObj.User" ></bean>

2) with parametric constructor

class= "Com.cx.createObj.User" >        <constructor-arg index= "0" type= "int" value= "></" constructor-arg>        <constructor-arg index= "1" type= "java.lang.String" value= "Jack" ></ Constructor-arg></bean>

<!--defines a string value when mike-->

<bean id= "str" class= "java.lang.String" >
<constructor-arg value= "Mike" ></constructor-arg>
</bean>

<bean id= "user" class= "Com.cx.createObj.User" >

<constructor-arg index= "0" type= "int" value= "></constructor-arg>"
<constructor-arg index= "1" type= "java.lang.String" ref= "str" ></constructor-arg>

</bean>

3) factory-created objects

Factory class, non-static method to create an object

    <!--Create Factory--    class= "Com.cx.createObj.ObjectFactory" ></bean>    <bean id= "User" Factory-bean= "Factory" factory-method= "getinstance" ></bean>

Factory class, static method Create object

    <!--static Method--    class= "Com.cx.createObj.ObjectFactory" factory-method= "Getstaticinstance" > </bean>

User entity class

 PackageCOM.CX.CREATEOBJ;/*** Created by Cxspace on 16-8-6.*/ Public classUser {Private intID; PrivateString name;  PublicUser () {System.out.println ("----Call no parameter construction method----"); }     PublicUser (intID, String name) {         This. ID =ID;  This. Name =name; System.out.println ("----Call with parameter constructor----"); }     Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {return"user{" + "id=" + ID + ", name= '" + name + ' \ ' + '} '; }     Public voidInit_user () {System.out.println ("After creating the object, initialize the"); }     Public voidDestroy_user () {System.out.println ("IOC container destruction, user Object Recycling"); }}

Factory class

 PackageCOM.CX.CREATEOBJ;/*** Created by Cxspace on 16-8-6.*/ Public classObjectfactory {//instance method to create an object     PublicUser getinstance () {return NewUser (101, "factory: Invoke instance method"); }    //static methods to create objects     Public StaticUser getstaticinstance () {return NewUser (100, "Factory: Call static Method"); }}

Spring Learning -2-spring IOC container

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.