Spring框架中<bean>第二种

使用构造器进行复制,<constructor-arg>

  bean中,其中constructor-arg的顺序要和构造器中一样,或者加name进行控制,或者加index索引:

<bean id="Course" class="org_shitang_entity.Course">
        <constructor-arg value="java"></constructor-arg>
        <constructor-arg value="1"></constructor-arg>
        <constructor-arg ref="teacher"></constructor-arg>
    </bean>
    <bean id="teacher" class="org_shitang_entity.teacher">
        <constructor-arg value="ls"></constructor-arg>
        <constructor-arg value="19"></constructor-arg>
    </bean>

teacher类中:

package org_shitang_entity;

public class teacher {
    private String name;
    private int age;
    public teacher(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public teacher() {
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    
}

Course类中:

package org_shitang_entity;

public class Course {
    private String courseName;
    private int courseHour;
    private teacher teacher1;
    public Course() {
    }
    public Course(String courseName, int courseHour, teacher teacher1) {
        super();
        this.courseName = courseName;
        this.courseHour = courseHour;
        this.teacher1 = teacher1;
    }
    
    public void showInfo(){
        System.out.println(this.courseName+","+this.teacher1.getName()+","+this.courseHour);
    }
    
}

猜你喜欢

转载自www.cnblogs.com/lmff/p/12751977.html
今日推荐