❤️Spring注入集合❤️(建议收藏)

❤️Spring注入集合

.以下是一些Spring常用的注入集合,请“食用”,记得给个三连噢!

如果需要传递类似于 Java Collection 类型的值,例如 List、Set、Map 和 properties,可以使用 Spring 提供的集合配置标签,如下表所示。

标签说明
list用于注入 list 类型的值,允许重复
set用于注入 set 类型的值,不允许重复
map用于注入 key-value 的集合,其中 key-value 可以是任意类型
props用于注入 key-value 的集合,其中 key-value 都是字符串类型
String name;
Address address;
String[] books;
List<String> hobbys;
Map<String,String> card;
Set<String> games;
String wife;
Properties info;

❤️示例

下面使用 IDEA 演示如何注入集合,步骤如下:

  1. 创建 SpringDemo 项目,并在 src 目录下创建包。
  2. 添加相应的 jar 包,可以参考《第一个Spring程序》一节。
  3. 在所包下创建 Address、Student 类。
  4. 在 src 目录下创建 Spring 配置文件 beans.xml。
  5. 运行 SpringDemo 项目。

❤️源码

❤️1、实体类

address类

package com.kk.pojo;

public class Address {
    private String address;
    public String getAddress() {
        return address;
    }

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

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}

Student类

package com.kk.pojo;


import java.util.*;
public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

    public Student() {

    }

    public Student(String name, Address address, String[] books,
                   List<String> hobbys, Map<String, String> card,
                   Set<String> games, String wife, Properties info) {
        this.name = name;
        this.address = address;
        this.books = books;
        this.hobbys = hobbys;
        this.card = card;
        this.games = games;
        this.wife = wife;
        this.info = info;
    }

    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

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

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

❤️ 2、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">
    <bean id="address" class="com.kk.pojo.Address">
        <property name="address" value="广东"></property>
    </bean>


    <bean id="student" class="com.kk.pojo.Student">
        <!--    第一种:普通值注入  使用value值注入-->
        <property name="name" value="火神"></property>
<!--        第二种:Bean注入 ,使用ref注入-->
        <property name="address" ref="address"></property>
<!--        数组注入,-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>三国演义</value>
                <value>水浒传</value>
            </array>
        </property>

<!--        list注入-->
        <property name="hobbys">
            <list>
                <value>编程</value>
                <value>听歌</value>
                <value></value>
            </list>
        </property>

<!--        map注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="4412002121223***"></entry>
                <entry key="银行卡" value="654645454546"></entry>
            </map>
        </property>

<!--        set注入-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>CSOL</value>
            </set>
        </property>

<!--        wife注入-->
        <property name="wife">
            <null></null>
        </property>

<!--        Properties-->
        <property name="info">
            <props>
                <prop key="学号">2011***</prop>
                <prop key="性别"></prop>
                <prop key="姓名">k</prop>
                <prop key="password">123456</prop>
            </props>
        </property>
     </bean>

</beans>

❤️3、测试类

import com.kk.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student =(Student) context.getBean("student");
        System.out.println(student.toString());
    }
}

❤️其他方式注入

实体类:

package com.kk.pojo;

public class User {
    private int age;
    private String name;

    //c命名空间需要有参构造器


    public User(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public User() {

    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

​ 1、P命名空间注入 : 需要在头文件中加入约束文件

 导入约束 : xmlns:p="http://www.springframework.org/schema/p"

 <!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
 <bean id="user" class="com.kuang.pojo.User" p:name="kk" p:age="17"/>

测试类:

    @Test
    public void test2(){
       ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
//        User user =(User) context.getBean("user");
        User user = context.getBean("user", User.class);
        System.out.println(user);

    }

2、c 命名空间注入 : 需要在头文件中加入约束文件

 导入约束 : xmlns:c="http://www.springframework.org/schema/c"
 <!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
 <bean id="user" class="com.kuang.pojo.User" c:name="kk" c:age="17"/>

测试类:

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不易撞的网名

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

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

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

打赏作者

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

抵扣说明:

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

余额充值