详解Spring注入集合(数组、List、Map、Set)类型属性

 更新时间:2021年01月19日 09:29:49   作者:牛哄哄的柯南  
这篇文章主要介绍了详解Spring注入集合(数组、List、Map、Set)类型属性,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

注入集合(数组、List、Map、Set)类型属性

(1)创建类,定义数组,list,map,set类型属性,并且生成对应的set方法。
(2)在spring配置文件中进行配置。

Stu类:

package com.Keafmd.spring5.collectiontype;

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

/**
 * Keafmd
 *
 * @ClassName: Stu
 * @Description: IOC操作Bean管理(xml注入属性集合)
 * @author: 牛哄哄的柯南
 * @date: 2021-01-15 14:15
 */
public class Stu {

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

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

  //3、map集合类型属性
  private Map<String,String> maps;

  //4、set集合类型属性
  private Set<String> sets;


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

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

  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(Arrays.toString(courses));
    System.out.println(list);
    System.out.println(maps);
    System.out.println(sets);
    System.out.println(courseList);
  }

}

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

  <!--集合类型属性注入-->
  <bean id="stu" class="com.Keafmd.spring5.collectiontype.Stu">
    <!--数组类型属性注入-->
    <property name="courses">
      <array>
        <value>Java</value>
        <value>C++</value>
        <value>Python</value>
      </array>
    </property>

    <!--list类型属性注入-->
    <property name="list">
      <list>
        <value>小明</value>
        <value>小红</value>
      </list>
    </property>

    <!--map类型属性注入-->
    <property name="maps">
      <map>
        <entry key="Java" value="java"></entry>
        <entry key="C++" value="c++"></entry>
      </map>
    </property>

    <!--set类型属性注入-->
    <property name="sets">
      <set>
        <value>北京</value>
        <value>上海</value>
      </set>
    </property>

    <!--注入list集合类型,值是对象-->
    <property name="courseList">
      <list>
        <ref bean="course1"></ref>
        <ref bean="course2"></ref>
      </list>
    </property>
  </bean>

  <!--创建多个course对象-->
  <bean id="course1" class="com.Keafmd.spring5.collectiontype.Course">
    <property name="cname" value="Spring5框架"></property>
  </bean>

  <bean id="course2" class="com.Keafmd.spring5.collectiontype.Course">
    <property name="cname" value="MyBatis框架"></property>
  </bean>
</beans>

测试类:

package com.Keafmd.spring5.testdemo;
import com.Keafmd.spring5.bean.Orders;
import com.Keafmd.spring5.collectiontype.Book;
import com.Keafmd.spring5.collectiontype.Course;
import com.Keafmd.spring5.collectiontype.Stu;
import com.Keafmd.spring5.factorybean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Keafmd
 *
 * @ClassName: TestSpring5demo1
 * @Description: 测试类
 * @author: 牛哄哄的柯南
 * @date: 2021-01-15 14:30
 */
public class TestSpring5demo1 {

  @Test
  public void testCollection1(){

    ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
    Stu stu = context.getBean("stu",Stu.class);
    stu.test();
  }
}

输出结果:

[Java, C++, Python]
[小明, 小红]
{Java=java, C++=c++}
[北京, 上海]
[Course{cname='Spring5框架'}, Course{cname='MyBatis框架'}]

Process finished with exit code 0

把集合注入部分提取出来

(1)在spring配置文件中引入名称空间util(
在配置信息中添加xmlns:util="http://www.springframework.org/schema/util"和http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd")。
(2)提取list集合类型属性注入。
(3)把提取的list集合类型属性注入使用。

Book类:

package com.Keafmd.spring5.collectiontype;
import java.util.List;

/**
 * Keafmd
 *
 * @ClassName: Book
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-01-15 14:56
 */
public class Book {
  private List<String> list;

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

 
bean2.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: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.Keafmd.spring5.collectiontype.Book">
    <property name="list" ref="bookList"></property>
  </bean>
</beans>

测试代码:

package com.Keafmd.spring5.testdemo;
import com.Keafmd.spring5.bean.Orders;
import com.Keafmd.spring5.collectiontype.Book;
import com.Keafmd.spring5.collectiontype.Course;
import com.Keafmd.spring5.collectiontype.Stu;
import com.Keafmd.spring5.factorybean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Keafmd
 *
 * @ClassName: TestSpring5demo1
 * @Description: 测试类
 * @author: 牛哄哄的柯南
 * @date: 2021-01-15 14:30
 */
public class TestSpring5demo1 {

  @Test
  public void testCollection2(){

    ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
    Book book = context.getBean("book",Book.class);
    book.test();

  }
}

输出结果:

[老人与海, 平凡的世界, 阿甘正传]

Process finished with exit code 0

到此这篇关于详解Spring注入集合(数组、List、Map、Set)类型属性的文章就介绍到这了,更多相关Spring注入集合内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 微信小程序与Java后端接口交互

    微信小程序与Java后端接口交互

    本文主要介绍了微信小程序与Java后端接口交互,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • Java逃逸分析详解及代码示例

    Java逃逸分析详解及代码示例

    这篇文章主要介绍了Java逃逸分析详解及代码示例,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • @Transactional注解不起作用的原因分析及解决

    @Transactional注解不起作用的原因分析及解决

    这篇文章主要介绍了@Transactional注解不起作用的原因分析及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • Kafka多节点分布式集群搭建实现过程详解

    Kafka多节点分布式集群搭建实现过程详解

    这篇文章主要介绍了Kafka多节点分布式集群搭建实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • eclipse java工程改造为java web工程详解

    eclipse java工程改造为java web工程详解

    这篇文章主要介绍了eclipse java工程改造为java web工程详解的相关资料,需要的朋友可以参考下
    2017-05-05
  • 一文搞懂JAVA 枚举(enum)

    一文搞懂JAVA 枚举(enum)

    这篇文章主要介绍了JAVA 枚举(enum)的相关资料,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • 使用Nacos实现动态路由的步骤和代码示例

    使用Nacos实现动态路由的步骤和代码示例

    这篇文章主要介绍了使用 Nacos 实现 Spring Cloud Gateway 的动态路由,本文给大家介绍了具体的实现步骤和代码案例,感兴趣的小伙伴跟着小编一起来看看吧
    2024-09-09
  • 如何用java实现分页查询

    如何用java实现分页查询

    这篇文章主要介绍了如何用java实现分页查询,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06
  • springboot之redis cache TTL选项的使用

    springboot之redis cache TTL选项的使用

    这篇文章主要介绍了springboot之redis cache TTL选项的使用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • Java设计模式之策略模式详解

    Java设计模式之策略模式详解

    这篇文章主要为大家详细介绍了Java设计模式之策略模式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10

最新评论