首页 > 其他分享 >Spring 注入集合

Spring 注入集合

时间:2023-09-11 22:34:25浏览次数:32  
标签:Pakistan Elements USA Spring Bean Set 集合 public 注入

使用<property>标签的value属性配置原始数据类型和ref属性配置对象引用的方式来定义Bean配置文件。这两种情况都涉及将单一值传递给Bean。那么如果您想传递多个值,例如Java集合类型,如List、Set、Map和Properties怎么办?为了处理这种情况,Spring提供了四种类型的集合配置元素,如下所示:

序号 元素 & 描述
1 <list>
用于注入一组值,允许重复。
2 <set>
用于注入一组值,但不允许重复。
3 <map>
可用于注入一组名称-值对,其中名称和值可以是任何类型。
4 <props>
可用于注入一组名称-值对,其中名称和值都是字符串。

您可以使用<list><set>来注入java.util.Collection的任何实现或数组。

在处理集合时,通常会遇到两种情况:(a)传递集合的直接值和(b)将Bean的引用作为集合元素之一传递。

示例

假设您已经准备好Eclipse IDE,并采取以下步骤创建Spring应用程序:

步骤 描述

1 创建一个名为SpringExample的项目,在创建的项目中的src文件夹下创建一个名为com.tutorialspoint的包。

2 使用"Add External JARs"选项添加所需的Spring库,如Spring Hello World示例章节中所述。

3 在com.tutorialspoint包下创建Java类JavaCollection和MainApp。

4 在src文件夹下创建Beans配置文件Beans.xml。

5 最后一步是创建所有Java文件和Bean配置文件的内容,并按以下说明运行应用程序。

以下是JavaCollection.java文件的内容:

package com.tutorialspoint;
import java.util.*;

public class JavaCollection {
   List addressList;
   Set  addressSet;
   Map  addressMap;
   Properties addressProp;

   // 用于设置List的setter方法
   public void setAddressList(List addressList) {
      this.addressList = addressList;
   }
   
   // 打印并返回列表的所有元素。
   public List getAddressList() {
      System.out.println("List Elements :"  + addressList);
      return addressList;
   }
   
   // 用于设置Set的setter方法
   public void setAddressSet(Set addressSet) {
      this.addressSet = addressSet;
   }
   
   // 打印并返回Set的所有元素。
   public Set getAddressSet() {
      System.out.println("Set Elements :"  + addressSet);
      return addressSet;
   }
   
   // 用于设置Map的setter方法
   public void setAddressMap(Map addressMap) {
      this.addressMap = addressMap;
   }
   
   // 打印并返回Map的所有元素。
   public Map getAddressMap() {
      System.out.println("Map Elements :"  + addressMap);
      return addressMap;
   }
   
   // 用于设置Property的setter方法
   public void setAddressProp(Properties addressProp) {
      this.addressProp = addressProp;
   }
   
   // 打印并返回Property的所有元素。
   public Properties getAddressProp() {
      System.out.println("Property Elements :"  + addressProp);
      return addressProp;
   }
}

以下是MainApp.java文件的内容:

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
      JavaCollection jc=(JavaCollection)context.getBean("javaCollection");

      jc.getAddressList();
      jc.getAddressSet();
      jc.getAddressMap();
      jc.getAddressProp();
   }
}

以下是包含所有集合类型配置的Beans.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-3.0.xsd

">

   <!-- Definition for javaCollection -->
   <bean id = "javaCollection" class = "com.tutorialspoint.JavaCollection">
      
      <!-- 产生 setAddressList(java.util.List) 调用 -->
      <property name = "addressList">
         <list>
            <value>INDIA</value>
            <value>Pakistan</value>
            <value>USA</value>
            <value>USA</value>
         </list>
      </property>

      <!-- 产生 setAddressSet(java.util.Set) 调用 -->
      <property name = "addressSet">
         <set>
            <value>INDIA</value>
            <value>Pakistan</value>
            <value>USA</value>
            <value>USA</value>
         </set>
      </property>

      <!-- 产生 setAddressMap(java.util.Map) 调用 -->
      <property name = "addressMap">
         <map>
            <entry key = "1" value = "INDIA"/>
            <entry key = "2" value = "Pakistan"/>
            <entry key = "3" value = "USA"/>
            <entry key = "4" value = "USA"/>
         </map>
      </property>
      
      <!-- 产生 setAddressProp(java.util.Properties) 调用 -->
      <property name = "addressProp">
         <props>
            <prop key = "one">INDIA</prop>
            <prop key = "one">INDIA</prop>
            <prop key = "two">Pakistan</prop>
            <prop key = "three">USA</prop>
            <prop key = "four">USA</prop>
         </props>
      </property>
   </bean>

</beans>

当您完成创建源代码和Bean配置文件后,让我们运行应用程序。如果一切正常,应用程序将打印以下消息:

List Elements :[INDIA, Pakistan, USA, USA]
Set Elements :[INDIA, Pakistan, USA]
Map Elements :{1=INDIA, 2=Pakistan, 3=USA, 4=USA}
Property Elements :{two=Pakistan, one=INDIA, three=USA, four=USA}

注入Bean引用

以下Bean定义将帮助您了解如何将Bean引用注入为集合的元素之一。您甚至可以将引用和值混合在一起,如下面的代码片段所示:

<?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-3.0.xsd">

   <!-- Bean Definition to handle references and values -->
   <bean id = "..." class = "...">

      <!-- Passing bean reference  for java.util.List -->
      <property name = "addressList">
         <list>
            <ref bean = "address1"/>
            <ref bean = "address2"/>
            <value>Pakistan</value>
         </list>
      </property>
      
      <!-- Passing bean reference  for java.util.Set -->
      <property name = "addressSet">
         <set>
            <ref bean = "address1"/>
            <ref bean = "address2"/>
            <value>Pakistan</value>
         </set>
      </property>
      
      <!-- Passing bean reference  for java.util.Map -->
      <property name = "addressMap">
         <map>
            <entry key = "one" value = "INDIA"/>
            <entry key = "two" value-ref = "address1"/>
            <entry key = "three" value-ref = "address2"/>
         </map>
      </property>
   </bean>

</beans>

要使用上述Bean定义,您需要以使它们能够处理引用的方式定义setter方法。

注入null和空字符串值

如果需要传递空字符串作为值,可以使用以下方式传递:

<bean id = "..." class = "exampleBean">
   <property name = "email" value = ""/>
</bean>

上述示例等效于Java代码:exampleBean.setEmail("")

如果需要传递NULL值,可以使用以下方式传递:

<bean id = "..." class = "exampleBean">
   <property name = "email"><null/></property>
</bean>

上述示例等效于Java代码:exampleBean.setEmail(null)

最后

为了方便其他设备和平台的小伙伴观看往期文章,链接奉上:

公众号搜索Let us Coding知乎开源中国CSDN思否掘金InfoQ简书博客园慕课51CTOhelloworld腾讯开发者社区阿里开发者社区

看完如果觉得有帮助,欢迎点赞、收藏关注

标签:Pakistan,Elements,USA,Spring,Bean,Set,集合,public,注入
From: https://www.cnblogs.com/xiaowange/p/17694721.html

相关文章

  • springcloud中网关起什么作用
    (目录)springcloud中网关起什么作用在SpringCloud中,网关起到了路由和过滤的作用。路由:网关通过配置路由规则,将请求转发到不同的服务实例上。它可以根据请求的URL、请求的HTTP方法、请求的Header等信息,将请求路由到相应的服务实例上。通过网关,可以实现请求的负载均衡和动态路......
  • Spring Boot 的核心注解是哪个?它主要由哪几个注解组成的?
    是@SpringBootApplication由三个注解组成@EnableAutoConfiguration:开启自动配置@SpringBootConfiguration:和@Configuration作用类似,表明自己是一个配置类,不同的是@SpringBootConfiguration允许自动配置@ComponetScan:设置扫描路径......
  • 如何理解Spring Boot中的Starters,以及Starter的工作原理
    如何理解SpringBoot中的StartersStarters可以理解为启动器,你可以一站式集成Spring及其他技术,而不需要到处找示例代码和依赖包。如你想使用SpringJPA访问数据库,只要加入spring-boot-starter-data-jpa启动器依赖就能使用了。Starter的工作原理:SpringBoot在启动的时候会......
  • Spring 循环依赖解决方案
    Spring解决循环依赖的思路与代码实现(qq.com)一文详解SpringBean循环依赖(qq.com)Spring循环依赖解决方案(qq.com)结论先说结论,Spring是通过三级缓存和提前曝光的机制来解决循环依赖的问题。两个BeanA,B互相引用循环依赖,Spring的解决过程如下:通过构建函数创建A对象(A对......
  • SpringMVC 工作原理了解吗?
    request发送给dispatcherservletdispa~根据handlermapping找到handlerAdapterhandlerAdapter调用handler处理,返回modelandview给dispa~dispa将modelandview发送给视图解析器解析为逻辑view并返回给dispadispa~根据逻辑的view对象找到真正的view对象并且用真正的v......
  • 简单介绍 Spring MVC 的核心组件
    DispatcherServlet和九大组件(按使用顺序排序的):组件说明DispatcherServletSpringMVC的核心组件,是请求的入口,负责协调各个组件工作MultipartResolver内容类型(Content-Type)为multipart/*的请求的解析器,例如解析处理文件上传的请求,便于获取参数信息以及上传的......
  • Drupal < 7.32版本 _“Drupalgeddon” SQL注入漏洞(CVE-2014-3704)
    目录1.1、漏洞描述1.2、漏洞等级1.3、影响版本1.4、漏洞复现1、基础环境2、漏洞扫描3、漏洞验证说明内容漏洞编号CVE-2014-3704漏洞名称Drupal“Drupalgeddon”SQL注入漏洞漏洞评级影响范围Drupal7.0~7.31版本漏洞描述修复方案1.1、漏洞......
  • Spring 事务
    Spring事务实现方式有哪些编程式优点:灵活缺点:麻烦,难以维护声明式加注解Spring的事务管理有什么优点支持声明式事务管理提供跨不同事务api的一致事务模型传播规则借用别人的的图片方便记忆支持当前事务的“女生”,这里的事务指的是“房子”,它分为3种(普通型......
  • Java集合
    集合框架单列集合:双列集合:集合和数组的区别长度:数组固定长度内容:集合只能是引用类型元素:数组只能存储同一类型Collection接口实现类有些可以重复,有些有序,没有直接实现,而是子接口//常用方法list.add(true)//可以添加不同类型 .remove(true)//可以按索引也可以直接删......
  • SSM SpringBoot vue快递柜管理系统
    SSMSpringBootvue快递柜管理系统系统功能登录注册个人中心快递员管理用户信息管理 用户寄件管理配送信息管理寄存信息管理开发环境和技术开发语言:Java使用框架:SSM(Spring+SpringMVC+Mybaits)或SpringBoot前端:vue数据库:Mysql架构:B/S源码类型......