Spring-- dependency injection (DI) Detailed

Disclaimer : This blog is just a beginner learning record, experience summary, there are many errors which certainly does not have the reference value, welcome chiefs correct me, thank you! I want to exchange, learn together and progress together friends can add my micro letter Liu__66666666

This is a simple record after learning it again, later will be modified.

First, the concept is introduced

We previously Spring - IOC already know how to create an empty instance of an object, but this is an instance of an object without any, are default values set the member variables, such as int type defaults to 0, the default reference type is null.

What if we want to create objects with specific attributes, and that now there is a Computer class, he has a property "model." So the factory when the property is long since been set up, and how that code to the factory setting this attribute it? In fact, dependency injection (DI, Dependency Injection) work is to set the property in Spring Bean (the value of member variables).

Baidu Encyclopedia concept: The so-called dependency injection, refers to the program is running, if you need assistance call another object, without having to create the caller is in the code, but instead relies on external injection. Spring's dependency injection almost no requirement for the caller and the callee, fully supports the management of dependencies between the POJO.

Second, the dependency injection

Dependency Injection There are three common:

    - 通过构造方法注入
    - setter方法注入
    - 接口注入

There are Apple's "MacBook pro 2019" computer models are "macOS Mojave" system, then how to set up models and operating systems of these two attributes? Here we are in the computer, for example explore!

First built project and import jar package, created applicationContext.xml file, the procedure reference Spring - IOC

Project directory structure is as follows:

image-20190923232101092

Computer class:

public class Computer {

    private String type;//电脑型号
    private OperatingSystem operatingSystem;//操作系统
        
    public Computer() {}
    public Computer(String type, OperatingSystem operatingSystem) {
        this.type = type;
        this.operatingSystem = operatingSystem;
    }
//此处略去getter setter toString方法
}

OperatingSystem class (which is a property Computer: Operating system)

public class OperatingSystem {
    private String name;//操作系统名字,例如Windows、macOS
  
  //操作系统版本,例如,当name为Windows,version为10的时候,就是win10
    private String version;

    public OperatingSystem(){}
    public OperatingSystem(String name, String version) {
        this.name = name;
        this.version = version;
    }
//此处略去getter setter toString方法

}

1. Constructor injection (corresponding constructor must write!)

This method is used inside the bean tag Tag dependency injection.

In a configuration method of Example

public Computer(String type, OperatingSystem operatingSystem) {
    this.type = type;
    this.operatingSystem = operatingSystem;
}
  • Written 1 (not recommended, not used):
<!--macOS mojave-->
<bean id="operatingSystem" class="di.OperatingSystem">
    <constructor-arg index="0" value="macOS"/>
    <constructor-arg index="1" value="Mojave"/>
</bean>

<bean id="computer" class="di.Computer">
    <constructor-arg index="0" value="MacBook Pro 2019"/>
    <constructor-arg index="1" ref="operatingSystem"/>
</bean>

Tag is written before, the only difference is that with the label body, label inside of the body is used to set the properties (dependency injection) of.

Each tag represents a constructor-arg parameter configuration method;

Order attribute parameter representative of the index starting from 0, index = "0" represents the String type constructor parameter, index = "1" represents the parameter OperatingSystem;

Attribute value represents the value of the parameters to be passed, for example, this parameter corresponds to the type macOS, version Mojave is, in general, the basic types and attribute value of type String use traditional values.

Since the Computer class OperatingSystem we refer to this object, so you need to define good OperatingSystem this bean, in order for us to be injected into the computer this bean, in fact, OperatingSystem is a dependent of the Computer. In the second bean, a ref attribute, this value is used to pass values ​​can not pass, in the present embodiment ref value of this bean operatingSystem id.

  • Writing 2 (recommended wording, but not commonly used):
<!--macOS mojave-->
<bean id="operatingSystem" class="di.OperatingSystem">
    <constructor-arg name="name" value="macOS"/>
    <constructor-arg name="version" value="Mojave"/>
</bean>

<bean id="computer" class="di.Computer">
    <constructor-arg name="type" value="MacBook Pro 2019"/>
    <constructor-arg name="operatingSystem" ref="operatingSystem"/>
</bean>

Like elsewhere in the wording 1, the only difference is that the wording of 1 index attributes replaced by the name attribute, so directly determined by the parameter name, clarity, less error-prone.

2.setter method injection (be sure to write the setter method empty argument constructor, and all properties !!!)

This method is used inside the bean tag Attribute dependency injection.

In a configuration method of Example

public Computer(String type, OperatingSystem operatingSystem) {
    this.type = type;
    this.operatingSystem = operatingSystem;
}

Injection:

<!--macOS mojave-->
<bean id="operatingSystem" class="di.OperatingSystem">
    <property name="name" value="macOS"/>
    <property name="version" value="Mojave"/>
</bean>

<bean id="computer" class="di.Computer">
    <property name="type" value="MacBook Pro 2019"/>
    <property name="operatingSystem" ref="operatingSystem"/>
</bean>

In fact, almost second way and constructor injection, just put Replaced

3. interface injection (useless, do not say)

Guess you like

Origin www.cnblogs.com/liuhongchen/p/11576346.html