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 DITest {

    private Integer i;
    private String str;
    private String[] sts;
    private List list;
    private Set set;
    private Map map;
    private Teacher teacher;

    public String getStr() {
        return str;
    }

    public DITest() {
    }

    public DITest(Integer i, String str, String[] sts, List list, Set set, Map map, Teacher teacher) {
        this.i = i;
        this.str = str;
        this.sts = sts;
        this.list = list;
        this.set = set;
        this.map = map;
        this.teacher = teacher;
    }

    public void setI(Integer i) {
        this.i = i;
    }

    public void setStr(String str) {
        this.str = str;
    }

    public void setSts(String[] sts) {
        this.sts = sts;
    }

    public void setList(List list) {
        this.list = list;
    }

    public void setSet(Set set) {
        this.set = set;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    @Override
    public String toString() {
        return "DITest{" +
                "i=" + i +
                ", str='" + str + '\'' +
                ", sts=" + Arrays.toString(sts) +
                ", list=" + list +
                ", set=" + set +
                ", map=" + map +
                ", teacher=" + teacher +
                '}';
    }
}

在resources目录下创建spring.spring_ditest.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="ditest" class="com.mine.bean.DITest">
        <!-- Integer 类型的注入 -->
        <property name="i"><value type="java.lang.Integer">1234</value></property>
        <!-- 空值的注入 -->
        <property name="str"><null></null></property>
        <!-- 数组类型的注入 -->
        <property name="sts">
            <array value-type="java.lang.String">
                <value>111</value>
                <value>aaa</value>
            </array>
        </property>
        <!-- list 类型的注入 -->
        <property name="list">
            <list value-type="com.mine.bean.Teacher">
                <bean class="com.mine.bean.Teacher">
                    <property name="tid"><value type="java.lang.Integer">01</value></property>
                    <property name="tname"><value type="java.lang.String">tea01</value></property>
                </bean>
                <bean class="com.mine.bean.Teacher">
                    <property name="tid"><value type="java.lang.Integer">02</value></property>
                    <property name="tname"><value type="java.lang.String">tea02</value></property>
                </bean>
            </list>
        </property>
        <!-- set 类型的注入 -->
        <property name="set">
            <set value-type="java.lang.String">
                <value>qqqq</value>
                <value>wwww</value>
            </set>
        </property>
        <!-- map 类型的注入 -->
        <property name="map">
            <map key-type="java.lang.String" value-type="java.lang.Object">
                <entry key="key1">
                    <value>value1</value>
                </entry>
                <entry key="key2">
                    <bean class="com.mine.bean.Teacher">
                        <property name="tid"><value type="java.lang.Integer">001</value></property>
                        <property name="tname"><value type="java.lang.String">tea001</value></property>
                    </bean>
                </entry>
            </map>
        </property>
        <!-- 对象的注入 -->
        <property name="teacher">
            <bean class="com.mine.bean.Teacher">
                <property name="tid" value="123"></property>
                <property name="tname" value="tea123"></property>
            </bean>
        </property>
    </bean>

</beans>
public class MyMain {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/spring_ditest.xml");
        DITest diTest = (DITest) context.getBean("ditest");
        System.out.println(diTest);
        System.out.println("---------------------------------------------");
        diTest.getStr().length();
    }
}

结果:

DITest{i=1234, str='null', sts=[111, aaa], list=[Teacher{id=1, name='tea01'}, Teacher{id=2, name='tea02'}], set=[qqqq, wwww], map={key1=value1, key2=Teacher{id=1, name='tea001'}}, teacher=Teacher{id=123, name='tea123'}}
---------------------------------------------
Exception in thread "main" java.lang.NullPointerException
	at MyMain.main(MyMain.java:11)

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