One of the important principles of spring is the IOC (inverse of control), which controls inversion. The IOC is a technique for creating and managing component dependencies outside the program, and the management control of the instantiation and dependency of bean components in spring is the responsibility of the spring container, and the relationship between objects is easily understood as a dependency between objects: Class A requires B Example, a method in Class A is required to invoke Class B to complete the function, called Class A depends on Class B. Dependency Injection (Di:dependency injection): A dependency between two objects is called Dependency injection (DI) by the dynamic injection-dependent behavior of the external container while the program is running, and Di is a form of the IOC. IOC can do this: your class will not find or instantiate the classes on which they depend. Control happens to be reversed, and the container sets this dependency. Using the IoC often makes the code more concise and provides a good way for interdependent classes.
Three implementation types of dependency Injection: constructor injection, setter injection, and interface injection.
1. Constructor injection
A dependency is established through a class constructor, and the container injects its required dependencies by invoking the constructor of the class, such as in spring's XML configuration file, which uses the <constructor-arg> element to set its dependent objects when constructing the bean. A complete, legitimate object is completed during construction. All dependencies are rendered centrally in the constructor. Dependencies are set at the time of construction by the container and remain in a relatively "constant" state after the component is created. Only the creator of the component cares about its internal dependencies, and for the caller, the dependency is in the black box.
<bean id=" Sonnet29 " class=" Lucien. Sonnet29 "/><!--constructor injection--><bean id=" Poeticduke " class=" Lucien. Poeticjuggler " ><constructor-arg value=" "/><constructor-arg ref=" sonnet29 "/></bean>
2. Setter Method Injection
The properties of the JavaBean are usually private, with a set of accessors: Setxxx () and GetXXX (), which spring uses to implement setter injection by using the set method of the property. For programmers accustomed to the development of traditional javabean, it is more intuitive to set the dependency relationship by setter method. If the dependencies are more complex, then the constructor for the construction injection pattern is quite large, and the setter injection pattern is more concise.
Package Lucien; Public Interface instrument { public void Play ();}
package Lucien; public class Saxophone Span class= "Code-keyword" style= "Color:rgb (0,0,145); Background-color:inherit ">implements instrument {@Override public Void Play () {//TODO auto-generated Method stub system . Out.println (" saxophone ");}}
PackageLucien PublicClass instrumentalistImplementsPerformer {Private StringSongPrivateinstrument instrument; Public StringGetsong () {returnSong;} Publicvoid Setsong (Stringsong) { This. song = Song;} PublicInstrument getinstrument () {returninstrument;} Publicvoid Setinstrument (instrument instrument) { This. instrument = instrument;} @Override Publicvoid Perform () {//TODO auto-generated method stubSystem. OUT.PRINTLN ("playing"+ Song +" : "); Instrument.play ();}}
The corresponding configuration file section is as follows:
<bean id="saxophone" class= "Lucien. Saxophone "/><bean id="Tim" class="Lucien. Instrumentalist "><property name="song" value="Jingle Bells"/><property name="instrument" ref="saxophone"/></bean>
3. Interface Injection
In a class, for a dependent interface, it is dynamically loaded and cast to its interface type through a specific implementation class name. Interface injection is not commonly used.
package Lucien; public class Saxophone Span class= "Code-keyword" style= "Color:rgb (0,0,145); Background-color:inherit ">implements instrument {@Override public Void Play () {//TODO auto-generated Method stub system . Out.println (" saxophone ");}}
PackageLucien PublicClass instrumentalistImplementsPerformer {Private StringSongPrivateinstrument instrument; Public StringGetsong () {returnSong;} Publicvoid Setsong (Stringsong) { This. song = Song;} @Override Publicvoid Perform () {//TODO auto-generated method stubSystem. OUT.PRINTLN ("playing"+ Song +" : ");Try{instrument = (instrument)Class. forname ("Lucien. Saxophone "). newinstance ();}Catch(Exception e) {//TODO auto-generated catch blockE.printstacktrace ();} Instrument.play ();}}
Three ways to inject Spring beans