Spring Learning IOC Control inversion (2)

Source: Internet
Author: User

Before you begin:

1. This blog post is original, reproduced please indicate the source

2. The author is not a computer trained, if there are errors, please correct me.

---------------------------------------------------------------------------'s starting. La La la--------------------------------------- ----------------------------------------

The previous blog post simply demonstrates how to create a bean from the spring framework, inject beans, and run it in Java. This article focuses on some of the naming and initialization rules for bean creation.

Each bean has and has only one unique identifier (identifier), and if the bean needs more than one identifier, we can use the alias (aliases) method.

<name= "FromName"  alias= "ToName"/>

For an XML-defined IOC container, an identifier is either an ID or a name. If no ID is specified or name,spring automatically generates an identifier for the bean. However, I still recommend that you beginners to specify the ID or name, so that the use of dependency when the probability of error will be smaller.

Let's do some actual work on the bean generation, this article will demonstrate three ways of writing (although the previous article has used the constructor method, this article will review)

1. Constructor method

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">    <BeanID= "Bird"class= "Com.dabingguozi.ioc2.pojo.ioc2.Bird">        <!--additional collaborators and configuration for this bean go here -    </Bean>    <Beanname= "Rabbit"class= "Com.dabingguozi.ioc2.pojo.ioc2.Rabbit">        <!--additional collaborators and configuration for this bean go here -    </Bean></Beans>

And what does our Java Pojo look like? Take rabbit for an example.

 Public class Rabbit {    private  Rabbit () {    }}

So we ran such a main program

 Public class Application {    publicstaticvoid  main (string[] a) {        =                 New Classpathxmlapplicationcontext (new string[] {"Ioc2_animal.xml"});         = Context.getbean ("Bird", bird.  Class);         = Context.getbean ("Rabbit", Rabbit.  Class);        System.out.println (Bird.getclass (). GetName ());        System.out.println (Rabbit.getclass (). GetName ());}    }

Bling~bling~ The result is this.

2. Static Factory method

Static factories provide static methods for creating instances, so this is the Java class.

 Public class Fish {    privatestaticnew  fish ();     Private fish () {    }    publicstatic  Fish CreateInstance () {          return  fish;    }}

And the bean definition, that is, where the Factory-method mapping to class static Factory method, through the initialization of such a definition, the ID of fish bean will be created

<id= "Fish"          class= "Com.dabingguozi.ioc2.pojo.ioc2.Fish"            factory-method= "CreateInstance"/>

The test is also successful yo ~

3. Instance Factory method

The so-called instance factory, is such a Java factory method, note that this factory is not static Oh ~

 Public class Zoo {    privatestaticnew  Rabbit ();     Private Zoo () {}      Public Rabbit createrabbitinstance () {        return  Rabbit;    }}

And our XML is defined in this way:

<Beanname= "Zoo"class= "Com.dabingguozi.ioc2.pojo.ioc2.Zoo">        <!--additional collaborators and configuration for this bean go here -    </Bean>    <BeanID= "Rabbit"Factory-bean= "Zoo"Factory-method= "Createrabbitinstance"/>

That is, we first have a zoo bean, and then when we create the rabbit bean, we use the Createrabbitinstance factory method inside the zoo bean as the rabbit bean creation method.

Run the main program.

Successful creation of rabbit, scattered flowers ~ ~ ~

Next: Spring Learning IOC Control inversion (3) (This is not link, poke not open 2333)

This article focuses on the creation of beans, the next article will introduce the Bean dependency injection method, please look forward to, Prudential Xie Watch.

This blog content refers to the Spring framework official documents, if there is a conflict, please refer to the original content


Spring Learning IOC Control inversion (2)

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.