Spring Framework (IV): spring dependency injection

A, spring dependency injection Introduction

1, depending on the definition of injection

DI (Dependency Inject, dependency injection), the vessel to establish the dependencies between object by calling the constructor or set method.

2, the role of dependency injection

spring dependency injection core container by establishing dependencies between objects, reducing the coupling between programs.

Two, spring-dependent manner injection

1, using the constructor injection

1.1, using the configured injection constructor-arg tag

<!--构造方法注入-->
<bean id="constructor" class="com.wedu.spring02.entity.UserInfo">
    <constructor-arg name="id" value="1"/>
    <constructor-arg name="username" value="test"/>
    <constructor-arg name="password" value="123456"/>
    <constructor-arg name="age" value="25"/>
    <constructor-arg name="regtime" ref="now"/>
    <constructor-arg name="siteaddress" value="https://mp.csdn.net/postedit/103544678"/>
</bean>

 1.2, constructor-arg tag attributes

  • type: specifies the data type of the data to be injected.
  • index: injecting data used to specify the index position specified parameter assignment to the constructor. The position of the index is to start from zero.
  • name: the constructor specifies the name specified in the parameter assignment.
  • value: String for providing basic types and data types.
  • ref: other bean types for the specified data.

1.3, the advantages and disadvantages

  • Pros: When obtaining bean object, inject data is a must, otherwise the object can not be created successfully.
  • Disadvantages: changing the way the bean is instantiated object, so that when we create objects, if less than this data must also be provided.

2, using the method set injection

2.1, set injection method using a tag property

<!--set方法注入-->
<bean id="property" class="com.wedu.spring02.entity.UserInfo">
    <property name="id" value="1"/>
    <property name="username" value="test"/>
    <property name="password" value="123456"/>
    <property name="age" value="25"/>
    <property name="regtime" ref="now"/>
    <property name="siteaddress" value="https://mp.csdn.net/postedit/103544678"/>
</bean>

Properties 2.2, property labels

  • name: the name of the method used to specify the set when injected into the call.
  • value: String for providing basic types and data types.
  • ref: other bean types for the specified data.

2.3, the advantages and disadvantages

  • Advantages: there is no clear limit when you create an object, you can directly use the default constructor
  • Disadvantages: If there is a member must have a value, it is possible to get the object set method is not implemented.

Third, the dependency of the complex type injection

<!--复杂类型注入-->
<bean id="userInfo" class="com.wedu.spring02.entity.UserInfo">
    <!--数组类型注入-->
    <property name="arr">
        <array>
            <value>hello</value>
            <value>world</value>
        </array>
    </property>
    <!--list集合类型注入-->
    <property name="list">
        <list>
            <value>hello</value>
            <value>world</value>
        </list>
    </property>
    <!--set集合类型注入-->
    <property name="set">
        <set>
            <value>hello</value>
            <value>world</value>
        </set>
    </property>
    <!--map集合类型注入-->
    <property name="map">
        <map>
            <entry key="zhangsan" value="18"/>
            <entry key="lisi">
                <value>25</value>
            </entry>
        </map>
    </property>
    <!--properties类型注入-->
    <property name="properties">
        <props>
            <prop key="username">赵六</prop>
            <prop key="password">123456</prop>
        </props>
    </property>
</bean>

 

Published 134 original articles · won praise 10 · views 7358

Guess you like

Origin blog.csdn.net/yu1755128147/article/details/103544678