Spring5入门到实战------4、IOC容器-Bean管理XML方式、集合的注入(二)

本文涉及的产品
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
简介: 这篇文章是Spring5框架的实战教程,主题是IOC容器中Bean的集合属性注入,通过XML配置方式。文章详细讲解了如何在Spring中注入数组、List、Map和Set类型的集合属性,并提供了相应的XML配置示例和Java类定义。此外,还介绍了如何在集合中注入对象类型值,以及如何使用Spring的util命名空间来实现集合的复用。最后,通过测试代码和结果展示了注入效果。

3、IOC容器-Bean管理XML方式(一):

1、IOC操作Bean管理(xml注入集合属性)

  • 1、注入数组类型属性
  • 2、注入list集合类型属性
  • 3、注入map集合类型属性
  • 4、注入set集合类型属性
类型 方式
数组 <array> <value>C语言</value> </array>
list <list> <value>张三</value> </list>
map <map> <entry key="JAVA" value="java"/></map>
set <set><value>MySQL</value> </set>

2、定义数组、list、map、set 类型属性,生成对应 set 方法

2.1 创建类

package com.zyz.spring5;

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

/**
 * @author Lenovo
 * @version 1.0
 * @data 2022/9/25 21:28
 */
public class TestStu {
    /**
     * 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;
    }

    @Override
    public String toString() {
        return "TestStu{\n" +
                "courses=" + Arrays.toString(courses) +
                ",\n list=" + list +
                ",\n maps=" + maps +
                ",\n sets=" + sets +
                '}';
    }
}

2.2、编写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.zyz.spring5.TestStu">
        <!--1、数组类型属性注入-->
        <property name="courses">
            <array>
                <value>C语言</value>
                <value>Go语言</value>
                <value>Java语言</value>
            </array>
        </property>

        <!--2、list类型属性注入-->
        <property name="list">
            <list>
                <value>张三</value>
                <value>李四</value>
                <value>麻子</value>
            </list>
        </property>

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

        <!--4、set类型属性注入-->
        <property name="sets">
            <set>
                <value>MySQL</value>
                <value>SQLServer</value>
            </set>
        </property>

    </bean>

</beans>

2.3、测试

package com.zyz.spring5.testdemo;

import com.zyz.spring5.TestStu;
import com.zyz.spring5.dao.Employee;
import com.zyz.spring5.entity.Person;
import com.zyz.spring5.service.CatService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author Lenovo
 * @version 1.0
 * @data 2022/9/22 22:25
 */
public class SpringTest5 {

    @Test
    public void testSpring(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
        TestStu testStu = context.getBean("stu",TestStu.class);
        System.out.println(testStu);

    }

}

2.4、测试结果

在这里插入图片描述

3、在集合里面设置对象类型值

主要是ref

3.1 创建一个用户类

package com.zyz.spring5.entity;

import lombok.Data;

/**
 * @author Lenovo
 * @version 1.0
 * @data 2022/9/22 22:23
 */
public class Person {
    private String address;
    private String name;

    public void setAddress(String address){
        this.address = address;
    }

    public void setName(String name){
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "address='" + address + '\'' +
                ", pname='" + name + '\'' +
                '}';
    }
}

3.2 用户对象作为集合中的值

    /**
     *5、集合中设置对象属性值
     */
    private List<Person> personList;

    public void setPersonList(List<Person> personList){
        this.personList = personList;
    }

3.3 xml配置文件

        <!--5、集合中设置对象属性值-->
        <property name="personList">
            <list >
                <ref bean="person1"/>
                <ref bean="person2"/>
            </list>
        </property>


    </bean>

    <bean id="person1" class="com.zyz.spring5.entity.Person">
        <property name="address" value="河南周口"/>
        <property name="name" value="张三"/>
    </bean>

    <bean id="person2" class="com.zyz.spring5.entity.Person">
        <property name="address" value="河南郑州"/>
        <property name="name" value="李四"/>
    </bean>

在这里插入图片描述

4、集合注入部分提取出来

把集合抽取成为一个公共部分、不同的bean使用。可以达到代码复用

4.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: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">

    <util:list id="bookList">
        <value>高等数学</value>
        <value>大学英语</value>
        <value>大学政治</value>
    </util:list>

    <bean id="testStu" class="com.zyz.spring5.TestStu">
        <property name="list" ref="bookList"></property>
    </bean>

</beans>

4.2 使用 util 标签完成 list 集合注入提取

将对象提取、一般的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: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">

