Getting Started from Java to quitting: the injection posture of the IOC in spring

Source: Internet
Author: User

What is an IOC? control reversals (inversion of control, English abbreviation for IOC) , is actually this stuff.


You can get a more written explanation by simply Baidu: by introducing an IOC container that implements IOC mode, the IOC container can be used to manage the object lifecycle, dependencies, and so on, so that the application's configuration and dependency specifications are separated from the actual application code. One of the features is the configuration of the text configuration file to configure the relationship between application components without having to re-modify and compile the specific code.


So much has been said, and it can be well understood by an example. before Wang to find objects, or clouds in the boundless crowd to match ( by the little Wang. Object = Small sheet Such syntax for association ), or through 3D printing directly out of the hearts of the goddess ( through new instantiation of ), and now, as long as the 51CTO Matchmaking Center to register, at the same time put forward their own requirements, 51CTO Matchmaking Center will be in all registered users to match, if there is a match on the arrangement of Xiao Wang to Blind date.

the 51CTO Matchmaking Center here is the equivalent of an IOC container, and since there is an intermediary (IOC container), it is not very easy to find objects (many things don't have to be dealt with by Xiao Wang himself). .


After the explanation, the IOC in spring is introduced in the following three ways:

    1. Attribute injection (Set injection)

    2. Constructor Injection (construction method injection)

    3. Factory injection ( rarely used, if you want to use the, then do it yourself, haha )


Next, please invite the code to play! 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0020.gif "alt=" J_0020.gif "/> (remember to import spring related jar packages before writing code)


First, attribute injection

There are two cases of attribute injection, one is the Java basic data type , one is a custom type , and the specific code looks down:

1.1) Writing Song entity classes

    //song class     public class  song {private int songid;//song idprivate string songname;//song name Private string  songtype;//song type Public song ()  {}public song (Int songid, string songname,  string songtype)  {this.songID = songID;this.songName = songName; This.songtype = songtype;} Public int getsongid ()  {return songid;} Public void setsongid (Int songid)  {this.songid = songid;} Public string getsongname ()  {return songname;} Public void setsongname (String songname)  {this.songname = songname;} Public string getsongtype ()  {return songtype;} Public void setsongtype (String songtype)  {this.songtype = songtype;}     } 

    1.2) injects bean objects into the spring configuration file

<beansxmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:p=" http://www.springframework.org/schema/p "xsi:schemalocation="/HTTP/ Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd ">< Bean id= "YJM" class= "Com.pxy.entity.Song" ><property name= "SongID" value= "10086" ></property>< Property Name= "Songname" value= "one cut Plum" ></property><property name= "Songtype" value= "Classic Oldies" ></ Property></bean></beans>

1.3) Create test class for testing (simple point, normal class contains Main method on line)

public static void Main (string[] args) {ApplicationContext applicationcontext = new Classpathxmlapplicationcontext ("AP    Plicationcontext.xml ");    Song song = Applicationcontext.getbean ("YJM", Song.class);    System.out.println ("Song Name:" +song.getsongname ()); System.out.println ("Song Type:" +song.getsongtype ());}

1.4) The results shown are as follows:

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/9D/83/wKioL1mBUTHT2ueDAAArJvKvTnA987.png "title=" 11. PNG "alt=" Wkiol1mbutht2uedaaarjvkvtna987.png "/>


The above is an injection of the basic data type, and if you include a custom type, make the following modifications:

1.5) Add Singer entity class

public class singer {    private int singerid;      //singer id    private string singername;//singer name      private string area;      //Area          public int getsingerid ()  {        return  singerid;    }    public void setsingerid (int  Singerid)  {        this.singerID = singerID;     }    public string getsingername ()  {         return singername;    }    public  void setsingername (String singername)  {         This.singername = singeRname;    }    public string getarea ()  {         return area;    }    public  void setarea (String area)  {        this.area  = area;    }}

1.6) Add the singer property to the song class

Song class public class Song {///previous code omitted ...//new code private Singer singer;//corresponding singer public Singer Getsinger () {RE Turn singer;} public void Setsinger (Singer Singer) {this.singer = Singer;}}

1.7) Modify the configuration file, add singer objects, and use   ref  make reference

        <!--  Create Singer object fyq --><bean id= "Fyq"  class= "Com.pxy.entity.Singer" ><property name= " Singerid " value=" 10000 "></property><property name=" Singername " value=" Yu Ching Fei "></ property></bean><!--  Create song object yjm --><bean id= "Yjm"  class= " Com.pxy.entity.Song "><property name=" SongID " value=" 10086 "></property><property  name= "Songname"  value= "one cut Plum" ></property><property name= "SongType"  value= "Classic Oldies" ></property><!--  Use ref to refer to the above Bean --><property name= "singer"  ref= "Fyq" > </property></bean> 

1.8) Modify the test class and view the results

public static void Main (string[] args) {ApplicationContext applicationcontext = new Classpathxmlapplicationcontext ("AP    Plicationcontext.xml ");    Song song = Applicationcontext.getbean ("YJM", Song.class);    System.out.println ("Song Name:" +song.getsongname ());    System.out.println ("Song Type:" +song.getsongtype ()); System.out.println ("Singer:" +song.getsinger (). Getsingername ());}

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9D/84/wKiom1mBU5qAToniAAAs6EPplBg560.png "title=" 12. PNG "alt=" Wkiom1mbu5qatoniaaas6epplbg560.png "/>

The way the property is injected is over here .... 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0042.gif "alt=" J_0042.gif "/>


Second, constructor injection

we have written the construction method Song (int songid, string songname, String songtype)in song class, Next, We inject the effect directly into the spring configuration file by using the constructor method.

2.1) Inject the Bean object into the spring configuration file

<bean id= "Jht" class= "Com.pxy.entity.Song" > <constructor-arg index= "0" value= "10088" ></ Constructor-arg> <constructor-arg index= "1" value= "Chrysanthemum Station" ></constructor-arg> <constructor-arg Index = "2" value= "Pop" ></constructor-arg></bean>

2.2) See the effect in the test class

public static void Main (string[] args) {ApplicationContext applicationcontext = new Classpathxmlapplicationcontext ("AP    Plicationcontext.xml ");    Song song = Applicationcontext.getbean ("Jht", Song.class);    System.out.println ("Song Name:" +song.getsongname ()); System.out.println ("Song Type:" +song.getsongtype ());}

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9D/84/wKiom1mBVZbSOZTMAAAqTwL5ufs190.png "title=" 13. PNG "alt=" Wkiom1mbvzbsoztmaaaqtwl5ufs190.png "/>

Today's content is here, thank you crossing idle in the bored stroll here and also read!!!

Finally, please crossing leave before the point of Praise , if there is nothing else to do, by the way to comment on two sentences ... 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0019.gif "alt=" J_0019.gif "/>


This article is from the "Software Thinking" blog, please be sure to keep this source http://softi.blog.51cto.com/13093971/1952958

Getting Started from Java to quitting: the injection posture of the IOC in spring

Related Article

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.