Summer self-study day 17 | spring (III)


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


    <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 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


<!--     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>
  • 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


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

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