Spring认证-Spring注入集合

简介: 您已经了解了如何使用值属性配置原始数据类型,并使用 Bean 配置文件中的<属性>标记的参考属性配置对象引用。这两种情况都涉及将值传递给 bean。

您已经了解了如何使用值属性配置原始数据类型,并使用Bean 配置文件中的 标记的ref属性配置对象引用。这两种情况都涉及将奇异值传递给 bean。

现在,如果您想传递多个值,例如 Java 集合类型,例如 List、Set、Map 和 Properties,该怎么办。为了处理这种情况,Spring 提供了四种类型的集合配置元素,如下所示 -

没有 元素和描述
1
<列表>

这有助于接线,即注入值列表,允许重复。

2
<设置>

这有助于连接一组值但没有任何重复。

3
<地图>

这可用于注入名称-值对的集合,其中名称和值可以是任何类型。

4
<道具>

这可用于注入名称和值都是字符串的名称-值对集合。

您可以使用 或 来连接 java.util.Collection 或数组的任何实现。

您将遇到两种情况 (a) 传递集合的直接值和 (b) 传递 bean 的引用作为集合元素之一。

例子
让我们有一个工作的 Eclipse IDE 并采取以下步骤来创建一个 Spring 应用程序 -

脚步 描述
1 创建一个名为SpringExample的项目,并在创建的项目的src文件夹下创建一个包com.tutorialspoint。
2 使用添加外部 JAR选项添加所需的 Spring 库,如Spring Hello World 示例章节中所述。
3 创建Java类JavaCollection和MainApp下com.tutorialspoint包。
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;

// a setter method to set List
public void setAddressList(List addressList) {

  this.addressList = addressList;
AI 代码解读

}

// prints and returns all the elements of the list.
public List getAddressList() {

  System.out.println("List Elements :"  + addressList);
  return addressList;
AI 代码解读

}

// a setter method to set Set
public void setAddressSet(Set addressSet) {

  this.addressSet = addressSet;
AI 代码解读

}

// prints and returns all the elements of the Set.
public Set getAddressSet() {

  System.out.println("Set Elements :"  + addressSet);
  return addressSet;
AI 代码解读

}

// a setter method to set Map
public void setAddressMap(Map addressMap) {

  this.addressMap = addressMap;
AI 代码解读

}

// prints and returns all the elements of the Map.
public Map getAddressMap() {

  System.out.println("Map Elements :"  + addressMap);
  return addressMap;
AI 代码解读

}

// a setter method to set Property
public void setAddressProp(Properties addressProp) {

  this.addressProp = addressProp;
AI 代码解读

}

// prints and returns all the elements of the Property.
public Properties getAddressProp() {

  System.out.println("Property Elements :"  + addressProp);
  return addressProp;
AI 代码解读

}
}
以下是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();
AI 代码解读

}
}
以下是配置文件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">

  
  <!-- results in a setAddressList(java.util.List) call -->
  <property name = "addressList">
     <list>
        <value>INDIA</value>
        <value>Pakistan</value>
        <value>USA</value>
        <value>USA</value>
     </list>
  </property>

  <!-- results in a setAddressSet(java.util.Set) call -->
  <property name = "addressSet">
     <set>
        <value>INDIA</value>
        <value>Pakistan</value>
        <value>USA</value>
        <value>USA</value>
     </set>
  </property>

  <!-- results in a setAddressMap(java.util.Map) call -->
  <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>
  
  <!-- results in a setAddressProp(java.util.Properties) call -->
  <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>
AI 代码解读


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

List Elements :[INDIA, Pakistan, USA, USA]
Set Elements :[INDIA, Pakistan, USA]
ap 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">

  <!-- 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>
AI 代码解读


要使用上面的 bean 定义,您需要以这样一种方式定义 setter 方法,以便它们也应该能够处理引用。

注入 null 和空字符串值
如果您需要传递一个空字符串作为值,那么您可以按如下方式传递它 -




前面的示例等效于 Java 代码:exampleBean.setEmail("")

