Spring 在xml中对象属性注入的方式

创建bean:

public class Student {

    private Integer sid;
    private Integer sage;
    private String sname;
    private String sclass;

    public Student(){
        System.out.println("Student 的无参构造");
    }

    public Student(Integer sid, Integer sage, String sname, String sclass) {
        System.out.println("Student 的有参构造");
        this.sid = sid;
        this.sage = sage;
        this.sname = sname;
        this.sclass = sclass;
    }

    public void setSid(Integer sid) {
        System.out.println("Student 的 setSid 方法");
        this.sid = sid;
    }

    public void setSage(Integer sage) {
        System.out.println("Student 的 setSage 方法");
        this.sage = sage;
    }

    public void setSname(String sname) {
        System.out.println("Student 的 setSname 方法");
        this.sname = sname;
    }

    public void setSclass(String sclass) {
        System.out.println("Student 的 setSclass 方法");
        this.sclass = sclass;
    }

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

方式一

在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">
        <!-- 在属性中注入 -->
        <constructor-arg name="sid" value="101" type="java.lang.Integer"></constructor-arg>
        <!-- 使用 value 标签注入 -->
        <constructor-arg name="sage">
            <value type="java.lang.Integer">15</value>
        </constructor-arg>
        <constructor-arg name="sname" value="aaa" type="java.lang.String"></constructor-arg>
        <constructor-arg name="sclass">
            <value type="java.lang.String">1班</value>
        </constructor-arg>
    </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 的有参构造
Student{sid=101, sage=15, sname='aaa', sclass='1班'}

Process finished with exit code 0

结论:
使用 constructor-arg 标签是通过 bean 中定义的有参构造方法直接创建的对象

方式二

在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">
        <!-- 在属性中注入,没有 type 属性 -->
        <property name="sid" value="102"></property>
        <!-- 使用 value 标签注入,有 type 属性 -->
        <property name="sage">
            <value type="java.lang.Integer">20</value>
        </property>
        <property name="sclass">
            <value type="java.lang.String">2班</value>
        </property>
        <property name="sname" value="bbb"></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 的无参构造
Student 的 setSid 方法
Student 的 setSage 方法
Student 的 setSclass 方法
Student 的 setSname 方法
Student{sid=102, sage=20, sname='bbb', sclass='2班'}

Process finished with exit code 0

结论:
使用 property 标签是通过 bean 中的无参构造方法创建的对象,通过其定义的 setxxx 方法进行的赋值,调用顺序与标签中定义的顺序一样

posted @ 2022-03-07 18:04  叕叕666  阅读(104)  评论(0)    收藏  举报