Spring's core mechanism: Dependency Injection

 

The concept relies injection

When an object to call another object, the object is generally to be a new call, example:

class  A{

  private B b=new B();

  public  void  test(){

    b.say();

  }

}

Object class A depends on B class object, if not the class B object, A class object will not work, saying that A depends on B.

Above approach will increase the coupling of class A and class B, is not conducive to upgrade later in the project (expansion), maintenance.

 

In Spring, instances of the class B (the caller), no longer created by Class A (the caller), but created by the Spring container created after a good, by the Spring container instance of the class B type A implanted object, called dependency injection (dependency injection, DI).

Was originally automatically create a B class object by the A class (class A control create a class B), is now Spring container to create a class B object, injection A class object, class A passively Class B Example Spring container created Class B object control over the creation of a reversal occurs, it is also called inversion of control (inversion of control, IoC).

 

Dependency injection is an excellent decoupling way, a relationship between the class is responsible for controlling Spring container (Bean), reducing the coupling between classes.

Because the dependencies injection Spring container (IoC), also referred to as the container so Spring Spring IoC container.

 

Dependency injection There are two ways:

  • Set value of the injected
  • Constructor injection

 

 

 

 

Set value of the injected

By the calling class setter () method of injection is adjusted class instance. I.e., a parameter dependent objects.

 

1, were written two interfaces

1 public interface Student {
2     public String getName();
3 }
1 public interface Teacher {
2     public void say();
3 }

 

 

2, respectively, write two implementation class

 1 public class BadStudent implements Student {
 2     private String name;
 3 
 4     public BadStudent(String name){
 5         this.name=name;
 6     }
 7 
 8     @Override
 9     public String getName(){
10         return name;
11     }
12 }
. 1  public  class MathTeacher the implements Teacher {
 2      Private Student Student;
 . 3  
. 4      // caller's the setter () method, the instance of receiving callee 
. 5      public  void setStudent (Student Student) {
 . 6          the this .student = Student;
 . 7      }
 . 8  
. 9      @override
 10      public  void say () {
 . 11          System.out.println (student.getName () + ", to what is called the parents." );
 12      }
 13 }

 Class and interface is coupled.

 

 

3, the configuration file Bean in applicationContext.xml

1 <! - Example lisi class initialization BadStudent ->
 2      <the bean name = " lisi " class = "beans.BadStudent">
 . 3          <constructor-Arg value = "John Doe" /> <! - of the BadStudent constructors pass "John Doe" ->
 . 4      </ the bean>
 . 5  
. 6      <-! configuration dependent ->
 . 7      <the bean name = "mathTeacher" class = "beans.MathTeacher">
 . 8          <Property name = "Student" = REF " Lisi " /> <- Specifies the name of the parameter setter method, ref arguments specified value or:! a Bean's name ->
 . 9      </ the bean>

 

 <Constructor-arg> element is transmitted to the constructor argument Bean, a <constructor-arg> passing an argument, a <bean> may be a plurality of <constructor-arg>, according to call number of arguments passed corresponding constructor. That constructor-arg constructor argument.

Argument value specified by the ref attribute or value. Java is the value specified in the parameter type basis, ref i.e. reference (reference), Bean specify other dependent.

This value can be specified by the first attribute is the index number of parameters, automatically increments from 0 by default.

 

 

<Property> attribute specifies the setter method arguments. A <property> Specifies a parameter a <bean> may have a plurality of <property> child element.

The name attribute specifies a setter method parameter, specified by the argument value or ref.

If the argument is an instance of a Bean, specified REF, is the value of the name attribute value of the Bean.

If the argument is a Java-based types (integer, floating point, String, etc.), with a specified value (in quotes), the Spring container automatically according to the type of the specified type name argument, the argument is converted to the corresponding the type of assignment.

 

 

4, write a test class

1 public class Test {
2     public static void main(String[] args) {
3         ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
4         MathTeacher mathTeacher=applicationContext.getBean("mathTeacher",MathTeacher.class);
5         mathTeacher.say();
6     }
7 }

 See the console has been out of print, "John Doe, called the parents to look at.".

 

 

 

 

 

Constructor injection

Injected via the constructor.

1, the above code, the class will be modified as follows MathTeacher

. 1  public  class MathTeacher the implements Teacher {
 2      Private Student Student;
 . 3  
. 4     constructors // caller's 
 . 5 public MathTeacher (Student Student) { 
 . 6 this.student = Student; 
 . 7}
 . 8  
. 9      @Override
 10      public  void say () {
 11          System.out.println (student.getName () + ", called the parents to look at." );
 12      }
 13 }

Injection without using setter methods depend on, but the use of the object constructor dependency injection.

 

2, the container is configured as follows modification Spring

1   <! - Example lisi class initialization BadStudent ->
 2      <the bean name = "lisi" class = "beans.BadStudent">
 . 3          <constructor-Arg value = "John Doe" />
 . 4      </ the bean>
 . 5  
. 6 <! - configuration dependent -> 
. 7 <the bean name = "mathTeacher" class = "beans.MathTeacher"> 
. 8 <constructor Arg-name = "Student" REF = "Lisi" /> 
. 9 </ the bean>

 

3, run, see the console print out the "John Doe, called the parents to look at.".

 

 

 

Explanation

  • Both approaches can be injected into Java's built-in data types.
  • value is the basis of type of injected Java, ref is injecting dependencies of Bean. Of course, also be used <value>, <ref> child element in the form of configuration.

     

 

Guess you like

Origin www.cnblogs.com/chy18883701161/p/11111858.html