IOC操作Bean管理XML方式(注入集合类型属性)

该博客介绍了如何在Spring中通过XML配置管理Bean,特别是处理集合类型属性的注入,包括数组、List、Map和Set。文章详细讲解了从环境搭建、实体类创建、Spring配置到测试类编写的过程,强调了在集合中注入对象类型的注意事项,并展示了如何通过util命名空间和标签将集合注入部分提取为通用代码。
摘要由CSDN通过智能技术生成

目录

IOC操作Bean管理XML方式(注入集合类型属性)

(1)首先进行环境的搭建和准备

(2)创建一个类:用来完成集合类型属性注入

(3)在Spring 配置文件进行配置

(4)编写一个测试类进行测试

(5)运行结果:

(6)注意细节:

(7)据上面所述,在集合里面设置对象类型值

(8)把集合注入部分提取出来,变成通用部分

(8.1)在Spring 配置文件中引入名称空间 util

(8.2)使用 util 标签完成 list 集合注入提取


 

IOC操作Bean管理XML方式(注入集合类型属性)

1.注入数组类型属性

2.注入 List 集合类型属性

3.注入 Map 集合类型属性

 

 

 

(1)首先进行环境的搭建和准备

新建一个collectiontype包

 

(2)创建一个类:用来完成集合类型属性注入

进行实体类编写

 

写出三个基本类型

包括:数组类型、List集合、Map集合、Set集合(同时生成对应的set方法)

package com.lbj.spring5.collectiontype;

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


public class Student {

    //1.数组类型属性
    private String[] courses;

    //2.list 集合类型属性
    private List<String> list;

    //3.map 集合类型属性
    private Map<String,String> maps;
    
    //4.set 集合类型属性
    private Set<String> sets;

    public void setCourses(String[] courses) {
        this.courses = courses;
    }

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

    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }

    public void setSets(Set<String> sets) {
        this.sets = sets;
    }
//    写一个测试方法方便输出查看
    public void test(){
//        数组如果直接输出的话不是一个 数值
//        System.out.println(courses);

//        因此我们引入工具类来方便输出
        System.out.println(Arrays.toString(courses));
        System.out.println(list);
        System.out.println(maps);
        System.out.println(sets);
    }
}

 

(3)在Spring 配置文件进行配置

注意:<property name="courses" value="">其中的value=""只能写入一个值,并不符合数组多个值的要求

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--集合类型属性注入-->

    <bean id="student" class="com.lbj.spring5.collectiontype.Student">

        <!--1.数组类型属性注入-->
        <property name="courses" >
            <array>
                <value>语文</value>
                <value>英语</value>
                <value>数学</value>
            </array>
        </property>

        <!--2.List类型属性注入-->
        <property name="list">
            <list>
                <value>张三</value>
                <value>王五</value>
            </list>
        </property>

        <!--3.Map类型属性注入-->
        <property name="maps">
            <map>
                <entry key="JAVA" value="java"></entry>
                <entry key="PHP" value="php"></entry>
            </map>
        </property>

        <!--4.Set类型属性注入-->
        <property name="sets">
            <set>
                <value>123</value>
                <value>321</value>
            </set>
        </property>

    </bean>
</beans>

 

(4)编写一个测试类进行测试

创建一个新的包 testdemo包

在 testdemo包 新建一个 TestSpring5Demo1类

代码如下:

package com.lbj.spring5.testdemo;

import com.lbj.spring5.collectiontype.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class TestSpring5Demo1 {

    @Test
    public void tsetCollection(){
        ApplicationContext context=
                new ClassPathXmlApplicationContext("bean1.xml");
        Student student=context.getBean("student", Student.class);
        student.test();
    }
}

(5)运行结果:

 

(6)注意细节:

细节1:在上面例子中,集合的类型都是String 字符串类型

思考,以后在String类型中,写入的是对象,那该如何处理呢?

 

细节2:例子所示的4个集合都只能在Student类内进行调用使用,但是以后需要这些集合可以被其它类调用,该如何处理

思考:集合是不是可以进行抽取,做成公共部分?

 

(7)据上面所述,在集合里面设置对象类型值

步骤:新建一个课程类 Course

Course类代码如下:

package com.lbj.spring5.collectiontype;

/**
 * 课程类
 */
public class Course {
//    课程名称
    private String cname;

    public void setCname(String cname) {
        this.cname = cname;
    }

//    为了方便输出才写这个方法,正常情况下输出的值为[com.lbj.spring5.collectiontype.Course@306279ee, com.lbj.spring5.collectiontype.Course@545997b1]
//    写了这个toString 方法后我们就可以进行正常值的输出
    @Override
    public String toString() {
        return "Course{" +
                "cname='" + cname + '\'' +
                '}';
    }
}

 

下一步,到Student类中,补充如下代码:

//    学生所学多门课程
    private List<Course> courseList;

    public void setCourseList(List<Course> courseList) {
        this.courseList = courseList;
    }

 

同样,为了测试,在Student类中的 test() 方法中加入如下输出语句

       System.out.println(courseList);

 

下一步,在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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


        <!--条件:注入的是List集合类型,值是对象的时候-->
        <property name="courseList">
            <list>
                <!--引入对象的id值-->
                <ref bean="course1"></ref>
                <ref bean="course2"></ref>
            </list>
        </property>
    </bean>
    
    <!--操作:创建多个course对象-->
    <!--写入第一个课程-->
    <bean id="course1" class="com.lbj.spring5.collectiontype.Course">
        <property name="cname" value="英语"></property>
    </bean>
    <!--写入第二个课程-->
    <bean id="course2" class="com.lbj.spring5.collectiontype.Course">
        <property name="cname" value="数学"></property>
    </bean>
</beans>

 

进行测试:

 

(8)把集合注入部分提取出来,变成通用部分

步骤:新建一个bean2.xml

 

步骤:新建一个类,专门做提取部分

代码如下:

package com.lbj.spring5.collectiontype;

import java.util.List;

public class Book {
    private List<String> list;

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

//测试方法
    public void test(){
        System.out.println(list);
    }
}

(8.1)在Spring 配置文件中引入名称空间 util

固定写法:

<?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:p="http://www.springframework.org/schema/p"

       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">

</beans>

(8.2)使用 util 标签完成 list 集合注入提取

<?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:p="http://www.springframework.org/schema/p"

       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">

    <!--1.先提取 list 集合类型属性注入-->
    <util:list id="booklist">
        <value>英语书</value>
        <value>数学书</value>
        <value>语文书</value>
    </util:list>

    <!--2.然后将提取 list 集合类型属性注入-->
    <bean id="book" class="com.lbj.spring5.collectiontype.Book">
        <property name="list" ref="booklist"></property>
    </bean>
</beans>

进行测试:

package com.lbj.spring5.testdemo;

import com.lbj.spring5.collectiontype.Book;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class TestSpring5Demo1 {

    @Test
    public void tsetCollection2(){
        ApplicationContext context=
                new ClassPathXmlApplicationContext("bean2.xml");
        Book book=context.getBean("book", Book.class);
        book.test();
    }
}

测试结果:

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值