Spring bean 中 constructor-arg屬性

1、說明

constructor-arg:通過構造函數注入。
property:通過setter對應的方法注入。

2、constructor-arg的使用示例

(1)、Model代碼:

public class Student {
private Integer id;
private String name;
private List<String> dream;
private Map<String, Integer> score;
private boolean graduation;                                                                                                                                                           
public Student() {
}
public Student(Integer id, String name, List<String> dream,
        Map<String, Integer> score, boolean graduation) {
    this.id = id;
    this.name = name;
    this.dream = dream;
    this.score = score;
    this.graduation = graduation;
}
                                                                                                                                                                       
@Override
public String toString() {
    return "Student [id=" + id + ", name=" + name + ", dream=" + dream
            + ", score=" + score + ", graduation=" + graduation + "]";
}

}

(2)、xml配置:


<bean id="student" class="com.rc.sp.Student">
    <constructor-arg name="id" value="1"/>
    <constructor-arg name="name" value="student"/>
    <constructor-arg name="dream">
        <list>
            <value>soldier</value>
            <value>scientist</value>
            <value>pilot</value>
        </list>
    </constructor-arg>
    <constructor-arg name="score">
        <map>
            <entry key="math" value="90"/>
            <entry key="english" value="85"/>
        </map>
    </constructor-arg>
    <constructor-arg name="graduation" value="false"/>
</bean>

說明:也可以改成方式;boolean的值既可以用0/1填充,也可以用true/false填充。

轉載至 : http://racoguo.blog.51cto.com/2309068/1236379

我覺得這篇是解釋的比較清楚的一篇博客 。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章