Article From:https://www.cnblogs.com/cplinux/p/9735725.html

Dependency injection can be done in two ways:

1 Structure injection, if < bean> label using < contructor-arg> structure injection.

2 setterInjection is the invocation of setter method injection, if & lt; Bean & gt; under the tag use & lt; Property & gt; tag, is the setter injection

  2.1: If the property is a simple type, such as the basic data type or String,

<bean id="peo" class="com.bjsxt.pojo.People">
    <property name="id" value="222"></property>
    <property name="name" value=" Zhang three "></property>
</bean>

The above writing is equivalent to the following.

<bean id="peo" class="com.bjsxt.pojo.People">
    <property name="id">
        <value>456</value>
    </property>
    <property name="name">
        <value>zhangsan</value>
    </property>
</bean>    

  2.2 Complex type injection

    2.2.1 The array type set<? > the question mark represents the generic type.

<property name="sets">
    <set>
        <value>1</value>
        <value>2</value>
        <value>3</value>
        <value>4</value>
    </set>
</property>    

  2.2.2 The injection of the list is List< >

<property name="list">
    <list>
        <value>1</value>
        <value>2</value>
        <value>3</value>
    </list>
</property>

If List<? > the element is the object.

The following is as follows:

            <property name="aList">
                <list>
                    <ref bean="a"/>
                </list>
            </property>    

If there is only one value in list

<property name="list" value="1">
</property>

  2.2.3 The injected type is an array, and if there is only one value in the array, it can be assigned directly through the value attribute, similarly, if the array element is an object, & lt; array & gt; under the tag, & lt; ref & gt; label;

<property name="strs" >
    <array>
        <value>1</value>
        <value>2</value>
        <value>3</value>
    </array>
</property>

  2.2.4 If the data type is Map,

<property name="map">
    <map>
        <entry key="a" value="b" > </entry>
       
        <entry key="c" value="d" ></entry>

    </map>
</property>    

If the value is an object, it is as follows

            <property name="map">
                <map >
                    <entry key="" value-ref=""></entry>
                </map>
            </property>    

<entry> Tags include key, key-ref, value, value-ref and other attributes.

 

  2.4.5  If attribute Properties type

<property name="demo">
    <props>
        <prop key="key">value</prop>
        <prop key="key1">value1</prop>
    </props>
</property>

 

Leave a Reply

Your email address will not be published. Required fields are marked *