20200111 - spring set injection

The method set injection
label involved:
the location of the property occurs in the interior of the bean tag
attribute tag
specified set of method calls upon injection name attribute
value and keep up a constructor as ref

Only set the entity class method

    <bean id="accountService2" class="com.itheima.service.impl.AccountServiceImpl2">
        <property name="name" value="test"></property>
        <property name="age" value="21"></property>
        <property name="birthday" ref="now"></property>
    </bean>

Implementation dependent injection

    <bean id="accountService2" class="com.itheima.service.impl.AccountServiceImpl2">
        <property name="name" value="test"></property>
        <property name="age" value="21"></property>
        <property name="birthday" ref="now"></property>
    </bean>

set method advantage, there is no clear limitation, direct default constructor
drawbacks: if a member must have a value, set method can not guarantee injection

Injection complex data
set of the data injection

 <bean id="accountService3" class="com.itheima.service.impl.AccountServiceImpl3">
        <property name="myStrs">
            <array>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </array>
        </property>
        <property name="myList">
            <list>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </list>
        </property>

name is to write the name of the data, but the internal index to continue with the form of labels. Array with array, a list with the list

List structure used to inject a tag set, there list array set
for injection to set a tag map structure: map props
the same structure, the label may be interchanged

The following map and props add tags

        <property name="myProperty">
            <props>
               <prop key="testC">ccc</prop>
            </props>
        </property>
        <property name="myMap">
            <map>
                <entry key="testA" value="AAA"></entry>
            </map>
        </property>

Content summary

Published 657 original articles · won praise 39 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_36344771/article/details/103939007