Three ways to inject spring

Source: Internet
Author: User

It is said that the programmer who does not understand spring is equivalent to not Java, so the recent time in parallel to learn the spring framework. Learn to summarize the five ways in which spring's IOC is injected.

Here I imagine the scene is this: the soldier and his weapon's story. This is my current structure diagram:

  

Human and weapon are two interfaces, the gun and solder are the classes that implement the above two interfaces, in which, because of the solder need gun to form the gun class dependence

The above code is:

Weapon interface

 Public Interface Weapon {    publicvoid  function ();}
View Code

Human interface

 Public Interface Human {    String name= "";      Public void action ();      Public String getName ();      Public void setName (String name);}
View Code

Gun class

 Public class Implements weapon{    publicvoid  function () {        System.out.println ("I has a gun, I can Shoot! " );    }}
View Code

1. First set injection:

Soldier class

 Public classSoldierImplementshuman{Privateweapon Weapon; PrivateString name;  Public voidaction () {weapon.function (); }     PublicString GetName () {return  This. Name; }     Public voidsetName (String name) { This. name=name; }                //Note that this must be there, otherwise the set injection is unsuccessful     Public voidSetweapon (Weapon weapon) { This. weapon=weapon; }    }    
View Code

Be sure to set the set method of the dependent class in soldier, otherwise the set injection will fail! Configure the class that you want to depend on as a property into the bean, and point ref to the name value of the corresponding bean. The configuration in the corresponding XML file is as follows:

    <Beanname= "Soldier"class= "Roomy.impl.Soldier">        < Propertyname= "Weapon"ref= "Weapon"></ Property>    </Bean>    <Beanname= "Weapon"class= "Roomy.impl.Gun"></Bean>

Run in the main function of the test class:

Test class

 Public class Test {    privatestatic  applicationcontext ctx;      Public Static void Main (string[] args) {        new classpathxmlapplicationcontext ("Beans.xml");         = (Human) Ctx.getbean ("Soldier");        Human.action ();    }}
View Code

Run to get:

I have a gun, I can shoot!

2. Then we look at the constructor injection method, just need to modify the Soldier class, add the constructor, and in the constructor, passed in, as follows:

 Public classSoldierImplementshuman{Privateweapon Weapon; PrivateString name;  PublicSoldier (Weapon weapon) { This. weapon=weapon; }         Public voidaction () {weapon.function (); }     PublicString GetName () {return  This. Name; }     Public voidsetName (String name) { This. name=name; }}
View Code

Also, modify the child elements in the corresponding bean element in the XML file as:

    <Beanname= "Soldier"class= "Roomy.impl.Soldier">    <Constructor-argref= "Weapon"></Constructor-arg>    </Bean>    <Beanname= "Weapon"class= "Roomy.impl.Gun"></Bean>

Also get the results above

3. The next step is to add this sentence to the XML file by using the annotation method:

< Context:annotation-config />

In addition, the entire Beans.xml file is as follows:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0. XSD Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring -context-2.5.xsd ">    <!--The xmlns with context are associated with annotation -    <!--And the next line are for annotation -    <Context:annotation-config/></Beans>

Based on the note is simply not too convenient, directly in front of the object to be added to a sentence @resource can, this note is Java comes with, directly modify the Soldier class as follows:

 Public classSoldierImplementshuman{@ResourcePrivateweapon Weapon; PrivateString name;  Public voidaction () {weapon.function (); }     PublicString GetName () {return  This. Name; }     Public voidsetName (String name) { This. name=name; }    }
View Code

It can also be a spring annotation, which is preceded by a dependent object:

@Autowired
@Qualifier ("name") if there are N dependent objects, you can use this tag to specifically define which bean to match

  

Three ways to inject spring

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.