spring数组与集合注入简单步骤(XML形式)

目录

一、介绍

二、数组与集合注入(XML形式)

总结


一、介绍

1.其他文章的介绍,如有不懂可以结合起来看

快速入门使用spring详细步骤(介绍、导入依赖、第一个简单程序)_云边的快乐猫的博客-CSDN博客

spring的注入(set注入、构造器注入)_云边的快乐猫的博客-CSDN博客

2.使用spring的时候,遇到要数组注入或者集合注入,那该怎么做呢?请看如下的步骤

二、数组与集合注入(XML形式)

1.建立一个类,里面存放数组与集合,还要获取注入返回的方法

package com.spring6.bean;

import java.util.*;

public class Book {
    //数组
    private int[] array;
    //list集合
    private List<String> list;
    //set集合
    private Set<String> set;
    //map集合
    private Map<String,String> map;
    //属性集合
    private Properties properties;
//生成的set方法
    public void setArray(int[] array) {
        this.array = array;
    }

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

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    //创建一个方法去调用spring注入的结果
    public void listBean(){
        System.out.println("这是数组的"+Arrays.toString(array));
        System.out.println("这是list的"+list);
        System.out.println("这是Set的"+set);
        System.out.println("这是Map的"+map);
        System.out.println("这是properties的"+properties);
    }
}

 2.新建一个spring的xm文件,在bean标签里面使用对应的方式添加注入数据进去数组或者集合里面

ps: 点击resources,然后快捷键Alt+insert--->XML配置文件--->spring配置

<?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="BookBean" class="com.spring6.bean.Book">

        <!--  array数组 -->
        <property name="array">
        <array>
            <value>1</value>
            <value>11</value>
        </array>
        </property>

        <!-- list集合注入 -->
        <property name="list">
            <list>
                <value>我是list集合</value>
                <value>22</value>
            </list>
        </property>

        <!--set集合注入 -->
        <property name="set">
            <set>
                <value>我是list集合</value>
                <value>22</value>
            </set>
        </property>

        <!--map集合注入 -->
        <property name="map">
            <map>
                <entry key="hobby" value="eat"/>
                <entry key="hobby2" value="sleep"/>
            </map>
        </property>

        <!-- Properties集合注入 -->
        <property name="properties">
            <props>
                <prop key="hobby"> 敲代码</prop>
                <prop key="hobby2"> 还是敲代码</prop>
            </props>
        </property>

    </bean>
</beans>

3.建立一个测试类去运行本次注入的输出结果

package com.spring6.text;

import com.spring6.bean.Book;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class springTText {
    @Test
    public void TT(){
        //1.半固定写法,扫描spring的xml文件(不固定的是要扫描的是哪个xml文件)
        ApplicationContext applicationContext =new ClassPathXmlApplicationContext("spring.xml");
        //2.半固定写法,获取spring里面要进行运行的类(不固定的是括号里面的,name为:要运行类的id,后面跟着的是要运行的类.class)
        Book bookBean = applicationContext.getBean("BookBean", Book.class);
        //3.上面自定义名.要运行类的方法
        bookBean.listBean();
    }
}

 4.运行结果

总结

这个数组和集合的注入也很简单,只需要在xml文件里面写入对应的注入标签就好了,建立类和测试类还是和原来一样 

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云边的快乐猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值