Spring日常笔记记录04--引用类型的设值

一、创建引用类School

package com.example.ba02;

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

    public void setName(String name) {
        this.name = name;
    }

    public void setAddress(String address) {
        this.address = address;
    }

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

 

二、在Student中声明引用类

package com.example.ba02;

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

    //声明一个引用类型
    private School school;

    public void Student(){
        System.out.println("spring会调用类的无参构造方法创建对象");
    }

    public void setName(String name) {
        System.out.println("setName:"+name);
        this.name = name;
    }

    public void setAge(int age) {
        System.out.println("setAge:"+age);
        this.age = age;
    }

    public void setSchool(School school){
        System.out.println("setSchool:"+school);
        this.school = school;
    }

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

三、编写配置文件

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--声明student对象
注入:就是赋值的意思
简单类型:spring中规定java的基本数据类型和string都是简单类型
di:给属性赋值
1.set注入(设值注入):spring调用类的set方法,可以在set方法中完成属性赋值
1)简单类型的set注入
<bean id="xx" class="yyy">
<property name="属性名字" value="此属性的值"/>
一个property只能给一个属性赋值
<property......>

2)引用类型的set注入:spring调用类的set方法
<bean id="xx" class="yyy">
<property name="属性名字" ref="bean的id(对象的名称)"/>
一个property只能给一个属性赋值
<property......>
</bean>
    <bean id="myStudent" class="com.example.ba02.Student">
        <property name="name" value="李四"/><!--setName("李四")-->
        <property name="age" value="22"/><!--setAge(20)-->
        <property name="school" ref="mySchool"/> <!--setSchool(mySchool)-->
    </bean>

    <!--声明School对象-->
    <bean id="mySchool" class="com.example.ba02.School">
        <property name="name" value="北京大学"/>
        <property name="address" value="北京的海淀区"/>
    </bean>
</beans>

 

四、测试

public class MyTest02 {
    @Test
    public void test01(){
        System.out.println("===test01===");
        String config = "ba02/applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);

        Student student = (Student) ac.getBean("myStudent");
        System.out.println("student对象="+student);
    }

    @Test
    public void test02(){
        System.out.println("===test02===");

        Student student = new Student();
        student.setName("lisi");
        student.setAge(20);

        School school = new School();
        school.setName("donglijiedian");
        school.setAddress("北京");

        student.setSchool(school);

        System.out.println("student:"+student);
    }
}

运行结果:

setName:李四
setAge:22
setSchool:School{name='北京大学', address='北京的海淀区'}
student对象=Student{name='李四', age=22, school=School{name='北京大学', address='北京的海淀区'}}
===test02===
setName:lisi
setAge:20
setSchool:School{name='donglijiedian', address='北京'}
student:Student{name='lisi', age=20, school=School{name='donglijiedian', address='北京'}}

 

 

 
posted @ 2021-07-11 23:57  Brack_Pearl  阅读(48)  评论(0编辑  收藏  举报