Spring框架中注入集合对象

简介: 你好看官,里面请!今天笔者讲的是在Spring框架中关于注入集合对象的用法(有示例!全网最详细!!)不懂可以在评论区留言,我看到会及时回复。注意:本文仅用与学习参考,不可用于商业用途。

关于Spring注入集合对象的用法(有示例!全网最详细!!)

如果觉得写的还可以,点个赞支持一下笔者呗!笔者会持续更新关于Java和大数据有关的文章

1.注入List

下图是配置文件bean的设置

image.png

2.注入Set

下图是配置文件bean的设置

image.png

注意点:set底层存储形式为LinkedHashSet,且会去重,举个例子如果在set里面存入多个相同的值或对象,set只会保留一个。

image.png

<propertyname="rooms"><set><value>2001-总裁办</value><value>2003-总经理办公室</value><value>2010-研发部会议室</value><value>2010-研发部会议室</value></set></property>

3.注入Map

image.png

注意点:采用的LinkedHashMap的方式,也就是说存储数据的顺序和提取数据的顺序是相同的

image.png

4.注入Properties

image.png

案例:

pom.xml

<?xmlversion="1.0" encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.imooc.spring</groupId><artifactId>ioc</artifactId><version>1.0-SNAPSHOT</version><repositories><repository><id>aliyun</id><name>aliyun</name><url>https://maven.aliyun.com/repository/public</url></repository></repositories><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.6.RELEASE</version></dependency></dependencies></project>

applicationContest.xml

<?xmlversion="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><beanid="c1"class="com.imooc.spring.ioc.entity.Computer"><constructor-argname="brand"value="联想"/><constructor-argname="type"value="台式机"/><constructor-argname="sn"value="8389283012"/><constructor-argname="price"value="3085"/></bean><beanclass="com.imooc.spring.ioc.entity.Computer"><constructor-argname="brand"value="微星"/><constructor-argname="type"value="台式机"/><constructor-argname="sn"value="8389280012"/><constructor-argname="price"value="3000"/></bean><beanclass="com.imooc.spring.ioc.entity.Computer"><constructor-argname="brand"value="华硕"/><constructor-argname="type"value="笔记本"/><constructor-argname="sn"value="9089380012"/><constructor-argname="price"value="6000"/></bean><beanid="company"class="com.imooc.spring.ioc.entity.Company"><propertyname="rooms"><set><value>2001-总裁办</value><value>2003-总经理办公室</value><value>2010-研发部会议室</value><value>2010-研发部会议室</value></set></property><propertyname="computers"><map><entrykey="dev-88172"value-ref="c1"/><entrykey="dev-88173"><beanclass="com.imooc.spring.ioc.entity.Computer"><constructor-argname="brand"value="联想"/><constructor-argname="type"value="笔记本"/><constructor-argname="sn"value="1280258012"/><constructor-argname="price"value="5060"/></bean></entry></map></property><propertyname="info"><props><propkey="phone">010-12345678</prop><propkey="address">北京市朝阳区XX路XX大厦</prop><propkey="website">http://www.xxx.com</prop></props></property></bean></beans>

Company实体类

importjava.util.List;
importjava.util.Map;
importjava.util.Properties;
importjava.util.Set;
publicclassCompany {
privateSet<String>rooms;
privateMap<String,Computer>computers;
privatePropertiesinfo;
publicSet<String>getRooms() {
returnrooms;
    }
publicvoidsetRooms(Set<String>rooms) {
this.rooms=rooms;
    }
publicMap<String, Computer>getComputers() {
returncomputers;
    }
publicvoidsetComputers(Map<String, Computer>computers) {
this.computers=computers;
    }
publicPropertiesgetInfo() {
returninfo;
    }
publicvoidsetInfo(Propertiesinfo) {
this.info=info;
    }
@OverridepublicStringtoString() {
return"Company{"+"rooms="+rooms+", computers="+computers+", info="+info+'}';
    }
}

Computer实体类

publicclassComputer {
privateStringbrand;
privateStringtype;
privateStringsn;
privateFloatprice;
publicComputer() {
    }
publicComputer(Stringbrand, Stringtype, Stringsn, Floatprice) {
this.brand=brand;
this.type=type;
this.sn=sn;
this.price=price;
    }
publicStringgetBrand() {
returnbrand;
    }
publicvoidsetBrand(Stringbrand) {
this.brand=brand;
    }
publicStringgetType() {
returntype;
    }
publicvoidsetType(Stringtype) {
this.type=type;
    }
publicStringgetSn() {
returnsn;
    }
publicvoidsetSn(Stringsn) {
this.sn=sn;
    }
publicFloatgetPrice() {
returnprice;
    }
publicvoidsetPrice(Floatprice) {
this.price=price;
    }
@OverridepublicStringtoString() {
return"Computer{"+"brand='"+brand+'\''+", type='"+type+'\''+", sn='"+sn+'\''+", price="+price+'}';
    }
}

springApplication应用类

