spring - property和constructor-arg的區別

配置文件的根元素是beans,每一個組件使用bean元素來定義,bean元素能夠有許多屬性,其中有兩個是必須的:id和class。id表示組件的默認名稱,class表示組件的類型。java

1、區別spring

constructor-arg與property都是依賴注入的方式app

constructor-arg:經過構造函數注入。  則property:經過setxx方法注入。 ide

2、constructor-arg使用實例函數

一、Model代碼:測試

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

 二、xml配置:this

<!--經過構造函數,需根據函數值 所有設置-->
<bean id="student" class="com.xyai.Student">
<!-- 能夠根據index或者name設置屬性,下標從0開始 -->
    <constructor-arg name="0" value="1"/>
    <constructor-arg name="name" value="zyf"/>
    <constructor-arg index="2" value="xyai"/>
    <constructor-arg name="dream">
        <list>
            <value>value1</value>
            <value>value2</value>
            <value>value3</value>
        </list>
    </constructor-arg>
    <constructor-arg name="score">
        <map>
            <entry key="key1" value="10"/>
            <entry key="key2" value="20"/>
        </map>
    </constructor-arg>
<!-- boolean的值既能夠用0/1填充,也能夠用true/false填充 -->
    <constructor-arg name="graduation" value="false"/>
</bean>

3、property使用實例code

一、Model代碼:xml

public class Teacher {
    private Integer id;
    private String name;
    private List<User> users= new ArrayList<>();                                                     
    public Integer getId() {
        return id;
    }
                                                         
    public void setId(Integer id) {
        this.id = id;
    }
                                                         
    public String getName() {
        return name;
    }
                                                         
    public void setName(String name) {
        this.name = name;
    }
    public List<User> getUsers() {
        return Users;
    }

    public void setFriends(List<User> users) {
        this.users = users;
    }                                                     
    @Override
    public String toString() {
        return "Teacher [id=" + id + ", name=" + name + ", users=" +users+ "]";
    }
                                                         
}

 二、xml配置:對象

<bean id="teacher" class="com.xyai.Teacher">
<!-- 經過setXXX方法,因此Teacher對象屬性不用所有設置,及id、name、users可設置可不設置-->
    <property name="id" value="1"></property>
    <property name="name" value="zyf"></property>
    <property name="users">         
            <list>                        
                <ref bean="xyai2"/>    
                <ref bean="xyai3"/>
            </list>
        </property>
    <bean id="xyai2" class="com.xyai.Teacher">
        <property name="id" value="2"/>
        <property name="name" value="zyf"/>
    </bean>
    <bean id="xyai3" class="com.xyai.Teacher">
        <property name="id" value="3"/>
        <property name="name" value="zyf"/>
    </bean>
</bean>

4、Test

1.測試代碼

public class Test {
    public static void main(String[] args) {
        //1.讀取spring初始化的配置文件  
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        //2.根據bean獲取Student實現類對象 student                            
        Student student = (Student) context.getBean("student");
        System.out.println(student);
                                  
        Teacher teacher = (Teacher) context.getBean("teacher");
        System.out.println(teacher);
    }
}
相關文章
相關標籤/搜索