其他分享
首页 > 其他分享> > Spring 03: 基于xml的构造方法注入

Spring 03: 基于xml的构造方法注入

作者:互联网

注入方式

参数名称注入

package com.example.pojo03;

public class School {
    private String name;
    private String address;

    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }

    public School(String name, String address) {
        this.name = name;
        this.address = address;
        System.out.println("School有参构造方法执行,实例对象被创建....");
    }
}
<?xml version="1.0" encoding="UTF-8"?>

<!-- bean工厂 -->
<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 http://www.springframework.org/schema/beans/spring-beans.xsd">


    <!-- 通过构造方法的参数名称,注册School实例对象 -->
    <bean id="school" class="com.example.pojo03.School">
        <constructor-arg name="name" value="nefu"/>
        <constructor-arg name="address" value="哈尔滨"/>
    </bean>
    
</beans>
package com.example.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestConstructor {

    //测试:通过构造方法的参数名称注入
    @Test
    public void testConstructorArgs(){
        //创建Spring容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("source03/applicationContext.xml");
        //取出School对象并打印输出
        System.out.printf("School实例对象: " + ac.getBean("school"));
    }
}
School有参构造方法执行,实例对象被创建....
School实例对象: School{name='nefu', address='哈尔滨'}

Process finished with exit code 0

参数下标注入

package com.example.pojo03;

public class Student {
    private String name;
    private int age;
    private School school;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", school=" + school +
                '}';
    }

    public Student(String name, int age, School school) {
        this.name = name;
        this.age = age;
        this.school = school;
        System.out.println("Student有参构造方法执行,实例对象被创建....");
    }
}
    <!-- 通过构造方法的参数下标,注册Student实例对象 -->
    <bean id="student" class="com.example.pojo03.Student">
        <constructor-arg index="0" value="荷包蛋"/>
        <constructor-arg index="1" value="20"/>
        <constructor-arg index="2" ref="school"/>
    </bean>
    //测试:通过构造方法的参数下标注入
    @Test
    public void testConstructorIndex(){
        //创建Spring容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("source03/applicationContext.xml");
        //取出Student对象并打印输出
        System.out.printf("Student实例对象: " + ac.getBean("student"));
    }
School有参构造方法执行,实例对象被创建....
Student有参构造方法执行,实例对象被创建....
Student实例对象: Student{name='荷包蛋', age=20, school=School{name='nefu', address='哈尔滨'}}

Process finished with exit code 0

默认参数顺序注入

    <!-- 通过构造方法默认参数顺序,注册Student实例对象 -->
    <bean id="student02" class="com.example.pojo03.Student">
        <constructor-arg value="荷包蛋"/>
        <constructor-arg value="20"/>
        <constructor-arg ref="school"/>
    </bean>
    //测试:通过构造方法默认参数顺序注入
    @Test
    public void testConstructorDefaultOrder(){
        //创建Spring容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("source03/applicationContext.xml");
        //取出Student对象并打印输出
        System.out.printf("Student实例对象: " + ac.getBean("student02"));
    }
School有参构造方法执行,实例对象被创建....
Student有参构造方法执行,实例对象被创建....
Student实例对象: Student{name='荷包蛋', age=20, school=School{name='nefu', address='哈尔滨'}}

Process finished with exit code 0

注意

前两种注入方式,由于一种依靠参数名和待注入值绑定,一种依靠参数下标和待注入值绑定,做到了注入值与待注入目标一一对应
所以注入标签顺序随意,调换 < constructor-arg />标签的前后顺序,仍可正确注入数据

    <!-- 通过构造方法的参数下标,注册Student实例对象 -->
    <bean id="student" class="com.example.pojo03.Student">
        <constructor-arg index="0" value="荷包蛋"/>
        <constructor-arg index="2" ref="school"/>
        <constructor-arg index="1" value="20"/>
    </bean>
    <!-- 通过构造方法默认参数顺序,注册Student实例对象 -->
    <bean id="student02" class="com.example.pojo03.Student">
        <constructor-arg value="荷包蛋"/>
        <constructor-arg ref="school"/>
        <constructor-arg value="20"/>
    </bean>

image

标签:xml,03,School,name,构造方法,实例,Student,注入
来源: https://www.cnblogs.com/nefu-wangxun/p/16607580.html