Summer self-study day 17 | spring (III)

Time:2021-12-5

Spring dependency injection

The objects of other classes that need to be used in the current class are provided by spring. The developer only needs to describe them in the configuration file.
The maintenance of this dependency is dependency injection.
There are three types of data that can be injected:

  • Basic type and string
  • Other bean types (beans configured in configuration files or annotations)
  • Complex type, collection type

Three injection methods:

  • Provided by constructor
  • Using the set method
  • Use annotation to provide

Provided by constructor

Use the constructor Arg tag in the bean tag to assign values to the parameters of the constructor.
Attributes used:

  • Name: parameter with specified name in constructor
  • Type: parameter that specifies the data type
  • Index: Specifies the parameters of the index
  • Value: value of basic data type and string type
  • Ref: other bean type data, such as date

Example
bean.xml

    <bean id="accountService" class="spring.selflearning.service.impl.AccountServiceImpl">
        <constructor-arg name = "name" value = "test"></constructor-arg>
        <constructor-arg name = "age" value = "18"></constructor-arg>
        <constructor-arg name = "birthday" ref = "now"></constructor-arg>
    </bean>

    <bean id = "now" class = "java.util.Date"></bean>
  • Advantage: when obtaining a bean object, injecting data is a necessary operation, otherwise the object cannot be created successfully
  • Disadvantages: parameters that do not need to be assigned have to be assigned

Injected by the set method (more commonly used)

  • Use the property tag in the bean tag
  • The available attributes are name, value, Ref

Example
bean.xml

<!--     Set method injection -- >
    <bean id="accountService2" class="spring.selflearning.service.impl.AccountServiceImpl2">
        <property name="name" value="test"></property>
        <property name="age" value="21"></property>
        <property name="birthday" ref="now"></property>
    </bean>
  • If the set method is used for injection, the class must have either a parameterized or nonparametric constructor, or it cannot have a parameterized constructor, otherwise an error will be reported during injection.
  • Advantages: there are no explicit restrictions when creating objects, and you can directly use the default constructor
  • Disadvantages: if a member must have a value, the set method may not be executed when obtaining the object.

Set injection

  • List, map, set, props and other tags are used in the property tag
  • In the above tags, list and set are assigned with the value tag; Map is assigned with the entry tag; Props uses the prop tag to assign values

Example
bean.xml

<!--     Injection of complex types -- >
    <bean id="accountService3" class="spring.selflearning.service.impl.AccountServiceImpl3">
        <property name="myStrs">
            <array>
                <value>AAA</value>
            </array>
        </property>
        <property name="myList">
            <list>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </list>
        </property>
        <property name="mySet">
            <set>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </set>
        </property>
        <property name="myMap">
            <map>
                <entry value = "AAA" key = "aaa"></entry>
                <entry value = "BBB" key = "bbb"></entry>
                <entry value = "CCC" key = "ccc"></entry>
            </map>
        </property>
        <property name="myProps">
            <props>
                <prop key = "aaa">AAA</prop>
                <prop key = "bbb">BBB</prop>
                <prop key = "ccc">CCC</prop>
            </props>
        </property>
    </bean>

This work adoptsCC agreement, reprint must indicate the author and the link to this article