如果您需要传递一个 NULL 值,那么您可以按如下方式传递它 -


<null/>

前面的示例等效于 Java 代码:exampleBean.setEmail(null)

目录
打赏
0
0
0
0
58
分享
相关文章
安全无忧!在 Spring Boot 3.3 中轻松实现 TOTP 双因素认证
【10月更文挑战第8天】在现代应用程序开发中,安全性是一个不可忽视的重要环节。随着技术的发展,双因素认证(2FA)已经成为增强应用安全性的重要手段之一。本文将详细介绍如何在 Spring Boot 3.3 中实现基于时间的一次性密码(TOTP)双因素认证,让你的应用安全无忧。
343 5
|
5天前
|
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 身份和权限认证
本文介绍了 Apache Shiro 的身份认证与权限认证机制。在身份认证部分,分析了 Shiro 的认证流程,包括应用程序调用 `Subject.login(token)` 方法、SecurityManager 接管认证以及通过 Realm 进行具体的安全验证。权限认证部分阐述了权限(permission)、角色(role)和用户(user)三者的关系,其中用户可拥有多个角色,角色则对应不同的权限组合,例如普通用户仅能查看或添加信息,而管理员可执行所有操作。
36 0
在Spring boot中 使用JWT和过滤器实现登录认证
在Spring boot中 使用JWT和过滤器实现登录认证
374 0
在使用Spring的`@Value`注解注入属性值时,有一些特殊字符需要注意
【10月更文挑战第9天】在使用Spring的`@Value`注解注入属性值时,需注意一些特殊字符的正确处理方法,包括空格、引号、反斜杠、新行、制表符、逗号、大括号、$、百分号及其他特殊字符。通过适当包裹或转义,确保这些字符能被正确解析和注入。
362 3
Spring Boot 实现通用 Auth 认证的 4 种方式
本文介绍了在Spring Boot中实现通用Auth的四种方式:传统AOP、拦截器(Interceptor)、参数解析器(ArgumentResolver)和过滤器(Filter)。每种方式都通过实例代码详细说明了实现步骤,并总结了它们的执行顺序。首先,Filter作为Servlet规范的一部分最先被调用;接着是Interceptor,它可以在Controller方法执行前后进行处理;然后是ArgumentResolver,在参数传递给Controller之前解析并验证参数
为什么Spring不推荐@Autowired用于字段注入?
作为Java程序员,Spring框架在日常开发中使用频繁,其依赖注入机制带来了极大的便利。然而,尽管@Autowired注解简化了依赖注入,Spring官方却不推荐在字段上使用它。本文将探讨字段注入的现状及其存在的问题,如难以进行单元测试、违反单一职责原则及易引发NPE等,并介绍为何Spring推荐构造器注入,包括增强代码可读性和维护性、方便单元测试以及避免NPE等问题。通过示例代码展示如何将字段注入重构为构造器注入,提高代码质量。
152 1
一键注入 Spring 成员变量,顺序编程
介绍了一款针对Spring框架开发的插件,旨在解决开发中频繁滚动查找成员变量注入位置的问题。通过一键操作(如Ctrl+1),该插件可自动在类顶部添加`@Autowired`注解及其成员变量声明,同时保持光标位置不变,有效提升开发效率和代码编写流畅度。适用于IntelliJ IDEA 2023及以上版本。
一键注入 Spring 成员变量,顺序编程
基于内存认证的 Spring Security
通过本文的介绍,希望您能够深入理解基于内存认证的Spring Security配置与使用方法,并能够在实际开发中灵活应用这一技术,提升应用的安全性和用户体验。
84 9
|
4月前
|
Spring Boot 应用如何实现 JWT 认证?
Spring Boot 应用如何实现 JWT 认证?
133 8
【Spring】“请求“ 之后端传参重命名,传递数组、集合,@PathVariable,@RequestPart
【Spring】“请求“ 之后端传参重命名,传递数组、集合,@PathVariable,@RequestPart
71 2

热门文章

最新文章