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 +
'}';
}
}
<hr>
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 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>
<hr>
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);
}
}
<hr>
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 class="com.zyz.spring5.entity.Person">
<property name="address" value="河南周口"/>
<property name="name" value="张三"/>
</bean>
<bean 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 >
<value>高等数学</value>
<value>大学英语</value>
<value>大学政治</value>
</util:list>
<bean class="com.zyz.spring5.TestStu">
<property name="list" ref="bookList"></property>
</bean>
</beans>
<hr>
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 >
<value>高等数学</value>
<value>大学英语</value>
<value>大学政治</value>
</util:list>
<util:list >
<ref bean="person1"/>
<ref bean="person2"/>
</util:list>
<bean class="com.zyz.spring5.entity.Person">
<property name="address" value="河南周口"/>
<property name="name" value="张三"/>
</bean>
<bean class="com.zyz.spring5.entity.Person">
<property name="address" value="河南郑州"/>
<property name="name" value="李四"/>
</bean>
<bean class="com.zyz.spring5.TestStu">
<property name="list" ref="bookList"></property>
<property name="personList" ref="personList"/>
</bean>
</beans>
<hr>