spring中ioc的使用方法

首页 / 🍁编程类 / 正文

需要用到的配置文件

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

       
</beans>

测试需要用到的类

student学生类

import java.util.List;

public class Student {

    private  int sid;

    private int sage;

    private String sname;

    public int getSid() {
        return sid;
    }

    public int getSage() {
        return sage;
    }

    public String getSname() {
        return sname;
    }

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

    public void setSage(int sage) {
        this.sage = sage;
    }

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

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

    public Student() {

    }
}

teacher教师类

import java.util.List;
import java.util.Map;

public class Teacher {

    private int tid;

    private String arr[];

    private List<Student> students;

    private Map<String,Student> studentMap;

    public Map<String, Student> getStudentMap() {
        return studentMap;
    }

    public void setStudentMap(Map<String, Student> studentMap) {
        this.studentMap = studentMap;
    }

    public String[] getArr() {
        return arr;
    }

    public void setArr(String[] arr) {
        this.arr = arr;
    }

    public int getTid() {
        return tid;
    }

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

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "tid=" + tid +
                ", arr=" + Arrays.toString(arr) +
                ", students=" + students +
                ", studentMap=" + studentMap +
                '}';
    }

    public Teacher() {

    }
}

测试类

public class TestClass {

    public static void main(String[] args) {
        //注入

        ApplicationContext ioc=new ClassPathXmlApplicationContext("bean.xml");
        //这种直接把<bean标签里面的id值拿出来
        //Student student = (Student) ioc.getBean("studentone");

        //后面最常用的就是这种,因为一个类只需要弄一次
        //Student student = (Student)ioc.getBean(Student.class);

        //如果同一个类声明了多次,可以这样写前面写id后面写类.class
        //Student student = (Student)ioc.getBean("studentlist",Student.class);
        //System.out.println(student.toString());

        //测试数组
        //Teacher teacher=ioc.getBean("teacherarr",Teacher.class);
        //System.out.println(teacher.toString());

        //测试list
        //Teacher teacher=ioc.getBean("teacherlist",Teacher.class);
        //System.out.println(teacher.toString());

        //测试map集合
        //Teacher teacher=ioc.getBean("teachermap",Teacher.class);
        //System.out.println(teacher.toString());
    }

}

spring中配置文件里面的含义

bean标签用来注入类,id是唯一值,之后调用要用,上面测试类写到,class的路径为类的全限定名
property里面是类中的所有属性

普通注入类型

<bean id="studentone" class="com.xiaonan.spring.Student">
        <property name="sage" value="1" />
        <property name="sid" value="1"/>
        <property name="sname" value="南香香"/>
    </bean>

    <bean id="studenttwo" class="com.xiaonan.spring.Student">
        <property name="sage" value="2" />
        <property name="sid" value="2"/>
        <property name="sname" value="南香香2"/>
    </bean>

    <bean id="studentthree" class="com.xiaonan.spring.Student">
        <property name="sage" value="3" />
        <property name="sid" value="3"/>
        <property name="sname" value="南香香3"/>
    </bean>

数组类型的注入方式
数组这里用到的是teacher类: bean标签和peoperty上面介绍了,重点是list集合和怎么使用数组,数组用array标签,list用ref引入类对象,具体方法ref bean="studentone",这里的bean标签里面就是刚刚写到的id值,写入即可

    <bean id="teacherarr" class="com.xiaonan.spring.Teacher">
        <property name="tid" value="1" />
        <property name="arr">
            <array >
                <value>111</value>
                <value>222</value>
                <value>333</value>
            </array>
        </property>
        <property name="students">
            <list>
                <ref bean="studentone" />
            </list>
        </property>
    </bean>

list集合下面有俩中方法,引入方式有所不同
list集合中如果用上面介绍的方法一就不用管了,如果使用方法二,那就要使用这个标签util:list id="testcherlistref" util:list这个标签,id里面写入的还是唯一值

    <bean id="teacherlist" class="com.xiaonan.spring.Teacher">
        <property name="tid" value="1" />
        <!-- 可以使用 util:list标签 不过我不是很喜欢用这种方式-->
        <property name="students" ref="testcherlistref">
            <!--<list>-->
                <!--&lt;!&ndash;<ref bean="studentone" />&ndash;&gt;-->
                <!--&lt;!&ndash;<ref bean="studenttwo" />&ndash;&gt;-->
                <!--&lt;!&ndash;<ref bean="studentthree" />&ndash;&gt;-->
            <!--</list>-->
        </property>
    </bean>

    <util:list id="testcherlistref">
        <ref bean="studentone" />
        <ref bean="studenttwo" />
        <ref bean="studentthree" />
    </util:list>

map集合的使用方法

map集合中注入属性需要用到,map标签,map标签中的entry标签里面有很多方法,这里我用到了key和value-ref,key就是和java基础阶段接触的map集合一样,然后value-ref这里写入的还是bean标签里面的id值

<bean id="teachermap" class="com.xiaonan.spring.Teacher">
        <property name="tid" value="1" />
        <property name="arr">
            <array >
                <value>111</value>
                <value>222</value>
                <value>333</value>
            </array>
        </property>
        <property name="students">
            <list>
                <ref bean="studentone" />
            </list>
        </property>
        <!--map集合也可以使用util:map标签-->
        <property name="studentMap">
            <map>
                <entry key="mapkey1" value-ref="studentone"/>
                <entry key="mapkey2" value-ref="studenttwo"/>
            </map>
        </property>
    </bean>
评论区
头像