Spring 在xml中进行聚合对象注入

创建两个 bean:

public class Teacher {

    private Integer tid;
    private String tname;

    public Teacher() {
    }

    public Teacher(Integer tid, String tname) {
        this.tid = tid;
        this.tname = tname;
    }

    public void setTid(Integer tid) {
        this.tid = tid;
    }

    public void setTname(String tname) {
        this.tname = tname;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "id=" + tid +
                ", name='" + tname + '\'' +
                '}';
    }
}
public class Student {

    private Integer sid;
    private String sname;
    private Teacher stea;

    public Student(){

    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public void setStea(Teacher stea) {
        this.stea = stea;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", stea=" + stea +
                '}';
    }
}

方式一

在resources目录下创建spring.spring_student.xml,写入:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="teacher" class="com.mine.bean.Teacher">
        <property name="tid"><value type="java.lang.Integer">201</value></property>
        <property name="tname"><value type="java.lang.String">teaOne</value></property>
    </bean>
    <bean id="student" class="com.mine.bean.Student" lazy-init="true">
        <property name="sid">
            <value type="java.lang.Integer">104</value>
        </property>
        <property name="sname">
            <value type="java.lang.String">ddd</value>
        </property>
        <property name="stea" ref="teacher"></property>
    </bean>

</beans>
public class MyMain {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/spring_student.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student);
    }
}

结果:

Student{sid=104, sname='ddd', stea=Teacher{id=201, name='teaOne'}}

Process finished with exit code 0

方式二:
在resources目录下创建spring.spring_student.xml,写入:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="student" class="com.mine.bean.Student" lazy-init="true">
        <property name="sid">
            <value type="java.lang.Integer">105</value>
        </property>
        <property name="sname">
            <value type="java.lang.String">fff</value>
        </property>
        <property name="stea">
            <bean class="com.mine.bean.Teacher">
                <property name="tid"><value type="java.lang.Integer">202</value></property>
                <property name="tname"><value type="java.lang.String">teaTwo</value></property>
            </bean>
        </property>
    </bean>

</beans>
public class MyMain {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/spring_student.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student);
    }
}

结果:

Student{sid=105, sname='fff', stea=Teacher{id=202, name='teaTwo'}}

Process finished with exit code 0
posted @ 2022-03-07 18:04  叕叕666  阅读(36)  评论(0)    收藏  举报