    <util:list id="bookList">
        <value>高等数学</value>
        <value>大学英语</value>
        <value>大学政治</value>
    </util:list>

    <util:list id="personList">
        <ref bean="person1"/>
        <ref bean="person2"/>
    </util:list>

    <bean id="person1" class="com.zyz.spring5.entity.Person">
        <property name="address" value="河南周口"/>
        <property name="name" value="张三"/>
    </bean>

    <bean id="person2" class="com.zyz.spring5.entity.Person">
        <property name="address" value="河南郑州"/>
        <property name="name" value="李四"/>
    </bean>

    <bean id="testStu" class="com.zyz.spring5.TestStu">
        <property name="list" ref="bookList"></property>
        <property name="personList" ref="personList"/>
     </bean>

</beans>

4.3 测试结果

在这里插入图片描述

在这里插入图片描述

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
4月前
|
负载均衡 监控 Java
Spring Cloud Gateway 全解析:路由配置、断言规则与过滤器实战指南
本文详细介绍了 Spring Cloud Gateway 的核心功能与实践配置。首先讲解了网关模块的创建流程,包括依赖引入(gateway、nacos 服务发现、负载均衡)、端口与服务发现配置,以及路由规则的设置(需注意路径前缀重复与优先级 order)。接着深入解析路由断言,涵盖 After、Before、Path 等 12 种内置断言的参数、作用及配置示例,并说明了自定义断言的实现方法。随后重点阐述过滤器机制,区分路由过滤器(如 AddRequestHeader、RewritePath、RequestRateLimiter 等)与全局过滤器的作用范围与配置方式,提
Spring Cloud Gateway 全解析:路由配置、断言规则与过滤器实战指南
|
5月前
|
监控 Java API
Spring Boot 3.2 结合 Spring Cloud 微服务架构实操指南 现代分布式应用系统构建实战教程
Spring Boot 3.2 + Spring Cloud 2023.0 微服务架构实践摘要 本文基于Spring Boot 3.2.5和Spring Cloud 2023.0.1最新稳定版本,演示现代微服务架构的构建过程。主要内容包括: 技术栈选择:采用Spring Cloud Netflix Eureka 4.1.0作为服务注册中心,Resilience4j 2.1.0替代Hystrix实现熔断机制,配合OpenFeign和Gateway等组件。 核心实操步骤: 搭建Eureka注册中心服务 构建商品
896 3
|
3月前
|
监控 Cloud Native Java
Spring Boot 3.x 微服务架构实战指南
🌟蒋星熠Jaxonic,技术宇宙中的星际旅人。深耕Spring Boot 3.x与微服务架构,探索云原生、性能优化与高可用系统设计。以代码为笔,在二进制星河中谱写极客诗篇。关注我,共赴技术星辰大海!(238字)
Spring Boot 3.x 微服务架构实战指南
|
3月前
|
XML Java 测试技术
《深入理解Spring》:IoC容器核心原理与实战
Spring IoC通过控制反转与依赖注入实现对象间的解耦,由容器统一管理Bean的生命周期与依赖关系。支持XML、注解和Java配置三种方式,结合作用域、条件化配置与循环依赖处理等机制,提升应用的可维护性与可测试性,是现代Java开发的核心基石。
|
3月前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
470 2
|
5月前
|
人工智能 监控 安全
如何快速上手【Spring AOP】?核心应用实战(上篇)
哈喽大家好吖~欢迎来到Spring AOP系列教程的上篇 - 应用篇。在本篇,我们将专注于Spring AOP的实际应用,通过具体的代码示例和场景分析,帮助大家掌握AOP的使用方法和技巧。而在后续的下篇中,我们将深入探讨Spring AOP的实现原理和底层机制。 AOP(Aspect-Oriented Programming,面向切面编程)是Spring框架中的核心特性之一,它能够帮助我们解决横切关注点(如日志记录、性能统计、安全控制、事务管理等)的问题,提高代码的模块化程度和复用性。
|
XML Java 数据格式
京东一面:spring ioc容器本质是什么? ioc容器启动的步骤有哪些?
京东一面:spring ioc容器本质是什么? ioc容器启动的步骤有哪些?
|
5月前
|
Kubernetes Docker Python
Docker 与 Kubernetes 容器化部署核心技术及企业级应用实践全方案解析
本文详解Docker与Kubernetes容器化技术,涵盖概念原理、环境搭建、镜像构建、应用部署及监控扩展,助你掌握企业级容器化方案,提升应用开发与运维效率。
883 108