Spring xml management mode (2 properties)

Method Properties injected 1.spring supported: constructor injection, setter injection method

User class, attribute name, age

public class User {                          
                                             
    private String name;                     
                                             
    private Integer age;                     
                                             
    public User(String name, Integer age) {  
        this.name = name;                    
        this.age = age;                      
    }            
 public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public User() {
}
@Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } } 

2. constructor injection - <constructor-Arg> (name, value, index, type, REF)

 

When the property has been the object of it?

 

The Replace <constructor-arg> or <property> ref to the value into, ref is the value of the object in the xml attribute beanId

 

<bean id="user" class="com.pheony.demo4.User" >
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="age" value="35"></constructor-arg>
</bean>

The method of injection 3.setter - <Property> (name, value, REF)

    <bean id="user1" class="com.pheony.demo4.User" >
        <property name="name" value="李四"></property>
        <property name="age" value="18"></property>
    </bean>

Note: Use setter not only to add property set method, when there have arg constructor, we need to add a constructor with no arguments, otherwise there is no <constructor-arg> will complain

xml namespace ------- p simplified configuration (after introducing Spring2.5)

<bean id="user2" class="com.pheony.demo4.User" p:name="王五" p:age="23" p:cat-ref="cat" />

SpEL injection: spring expression language to simplify dependence

      Syntax: value = "# {} Expression"

 

4. The injection of the complex property

 

    Array type private String [] arrs;

        <property name="arrs">                     
            <list>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </list>
        </property>

    List collection type private List <String> list;

        <property name="list">
            <list>
                <value>111</value>
                <value>222</value>
                <value>333</value>
            </list>
        </property>

    Set collection type private Set <String> set;

        <property name="set">
            <set>
                <value>ddd</value>
                <value>eee</value>
                <value>fff</value>
            </set>
        </property>

    Map collection type private Map <String, Integer> map;

        <property name="map">
            <map>
                <entry key="a" value="1"></entry>
                <entry key="b" value="2"></entry>
                <entry key="c" value="3"></entry>
            </map>
        </property>

    Properties类型  private Properties properties;

        <property name="properties">
            <props>
                <prop key="username" >root</prop>
                <prop key="password" >root</prop>
            </props>
        </property>

 

Guess you like

Origin www.cnblogs.com/sycamore0802/p/11732127.html