Spring notes (5) ------ dependency Injection

Source: Internet
Author: User

Spring notes (5) ------ dependency Injection

Property injection: The setXxx () method is used to inject Bean property values or dependent objects. This method features high flexibility and high usage.

Example:

public class Car {private String brand;private String color;private String maxSpeed;public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public String getMaxSpeed() {return maxSpeed;}public void setMaxSpeed(String maxSpeed) {this.maxSpeed = maxSpeed;}}

Let's look at the Bean. xml configuration file:

 
 
  
   
   
   
  
 

Property injection requires a default constructor and a corresponding setter () method for the property to be injected. Spring first calls the Bean's default constructor to instantiate the Bean object, then, the setter method is called through reflection to inject the property value.

In addition, Spring only checks whether the Bean has the corresponding setter method. It is not required if the Bean has the corresponding attribute variable. For example:

Package com. baobaobao; public class Car {private String brand; private String color; public String getBrand () {return brand;} public void setBrand (String brand) {this. brand = brand;} public String getColor () {return color;} public void setColor (String color) {this. color = color;} public void setMaxSpeed (String maxSpeed) {System. out. println ("speed setter ...... ");}}

The configuration file is the same as above. It can still be used without the speed attribute, But we generally reserve a member variable with the same name.

Constructor injection:

Constructor injection is another common injection method besides property injection. It ensures that some necessary attributes are set during Bean instantiation and can be used after Bean instantiation.

1) matching input parameters by type:

If you want to provide the value of brand and price for any available Car object, if you want to use property injection, you can only provide guarantee for manual configuration, but not at the syntax level, at this time, constructor injection will be used. Before using constructor injection, you must provide constructor with parameters. As follows:

package com.baobaobao;public class Car {private double price;private String color;public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public Car(double price,String color){this.price = price;this.color = color;}}

Add a constructor with parameters and check the configuration file again:

 
 
  
   
    
     
200000
    
   
   
    
     
Red
    
   
  
 

In The element has a type attribute, which provides Spring with information to determine the ing between configuration items and constructor input parameters. In addition, Spring label attributes are independent of elements, ensuring the correctness of the configuration file. For example:

  
  
   
    
     
      
Red
     
    
    
     
      
200000
     
    
   
  
The order is changed and will not be affected ....

2) matching input parameters by index:

If the type attribute is used to distinguish attributes, it is obviously unrealistic. If the two attributes have the same type, they will be finished. This can be determined through the input parameter index.

Because the Java reflection mechanism cannot remember the input parameter name of the constructor, we cannot specify the input parameter name of the constructor to configure the constructor injection, the ing between the constructor configuration item and the input parameter can only be indirectly determined by the input parameter type and the input parameter index. As follows:

package com.baobaobao;public class Car {private double price;private String color;private String brand;public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public Car(double price,String color,String brand){this.price = price;this.color = color;this.brand = brand;}}

The configuration file is as follows:

  
  
   
    
     
      
200000
     
    
    
     
      
Red
     
    
    
     
      
Audi
     
    
   
  

The index starts from 0, indicating the sequential index of input parameters in the constructor.

3) the combined use type and index match input parameters:

Sometimes the type and index must be combined to determine the correspondence between the configuration item and the constructor input parameter, as shown below:

public class Car {private double price;private String color;private String brand;private int maxSpeed;public int getMaxSpeed() {return maxSpeed;}public void setMaxSpeed(int maxSpeed) {this.maxSpeed = maxSpeed;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public Car(double price,String color,String brand){this.price = price;this.color = color;this.brand = brand;}public Car(String color,int maxSpeed,String brand){this.color = color;this.maxSpeed = maxSpeed;this.brand = brand;}}

The Bean has two overloaded constructors, both of which have three input parameters. If the corresponding relationship cannot be determined only by index, you can use type and index in combination:

  
  
   
    
     
      
Red
     
    
    
     
      
200
     
    
    
     
      
Audi
     
    
   
  

This configuration corresponds to the second constructor of the Car above ....

4) matching input parameters through reflection of its own type:

If the input parameter type of the Bean constructor is recognizable (non-basic data type and the input parameter type varies), the java reflection mechanism can obtain the input parameter type of the constructor, even if the constructor injection configuration does not provide types and indexes, Spring can still complete constructor injection.

As follows:

  
  
   
    
     
      
James
     
    
    
     
    
   
   
    
     
      
Red
     
    
    
     
      
200
     
    
    
     
      
Audi
     
    
   
  

The Car class is the same as in the preceding example, which can implement injection. However, if there are multiple constructors in the Bean, the specified type and index displayed are still a good habit.

5) Circular dependency:

The Spring container can be smoothly instantiated to inject the Bean configured by the constructor. The object referenced by the input parameters of the Bean constructor must be ready. Due to this mechanism limitation, if both beans adopt constructor injection and the constructor input parameters reference each other, a circular dependency problem similar to a thread deadlock occurs.


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.