Spring的DI依赖注入(set注入)
程序员文章站
2022-05-29 15:15:19
...
为什么叫set注入,是因为实际还是通过反射调用set方法注入,所以要注入的实体类字段要有相应的set方法
现在我们通过一个例子来说明一下
环境
我们有学生类Student,班级类ClassRoom,科目类Subject,书类Book,我们要往学生类的属性注入值
学生类Student的代码
package com.lingaolu; import java.util.*; /** * @author 林高禄 * @create 2020-11-03-9:19 */ public class Student { private Long id; private String name; private String girlFriend; private ClassRoom classRoom; private String[] games; private List<Book> books; private Map<String,List<Book>> bookMap; private Set<Subject> subjectSet; private Properties properties; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGirlFriend() { return girlFriend; } public void setGirlFriend(String girlFriend) { this.girlFriend = girlFriend; } public ClassRoom getClassRoom() { return classRoom; } public void setClassRoom(ClassRoom classRoom) { this.classRoom = classRoom; } public String[] getGames() { return games; } public void setGames(String[] games) { this.games = games; } public List<Book> getBooks() { return books; } public void setBooks(List<Book> books) { this.books = books; } public Map<String, List<Book>> getBookMap() { return bookMap; } public void setBookMap(Map<String, List<Book>> bookMap) { this.bookMap = bookMap; } public Set<Subject> getSubjectSet() { return subjectSet; } public void setSubjectSet(Set<Subject> subjectSet) { this.subjectSet = subjectSet; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", girlFriend='" + girlFriend + '\'' + ", classRoom=" + classRoom + ", games=" + Arrays.toString(games) + ", books=" + books + ", bookMap=" + bookMap + ", subjectSet=" + subjectSet + ", properties=" + properties + '}'; } }
班级类ClassRoom的代码
package com.lingaolu; /** * @author 林高禄 * @create 2020-11-04-8:53 */ public class ClassRoom { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "ClassRoom{" + "name='" + name + '\'' + '}'; } }
科目类Subject的代码
package com.lingaolu; /** * @author 林高禄 * @create 2020-11-04-9:00 */ public class Subject { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Subject{" + "name='" + name + '\'' + '}'; } }
书类Book的代码
package com.lingaolu; /** * @author 林高禄 * @create 2020-11-04-8:52 */ public class Book { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + '}'; } }
普通属性
name赋值林高禄
空值
女朋友是null
引用对象
数组
List
Map
Set
Properties
综合代码
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="book2" class="com.lingaolu.Book"> <property name="name" value="我与小姨子的故事"/> </bean> <bean id="book3" class="com.lingaolu.Book"> <property name="name" value="老师的特殊辅导"/> </bean> <bean id="classRoom" class="com.lingaolu.ClassRoom"> <property name="name" value="材料103"/> </bean> <bean id="student" class="com.lingaolu.Student" > <property name="name" value="林高禄"/> <property name="girlFriend"> <null/> </property> <property name="classRoom" ref="classRoom"/> <property name="games"> <array> <value>LOL</value> <value>古墓丽影</value> </array> </property> <property name="books"> <list> <bean id="book1" class="com.lingaolu.Book"> <property name="name" value="花季少年"/> </bean> <ref bean="book2"/> </list> </property> <property name="bookMap"> <map> <entry key="老师系列"> <list> <ref bean="book3"/> </list> </entry> <entry key="空姐系列"> <list> <bean class="com.lingaolu.Book"> <property name="name" value="我与副驾驶的故事"/> </bean> </list> </entry> </map> </property> <property name="subjectSet"> <set> <bean class="com.lingaolu.Subject"> <property name="name" value="人体运动学"/> </bean> </set> </property> <property name="properties"> <props> <prop key="age">17</prop> </props> </property> </bean> </beans>
测试打印结果
上一篇: 逆战班:css浮动的简单介绍