importcom.imooc.spring.ioc.entity.Company;
importcom.imooc.spring.ioc.entity.Computer;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclassSpringApplication {
publicstaticvoidmain(String[] args) {
ApplicationContextcontext=newClassPathXmlApplicationContext("classpath:applicationContext.xml");
Companycompany=context.getBean("company", Company.class);
System.out.println(company);
Stringwebsite=company.getInfo().getProperty("website");
System.out.println(website);
System.out.println("==============================");
//获取容器内所有beanId数组String[] beanNames=context.getBeanDefinitionNames();
for (StringbeanName:beanNames){
System.out.println(beanName);
System.out.println("类型:"+context.getBean(beanName).getClass().getName());
System.out.println("内容:"+context.getBean(beanName));
        }
Computercomputer=context.getBean("com.imooc.spring.ioc.entity.Computer", Computer.class);
System.out.println(computer.getBrand());
Computercomputer1=context.getBean("com.imooc.spring.ioc.entity.Computer#1", Computer.class);
System.out.println(computer1.getBrand());
    }
}


相关文章
|
10月前
|
安全 Java Ruby
我尝试了所有后端框架 — — 这就是为什么只有 Spring Boot 幸存下来
作者回顾后端开发历程,指出多数框架在生产环境中难堪重负。相比之下,Spring Boot凭借内置安全、稳定扩展、完善生态和企业级支持,成为构建高可用系统的首选,真正经受住了时间与规模的考验。
693 2
|
11月前
|
XML JSON Java
Spring框架中常见注解的使用规则与最佳实践
本文介绍了Spring框架中常见注解的使用规则与最佳实践,重点对比了URL参数与表单参数的区别,并详细说明了@RequestParam、@PathVariable、@RequestBody等注解的应用场景。同时通过表格和案例分析,帮助开发者正确选择参数绑定方式,避免常见误区,提升代码的可读性与安全性。
|
9月前
|
安全 前端开发 Java
《深入理解Spring》:现代Java开发的核心框架
Spring自2003年诞生以来,已成为Java企业级开发的基石,凭借IoC、AOP、声明式编程等核心特性,极大简化了开发复杂度。本系列将深入解析Spring框架核心原理及Spring Boot、Cloud、Security等生态组件,助力开发者构建高效、可扩展的应用体系。(238字)
|
9月前
|
消息中间件 缓存 Java
Spring框架优化:提高Java应用的性能与适应性
以上方法均旨在综合考虑Java Spring 应该程序设计原则, 数据库交互, 编码实践和系统架构布局等多角度因素, 旨在达到高效稳定运转目标同时也易于未来扩展.
757 8
|
10月前
|
监控 Kubernetes Cloud Native
Spring Batch 批处理框架技术详解与实践指南
本文档全面介绍 Spring Batch 批处理框架的核心架构、关键组件和实际应用场景。作为 Spring 生态系统中专门处理大规模数据批处理的框架,Spring Batch 为企业级批处理作业提供了可靠的解决方案。本文将深入探讨其作业流程、组件模型、错误处理机制、性能优化策略以及与现代云原生环境的集成方式,帮助开发者构建高效、稳定的批处理系统。
861 1
|
12月前
|
安全 Java 微服务
Java 最新技术和框架实操:涵盖 JDK 21 新特性与 Spring Security 6.x 安全框架搭建
本文系统整理了Java最新技术与主流框架实操内容,涵盖Java 17+新特性(如模式匹配、文本块、记录类)、Spring Boot 3微服务开发、响应式编程(WebFlux)、容器化部署(Docker+K8s)、测试与CI/CD实践,附完整代码示例和学习资源推荐,助你构建现代Java全栈开发能力。
1016 1
|
11月前
|
Cloud Native Java API
Java Spring框架技术栈选和最新版本及发展史详解(截至2025年8月)-优雅草卓伊凡
Java Spring框架技术栈选和最新版本及发展史详解(截至2025年8月)-优雅草卓伊凡
1737 0
|
XML Java 数据格式
Spring【依赖注入】就是这么简单(二)
在Spring的第二篇中主要讲解了Spring Core模块的使用IOC容器创建对象的问题,Spring Core模块主要是解决对象的创建和对象之间的依赖关系,因此本博文主要讲解如何使用IOC容器来解决对象之间的依赖关系!
308 0
Spring【依赖注入】就是这么简单(二)
|
Java 测试技术 容器
Spring【依赖注入】就是这么简单
前言 在Spring的第二篇中主要讲解了Spring Core模块的使用IOC容器创建对象的问题,Spring Core模块主要是解决对象的创建和对象之间的依赖关系,因此本博文主要讲解如何使用IOC容器来解决对象之间的依赖关系! 回顾以前对象依赖 我们来看一下我们以前关于对象依赖,是怎么的历程 直接new对象 在最开始,我们是直接new对象给serice的userDao属性赋值.
1346 0
|
12月前
|
Java Spring 容器
SpringBoot自动配置的原理是什么?
Spring Boot自动配置核心在于@EnableAutoConfiguration注解,它通过@Import导入配置选择器,加载META-INF/spring.factories中定义的自动配置类。这些类根据@Conditional系列注解判断是否生效。但Spring Boot 3.0后已弃用spring.factories,改用新格式的.imports文件进行配置。
1415 0

热门文章

最新文章