企业如何通过vue小程序开发满足高效运营与合规性需求
1310
2023-02-18
详解Spring注入集合(数组、List、Map、Set)类型属性
注入集合(数组、List、Map、Set)类型属性
(1)创建类,定义数组,list,map,set类型属性,并且生成对应的set方法。
(2)在spring配置文件中进行配置。
Stu类:
package com.Keafmd.spring5.collectiontype;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Keafmd
*
* @ClassName: Stu
* @Description: IOC操作Bean管理(xml注入属性集合)
* @author: 牛哄哄的柯南
* @date: 2021-01-15 14:15
*/
public class Stu {
//1、数组类型属性
private String[] courses;
//2、list集合类型属性
private List
//3、map集合类型属性
private Map
//4、set集合类型属性
private Set
//学生所学的多门课程
private List
public void setCourseList(List
this.courseList = courseList;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
public void setList(List this.list = list; } public void setMaps(Map this.maps = maps; } public void setSets(Set this.sets = sets; } public void test(){ System.out.println(Arrays.toString(courses)); System.out.println(list); System.out.println(maps); System.out.println(sets); System.out.println(courseList); } } bean1.xml: xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd"> 测试类: package com.Keafmd.spring5.testdemo; import com.Keafmd.spring5.bean.Orders; import com.Keafmd.spring5.collectiontype.Book; import com.Keafmd.spring5.collectiontype.Course; import com.Keafmd.spring5.collectiontype.Stu; import com.Keafmd.spring5.factorybean.MyBean; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Keafmd * * @ClassName: TestSpring5demo1 * @Description: 测试类 * @author: 牛哄哄的柯南 * @date: 2021-01-15 14:30 */ public class TestSpring5demo1 { @Test public void testCollection1(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); Stu stu = context.getBean("stu",Stu.class); stu.test(); } } 输出结果: [Java, C++, Python] [小明, 小红] {Java=java, C++=c++} [北京, 上海] [Course{cname='Spring5框架'}, Course{cname='MyBatis框架'}] Process finished with exit code 0 把集合注入部分提取出来 (1)在spring配置文件中引入名称空间util( 在配置信息中添加xmlns:util="http://springframework.org/schema/util"和http://springframework.org/schema/util http://springframework.org/schema/util/spring-util.xsd")。 (2)提取list集合类型属性注入。 (3)把提取的list集合类型属性注入使用。 Book类: package com.Keafmd.spring5.collectiontype; import java.util.List; /** * Keafmd * * @ClassName: Book * @Description: * @author: 牛哄哄的柯南 * @date: 2021-01-15 14:56 */ public class Book { private List public void setList(List this.list = list; } public void test(){ System.out.println(list); } } bean2.xml xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:util="http://springframework.org/schema/util" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd hthttp://tp://springframework.org/schema/util http://springframework.org/schema/util/spring-util.xsd"> 测试代码: package com.Keafmd.spring5.testdemo; import com.Keafmd.spring5.bean.Orders; import com.Keafmd.spring5.collectiontype.Book; import com.Keafmd.spring5.collectiontype.Course; import com.Keafmd.spring5.collectiontype.Stu; import com.Keafmd.spring5.factorybean.MyBean; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Keafmd * * @ClassName: TestSpring5demo1 * @Description: 测试类 * @author: 牛哄哄的柯南 * @date: 2021-01-15 14:30 */ public class TestSpring5demo1 { @Test public void testCollection2(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml"); Book book = context.getBean("book",Book.class); book.test(); } } 输出结果: VPctUuUEJC[老人与海, 平凡的世界, 阿甘正传] Process finished with exit code 0
this.list = list;
}
public void setMaps(Map
this.maps = maps;
}
public void setSets(Set
this.sets = sets;
}
public void test(){
System.out.println(Arrays.toString(courses));
System.out.println(list);
System.out.println(maps);
System.out.println(sets);
System.out.println(courseList);
}
}
bean1.xml:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">
测试类:
package com.Keafmd.spring5.testdemo;
import com.Keafmd.spring5.bean.Orders;
import com.Keafmd.spring5.collectiontype.Book;
import com.Keafmd.spring5.collectiontype.Course;
import com.Keafmd.spring5.collectiontype.Stu;
import com.Keafmd.spring5.factorybean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Keafmd
*
* @ClassName: TestSpring5demo1
* @Description: 测试类
* @author: 牛哄哄的柯南
* @date: 2021-01-15 14:30
*/
public class TestSpring5demo1 {
@Test
public void testCollection1(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
Stu stu = context.getBean("stu",Stu.class);
stu.test();
}
}
输出结果:
[Java, C++, Python]
[小明, 小红]
{Java=java, C++=c++}
[北京, 上海]
[Course{cname='Spring5框架'}, Course{cname='MyBatis框架'}]
Process finished with exit code 0
把集合注入部分提取出来
(1)在spring配置文件中引入名称空间util(
在配置信息中添加xmlns:util="http://springframework.org/schema/util"和http://springframework.org/schema/util http://springframework.org/schema/util/spring-util.xsd")。
(2)提取list集合类型属性注入。
(3)把提取的list集合类型属性注入使用。
Book类:
package com.Keafmd.spring5.collectiontype;
import java.util.List;
/**
* Keafmd
*
* @ClassName: Book
* @Description:
* @author: 牛哄哄的柯南
* @date: 2021-01-15 14:56
*/
public class Book {
private List
public void setList(List
this.list = list;
}
public void test(){
System.out.println(list);
}
}
bean2.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:util="http://springframework.org/schema/util" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd hthttp://tp://springframework.org/schema/util http://springframework.org/schema/util/spring-util.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:util="http://springframework.org/schema/util"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd
hthttp://tp://springframework.org/schema/util http://springframework.org/schema/util/spring-util.xsd">
测试代码:
package com.Keafmd.spring5.testdemo;
import com.Keafmd.spring5.bean.Orders;
import com.Keafmd.spring5.collectiontype.Book;
import com.Keafmd.spring5.collectiontype.Course;
import com.Keafmd.spring5.collectiontype.Stu;
import com.Keafmd.spring5.factorybean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Keafmd
*
* @ClassName: TestSpring5demo1
* @Description: 测试类
* @author: 牛哄哄的柯南
* @date: 2021-01-15 14:30
*/
public class TestSpring5demo1 {
@Test
public void testCollection2(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
Book book = context.getBean("book",Book.class);
book.test();
}
}
输出结果:
VPctUuUEJC[老人与海, 平凡的世界, 阿甘正传]
Process finished with exit code 0
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~