Spring's Constructor Injection

Injection: Assign values ​​to member variables through Spring's configuration file
Set injection: Spring calls the Set method, assigns values ​​to member variables through the configuration file
Constructive injection: Spring calls the constructor, assigns values ​​to member variables through the configuration file

1. Development steps

  • Provides a parameterized constructor
public class Customer implements Serializable {
    
    
    private String name;
    private int age;

    public Customer(String name, int age) {
    
    
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
    
    
        return "Customer{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
  • Assign values ​​to member variables through Spring's configuration file
<bean id="customer" class="com.zyh.basic.Customer">
    <constructor-arg >
        <value>tom</value>
    </constructor-arg>
    <constructor-arg  >
        <value>1</value>
    </constructor-arg>
</bean>

A construction parameter, corresponding to a construction parameter of the construction method, and the order is corresponding
insert image description here

2. Overloading of constructors

Let's first review what is method overloading

             1.在同一个类中
             2.方法名相同
             3.形参列表不同(个数,类型,顺序)
             4.方法的重载和返回值类型无关,和权限修饰符也无关

So when the constructor is overloaded, what details do we need to pay attention to when we want to construct injection to assign values ​​to member variables?

2.1 The number of parameters is different

public class Customer implements Serializable {
    
    
    private String name;
    private int age;

    public Customer(String name) {
    
    
        this.name = name;
    }

    public Customer(String name, int age) {
    
    
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
    
    
        return "Customer{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

If we only want to assign a value to the name attribute now, then age will have a default value

<bean id="customer" class="com.zyh.basic.Customer">
    <constructor-arg >
        <value>jack</value>
    </constructor-arg>
 
</bean>

insert image description here

总结:
	我们可以发现当构造参数个数不同的时候,我们可以通过
	<constructor-arg >标签的数量来进行区分

2.2 When the number of construction parameters is the same

Let's look at another situation. When there are two constructors with only one parameter in the overloaded constructor, how does Spring know which member variable we want to assign a value to. At this time, we should distinguish by the type of member variables

通过标签引入type属性,进行类型区分
<bean id="customer" class="com.zyh.basic.Customer">
    <constructor-arg type="int" >
        <value>3</value>
    </constructor-arg>
 
</bean>
public class Customer implements Serializable {
    
    
    private String name;
    private int age;

    public Customer(String name) {
    
    
        this.name = name;
    }

    public Customer(int age) {
    
    
        this.age = age;
    }

    public Customer(String name, int age) {
    
    
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
    
    
        return "Customer{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

The type attribute indicates the type of parameter to be assigned

<bean id="customer" class="com.zyh.basic.Customer">
    <constructor-arg type="int" >
        <value>3</value>
    </constructor-arg>
 
</bean>

insert image description here
At this time, we will still have a doubt, what if there are two member variables of the same type?
Haha, everyone must have thought carefully about this, but you may have forgotten the definition of overloading. We say that overloading refers to the number, type, and order of parameters. A difference in the order constitutes overloading, but pay attention , we never said that different parameter names constitute overloading, which is a syntax error. After all, we have to remember that parameter names can be arbitrarily chosen.

3. Inject summary

  • set injection is used more
  • Constructive injection is relatively troublesome after all, after all, there is the problem of overloading constructors
  • In the Spring framework, set injection is widely used

Guess you like

Origin blog.csdn.net/qq_52797170/article/details/124219835