❤️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命名空间注入 : 需要在头文件中加入约束文件

<!--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 命名空间注入 : 需要在头文件中加入约束文件

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

测试类: