Spring injection properties in several ways

1. Method 1: Method by a set injection

    <bean id="user" class="com.fuke.domain.User">
        <property name="username" value="张三"/>
        <property name="password" value="1234"/>
    </bean>
@Data
public class User {
    private String username;
    private String password;
}

2. injected through the constructor

<bean id="user" class="com.fuke.domain.User">
        <constructor-arg name="username" value="张三"/>
        <constructor-arg name="password" value="1234"/>
    </bean>
public class User {
    private String username;
    private String password;
    public User(String username,String password){
        this.username = username;
        this.password = password;
    }
}

3.p namespace injection

Premise of this method is the introduction of labels, note that in this way can not be injected into the formation, only the injection mode set
xmlns: p = "http://www.springframework.org/schema/p"

<bean id="user" class="com.fuke.domain.User" p:password="132" p:username="张三">
</bean>

EL expression property injection

<bean id="dog" class="com.fuke.domain.Dog">
</bean>
<bean id="user" class="com.fuke.domain.User" >
    <property name="username" value="#{'张三'}"/>
    <property name="password" value="#{'李四'}"/>
    <property name="age" value="#{20}"/>
    <property name="dog" value="#{dog}"/>
</bean>

Injection properties arrays and collections

1. Array injection

        <property name="attr">
            <list>
                <value>13</value>
                <value>14</value>
            </list>
        </property>

2.list collection injection

 <property name="mylist">
            <list>
                <value>13</value>
                <value>14</value>
            </list>
        </property>

3.set collection injection

<property name="myset">
            <set>
                <value>000</value>
                <value>001</value>
                <value>002</value>
            </set>
        </property>

4.map collection injection

<property name="mymap">
            <map>
                <entry key="key01" value="value01"/>
                <entry key="key02" value="value02"/>
            </map>
        </property>
Published 47 original articles · won praise 6 · views 2190

Guess you like

Origin blog.csdn.net/weixin_44467251/article/details/102880737