| # 实体类 |
| public class Stu { |
| |
| //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 setSets(Set<String> sets) { |
| this.sets = 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 test() { |
| System.out.println(Arrays.toString(courses)); |
| System.out.println(list); |
| System.out.println(maps); |
| System.out.println(sets); |
| } |
| |
| } |
| |
| # 配置bean.xml |
| <bean id="stu" class="com.ychen.spring.bean.Stu"> |
| |
| <property name="courses"> |
| <array> |
| <value>java课程</value> |
| <value>数据库课程</value> |
| </array> |
| </property> |
| |
| <property name="list"> |
| <list> |
| <value>张三</value> |
| <value>小三</value> |
| </list> |
| </property> |
| |
| <property name="maps"> |
| <map> |
| <entry key="JAVA" value="java"></entry> |
| <entry key="PHP" value="php"></entry> |
| </map> |
| </property> |
| |
| <property name="sets"> |
| <set> |
| <value>MySQL</value> |
| <value>Redis</value> |
| </set> |
| </property> |
| </bean> |
| |
| # 测试方法 |
| @Test |
| public void testCollection1() { |
| ApplicationContext context = |
| new ClassPathXmlApplicationContext("bean1.xml"); |
| Stu stu = context.getBean("stu", Stu.class); |
| stu.test(); |
| } |
| |
| # 控制台 |
| [java课程, 数据库课程] |
| [张三, 小三] |
| {JAVA=java, PHP=php} |
| [MySQL, Redis] |
| |
| Process finished with exit code 0 |
| # 课程实体类 |
| public class Course { |
| |
| private String cname; //课程名称 |
| |
| public void setCname(String cname) { |
| this.cname = cname; |
| } |
| |
| @Override |
| public String toString() { |
| return "Course{" + |
| "cname='" + cname + '\'' + |
| '}'; |
| } |
| |
| } |
| |
| # 学生实体类 |
| public class Stu { |
| |
| //1 数组类型属性 |
| private String[] courses; |
| |
| //2 list集合类型属性 |
| private List<String> list; |
| |
| //3 map集合类型属性 |
| private Map<String,String> maps; |
| |
| //4 set集合类型属性 |
| private Set<String> sets; |
| |
| //学生所学多门课程 |
| private List<Course> courseList; |
| |
| public void setCourseList(List<Course> courseList) { |
| this.courseList = courseList; |
| } |
| |
| public void setSets(Set<String> sets) { |
| this.sets = 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 test() { |
| System.out.println(Arrays.toString(courses)); |
| System.out.println(list); |
| System.out.println(maps); |
| System.out.println(sets); |
| System.out.println(courseList); |
| } |
| |
| } |
| |
| # 配置bean.xml |
| <bean id="stu" class="com.ychen.spring.bean.Stu"> |
| |
| <property name="courses"> |
| <array> |
| <value>java课程</value> |
| <value>数据库课程</value> |
| </array> |
| </property> |
| |
| <property name="list"> |
| <list> |
| <value>张三</value> |
| <value>小三</value> |
| </list> |
| </property> |
| |
| <property name="maps"> |
| <map> |
| <entry key="JAVA" value="java"></entry> |
| <entry key="PHP" value="php"></entry> |
| </map> |
| </property> |
| |
| <property name="sets"> |
| <set> |
| <value>MySQL</value> |
| <value>Redis</value> |
| </set> |
| </property> |
| |
| <property name="courseList"> |
| <list> |
| <ref bean="course1"></ref> |
| <ref bean="course2"></ref> |
| </list> |
| </property> |
| </bean> |
| |
| |
| <bean id="course1" class="com.ychen.spring.bean.Course"> |
| <property name="cname" value="Spring5框架"></property> |
| </bean> |
| <bean id="course2" class="com.ychen.spring.bean.Course"> |
| <property name="cname" value="MyBatis框架"></property> |
| </bean> |
| |
| # 测试方法 |
| @Test |
| public void testCollection1() { |
| ApplicationContext context = |
| new ClassPathXmlApplicationContext("bean1.xml"); |
| Stu stu = context.getBean("stu", Stu.class); |
| stu.test(); |
| } |
| |
| # 控制台 |
| [java课程, 数据库课程] |
| [张三, 小三] |
| {JAVA=java, PHP=php} |
| [MySQL, Redis] |
| [Course{cname='Spring5框架'}, Course{cname='MyBatis框架'}] |
| |
| Process finished with exit code 0 |
| # book实体类 |
| public class Book { |
| |
| private List<String> list; |
| |
| public void setList(List<String> list) { |
| this.list = list; |
| } |
| |
| public void test() { |
| System.out.println(list); |
| } |
| |
| } |
| |
| # 配置bean.xml |
| # 添加xmlns:util="http://www.springframework.org/schema/util" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd" |
| <?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 id="bookList"> |
| <value>易筋经</value> |
| <value>九阴真经</value> |
| <value>九阳神功</value> |
| </util:list> |
| |
| |
| <bean id="book" class="com.ychen.spring.bean.Book" scope="singleton"> |
| <property name="list" ref="bookList"></property> |
| </bean> |
| |
| </beans> |
| |
| # 测试方法 |
| @Test |
| public void testCollection2() { |
| ApplicationContext context = |
| new ClassPathXmlApplicationContext("bean2.xml"); |
| Book book = context.getBean("book", Book.class); |
| book.test(); |
| } |
| |
| # 控制台 |
| [易筋经, 九阴真经, 九阳神功] |
| |
| Process finished with exit code 0 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术