赞
踩
目录
使用index标签属性:index的值为参数的顺序,默认从0开始
5、注入集合属性——数组、List集合、Map集合、Set集合
- <!-- 配置User对象创建-->
- <!-- id是取得别名,通过id别名可以把类找到-->
- <bean id="user" class="com.atguigu.spring5.User"></bean>
DI:依赖注入,就是注入属性
- package com.atguigu.spring5.Book;
-
- public class Book {
- private String bname;
- private String bauthor;
- public void setBname(String bname){
- this.bname=bname;
- }
-
- public void setBauthor(String bauthor){
- this.bauthor=bauthor;
- }
-
- public static void main(String[] args) {
- Book book = new Book();
- book.setBname("abc");
- }
-
- public void testDemo(){
- System.out.println(bname+"---"+bauthor);
- }
- }
- <?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">
-
- <!-- 2.set方法注入属性-->
- <bean id="book" class="com.atguigu.spring5.Book.Book">
- <!-- 使用propterty完成属性注入-->
- <!-- name:类里面属性名称-->
- <!-- value:向属性注入的值-->
- <property name="bname" value="追风筝的人"></property>
- <property name="bauthor" value="卡勒德·胡赛尼"></property>
- </bean>
- </beans>
- package com.atguigu.spring5.Book;
-
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class BoolTestMain {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("Booktest.xml");
- Book book = context.getBean("book", Book.class);
- System.out.println(book);
- book.testDemo();
- }
- }
我们还可以通过另一种方式简化写法—p名称空间注入
- <?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:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
-
- <bean id="books" class="com.atguigu.spring5.Book.Book" p:bname="朝花夕拾" p:bauthor="鲁迅"></bean>
-
- </beans>
- public class Orders {
- // 有参数构造注入属性
- private String oname;
- private String address;
- private double money;
-
- public Orders(String oname, String address, double money) {
- this.oname = oname;
- this.address = address;
- this.money = money;
- }
-
- public void show() {
- System.out.println(oname + "---" + address + "---" + money);
- }
- }
使用value标签属性: 默认按照constructor-arg标签的顺序
- <constructor-arg value="电脑"></constructor-arg>
- <constructor-arg value="China"></constructor-arg>
- <constructor-arg name="oname" value="电脑"></constructor-arg>
- <constructor-arg name="address" value="China"></constructor-arg>
- <!--index="0":有参构造的第一个属性-->
- <constructor-arg index="0" value="电脑"></constructor-arg>
- <constructor-arg index="1" value="China"></constructor-arg>
(如果数据类型一致,按照constructor-arg标签的顺序匹配)
- <constructor-arg type="double" value="23"></constructor-arg>
- <constructor-arg type="java.lang.String" value="电脑"></constructor-arg>
- <constructor-arg type="java.lang.String" value="China"></constructor-arg>
- <constructor-arg name="oname" value="朝花夕拾"/>
- <constructor-arg name="oauthor" value="鲁迅"/>
- <constructor-arg name="address" >
- <null></null>
- </constructor-arg>
- <!-- 1、使用<>先进行转义-->
- <property name="bauthor" value="鲁迅"></property>
- <property name="bname" value="<>朝花夕拾>>"></property>
-
- <!-- 2、使用!CDATA-->
- <property name="bauthor">
- <value><![CDATA[<<南京>>]]></value>
- </property>
通过ref属性将进行注入
- public class School {
- //创建School类型属性,生成set方法
- private Teacher teacher;
-
- public void setTeacher(Teacher teacher) {
- this.teacher = teacher;
- }
- }
- <!-- 注入外部bean-->
- <bean id="school" class="com.atguigu.spring5.propertyTest.School">
- <property name="teacher" ref="teachers"></property>
- </bean>
-
- <bean id="teachers" class="com.atguigu.spring5.propertyTest.Teacher"></bean>
在某一个bean中定义另一个bean作为属性
如果School类中的某个属性的数据类型是另一个类Book,则可以把这个类Book定义在属性的内部
- public class School {
- //创建School类型属性,生成set方法
- private Teacher teacher;
-
- public void setTeacher(Teacher teacher) {
- this.teacher = teacher;
- }
-
- //部门
- private Book dept;
- public void setDept(Book dept) {
- this.dept = dept;
- }
- }
- public class Book {
- private String bname;
- private String bauthor;
-
- public void setBname(String bname) {
- this.bname = bname;
- }
- }
- <!-- 注入内部bean-->
- <bean id="school" class="com.atguigu.spring5.propertyTest.School">
- <property name="teacher" ref="teachers"></property>
- <property name="dept" >
- <bean id="book" class="com.atguigu.spring5.Book.Book">
- <property name="bname" value="朝花夕拾"></property>
- </bean>
- </property>
- </bean>
- package com.atguigu.spring5.CollectionTypeTest;
-
- import java.util.Arrays;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
-
- /**
- * @BelongsProject: 02-Spring
- * @BelongsPackage: com.atguigu.spring5.CollectionTypeTest
- * @Author: dengLiMei
- * @CreateTime: 2023-02-09 09:46
- * @Description: TODO
- * @Version: 1.0
- */
- public class Student {
- //数组
- private String[] courses;
-
- // List集合
- private List<String> lists;
-
- //Map集合
- private Map<String, String> maps;
-
- //Set集合
- private Set<String> sets;
-
- public void setSets(Set<String> sets) {
- this.sets = sets;
- }
-
- public Student() {
- }
-
- public void setCourses(String[] courses) {
- this.courses = courses;
- }
-
- public void setLists(List<String> lists) {
- this.lists = lists;
- }
-
- public void setMaps(Map<String, String> maps) {
- this.maps = maps;
- }
-
- public void show() {
- System.out.println(Arrays.toString(courses));
- System.out.println((lists));
- System.out.println((maps));
- System.out.println((sets));
- }
- }
- <?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 id="students" class="com.atguigu.spring5.CollectionTypeTest.Student">
- <!-- 数组-->
- <property name="courses">
- <array>
- <value>spring5</value>
- <value>springboot</value>
- </array>
- </property>
-
- <!-- List类型-->
- <property name="lists">
- <list>
- <value>springcloud</value>
- <value>springcloudAlibaba</value>
- </list>
- </property>
-
- <!-- Map集合-->
- <property name="maps">
- <map>
- <entry key="QQ" value="腾讯"></entry>
- <entry key="淘宝" value="阿里"></entry>
- </map>
- </property>
-
- <!-- Set集合-->
- <property name="sets">
- <set>
- <value>set-spring5</value>
- <value>set-springboot</value>
- </set>
- </property>
- </bean>
- </beans>
输出结果:
如果一个类中包含了另一个类的实体对象,可以这样写
- public class Course {
- // 课程名称
- private String cname;
-
- public void setCname(String cname) {
- this.cname = cname;
- }
- }
- package com.atguigu.spring5.CollectionTypeTest;
-
- import java.util.Arrays;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
-
- public class Student2 {
- //实体对象-学生有多门课程
- private List<Course> courseList;
-
- public void setCourseList(List<Course> courseList) {
- this.courseList = courseList;
- }
-
- @Override
- public String toString() {
- return "Student2{" +
- "courseList=" + courseList +
- '}';
- }
- }
- <bean id="student2" class="com.atguigu.spring5.CollectionTypeTest.Student2">
- <property name="courseList">
- <list>
- <ref bean="course1"></ref>
- <ref bean="course2"></ref>
- </list>
- </property>
- </bean>
-
- <!-- 创建多个course对象-->
- <bean id="course1" class="com.atguigu.spring5.CollectionTypeTest.Course">
- <property name="cname" value="spring5框架"></property>
- </bean>
- <bean id="course2" class="com.atguigu.spring5.CollectionTypeTest.Course">
- <property name="cname" value="MyBatis框架"></property>
- </bean>
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("CollectionTypeBeans.xml");
- Student2 students1 = context.getBean("student2", Student2.class);
- students1.show();
- }
输出结果:
目前的问题:只能当前类使用,其他类不能用,接下来就是把集合属性抽取成公共部分让所有实体类都能够引入
- public class Book {
- private List<String> list;
-
- public void setList(List<String> list) {
- this.list = list;
- }
-
- public void show(){
- System.out.println(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">
-
- <!-- 1、提取list集合类型属性注入-->
- <util:list id="bookList">
- <value>spring5</value>
- <value>springboot</value>
- <value>springcloud</value>
- </util:list>
-
- <!-- 2、提取list集合类型属性注入使用-->
- <bean id="book" class="com.atguigu.spring5.CollectionTypeTest.Book">
- <property name="list" ref="bookList"></property>
- </bean>
- </beans>
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("CollectionTypeBeans.xml");
- Book book = context.getBean("book", Book.class);
- book.show();
- }
输出结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。