本文主要講解Spring開發中三種不同的註入方式,以及集合數據類型的註入,僅供學習分享使用,如有不足之處,還請指正。 ...
本文主要講解Spring開發中三種不同的註入方式,以及集合數據類型的註入,僅供學習分享使用,如有不足之處,還請指正。
概述
Spring的註入方式一共有三種,如下所示:
- 通過set屬性進行註入,即通過屬性的geter,seter方法進入,如果沒有對應的方法,就會報錯。
- 通過構造函數進行註入,通過類的構造函數進行註入,如果參數個數和構造函數對應不上,也會跑異常。
- 通過p標簽進行註入,通過引入p標簽的方式進行註入。
前提準備
首先有一個Teacher類和Course類,課程需要有對應的教課老師,兩個類代碼如下:
Teacher類

1 package com.hex.first; 2 3 /** 4 * 老師類 5 * @author Administrator 6 * 7 */ 8 public class Teacher { 9 10 /** 11 * 姓名 12 */ 13 private String teacherName=""; 14 15 /** 16 * 年齡 17 */ 18 private int teacherAge=0; 19 20 21 22 public String getTeacherName() { 23 return teacherName; 24 } 25 26 public void setTeacherName(String teacherName) { 27 this.teacherName = teacherName; 28 } 29 30 public int getTeacherAge() { 31 return teacherAge; 32 } 33 34 public void setTeacherAge(int teacherAge) { 35 this.teacherAge = teacherAge; 36 } 37 38 39 40 /** 41 * 教師信息 42 */ 43 @Override 44 public String toString() { 45 return "Teacher [teacherName=" + teacherName + ", teacherAge=" + teacherAge + "]"; 46 } 47 48 49 /** 50 * 構造函數 51 */ 52 public Teacher() { 53 super(); 54 // TODO Auto-generated constructor stub 55 } 56 57 /** 58 * 構造函數 59 * @param teacherName 60 * @param teacherAge 61 */ 62 public Teacher(String teacherName, int teacherAge) { 63 this.teacherName = teacherName; 64 this.teacherAge = teacherAge; 65 } 66 67 }View Code
Course類

1 package com.hex.first; 2 3 /** 4 * 課程 5 * @author Administrator 6 * 7 */ 8 public class Course { 9 10 /** 11 * 課程名 12 */ 13 private String courseName=""; 14 15 /** 16 * 課時長 17 */ 18 private int courseHour=0; 19 20 /** 21 * 課程老師 22 */ 23 private Teacher teacher; 24 25 public String getCourseName() { 26 return courseName; 27 } 28 29 public void setCourseName(String courseName) { 30 this.courseName = courseName; 31 } 32 33 public int getCourseHour() { 34 return courseHour; 35 } 36 public void setCourseHour(int courseHour) { 37 this.courseHour = courseHour; 38 } 39 40 public Teacher getTeacher() { 41 return teacher; 42 } 43 44 public void setTeacher(Teacher teacher) { 45 this.teacher = teacher; 46 } 47 48 49 /** 50 * 構造函數 51 */ 52 public Course() { 53 super(); 54 // TODO Auto-generated constructor stub 55 } 56 57 /** 58 * 構造函數 59 * @param courseName 60 * @param courseHour 61 */ 62 public Course(String courseName, int courseHour) { 63 this.courseName = courseName; 64 this.courseHour = courseHour; 65 } 66 67 68 /** 69 * 三個參數的構造函數 70 * @param courseName 71 * @param courseHour 72 * @param teacher 73 */ 74 public Course(String courseName, int courseHour, Teacher teacher) { 75 this.courseName = courseName; 76 this.courseHour = courseHour; 77 this.teacher = teacher; 78 } 79 80 /** 81 * 重寫toString()方法 82 */ 83 @Override 84 public String toString() { 85 return "Course [courseName=" + courseName + ", courseHour=" + courseHour + ",teacher="+teacher+"]"; 86 } 87 88 89 }View Code
通過屬性的方式進行註入
在applicatonContext.xml中配置如下:
1 <!-- 第1種: 通過set 方法進行賦值,屬性必須有對應的set+屬性名,第一個字母大寫的方法,才可以賦值 --> 2 <bean id="teacher1" class="com.hex.first.Teacher"> 3 <property name="teacherName" value="hex"></property> 4 <property name="teacherAge" value="20"></property> 5 </bean> 6 <!-- 7 如果是常規的數據類型,採用property name value進行賦值 8 如果是對象類型,採用property name ref 進行賦值 9 --> 10 <bean id="course1" class="com.hex.first.Course"> 11 <property name="courseName" value="Java"></property> 12 <property name="courseHour" value="120"></property> 13 <property name="teacher" ref="teacher1"></property> 14 </bean>
通過構造函數的方式進行註入
如果在採用構造函數的方式進行註入,則必須有對應的構造函數與之對應,在applicatonContext.xml中配置如下:
1 <!--第2種: 採用構造函數,進行賦值 --> 2 <!-- 3 如果構造參數的順序,和構造函數中的參數順序是一樣的,則name(參數名稱),index(參數順序號),type(參數數據類型)均可以省略 4 如果不一樣,則需要用name,index的其中一個來標識,如果參數的type不同,則也可以用type來區分 5 --> 6 <bean id="teacher2" class="com.hex.first.Teacher"> 7 <constructor-arg name="teacherName" value="hex" index="0" type="String"></constructor-arg> 8 <constructor-arg name="teacherAge" value="22" index="1" type="int"></constructor-arg> 9 </bean> 10 <!-- 11 如果是常規的數據類型,採用constructor-arg name value進行賦值 12 如果是對象類型,採用constructor-arg name ref 進行賦值 13 --> 14 <bean id="course2" class="com.hex.first.Course"> 15 <constructor-arg name="courseName" value=".Net C#" index="0" type="String"></constructor-arg> 16 <constructor-arg name="courseHour" value="110" index="1" type="int"></constructor-arg> 17 <constructor-arg name="teacher" ref="teacher2" index="2" type="com.hex.first.Teacher"></constructor-arg> 18 </bean>
採用p標簽的方式進行註入
如果需要採用p標簽進行註入,則需要引入命名空間,如下所示:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd">
配置如下:
1 <!-- 第3種:通過P標簽實現 ,需要引入命名空間--> 2 <!-- 3 如果就常規數據類型,採用p:屬性名="value"的方式進行賦值 4 如果是對象數據類型,採用p:屬性名-ref=""的方式賦值 5 --> 6 <bean id="teacher3" class="com.hex.first.Teacher" p:teacherName="zs" p:teacherAge="21"> 7 </bean> 8 <bean id="course3" class="com.hex.first.Course" p:courseName="C++" p:courseHour="100" p:teacher-ref="teacher3"> 9 10 </bean>
以上三種方式,在創建對象時,都是一樣的,如下所示:

1 //通過Spring進行註入,Spring上下文對象 2 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); 3 //從Spring的IOC容器中獲取id為course1的bean對象 4 Course course1 =(Course) context.getBean("course1"); 5 System.out.println(course1); 6 //從Spring的IOC容器中獲取id為course2的bean對象 7 Course course2 =(Course) context.getBean("course2"); 8 System.out.println(course2); 9 //從Spring的IOC容器中獲取id為course3的bean對象 10 Course course3 =(Course) context.getBean("course3"); 11 System.out.println(course3);View Code
集合類型的註入
首先有一個類,屬性為集合數據類型,代碼如下:

1 package com.hex.first; 2 3 import java.util.Arrays; 4 import java.util.List; 5 import java.util.Map; 6 import java.util.Properties; 7 import java.util.Set; 8 9 /** 10 * 測試集合數據類型 11 * @author Administrator 12 * 13 */ 14 public class TestCollection { 15 /** 16 * 數組信息 17 */ 18 private String[] arrInfo; 19 20 /** 21 * 列表信息 22 */ 23 private List<String> lstInfo; 24 25 /** 26 * Map信息 27 */ 28 private Map<String,String> mapInfo; 29 30 /** 31 * Set信息 32 */ 33 private Set<String> setInfo; 34 35 private Properties propInfo; 36 37 public String[] getArrInfo() { 38 return arrInfo; 39 } 40 41 public void setArrInfo(String[] arrInfo) { 42 this.arrInfo = arrInfo; 43 } 44 45 public List<String> getLstInfo() { 46 return lstInfo; 47 } 48 49 public void setLstInfo(List<String> lstInfo) { 50 this.lstInfo = lstInfo; 51 } 52 53 public Map<String, String> getMapInfo() { 54 return mapInfo; 55 } 56 57 public void setMapInfo(Map<String, String> mapInfo) { 58 this.mapInfo = mapInfo; 59 } 60 61 public Set<String> getSetInfo() { 62 return setInfo; 63 } 64 65 public void setSetInfo(Set<String> setInfo) { 66 this.setInfo = setInfo; 67 } 68 69 public Properties getPropInfo() { 70 return propInfo; 71 } 72 73 public void setPropInfo(Properties propInfo) { 74 this.propInfo = propInfo; 75 } 76 77 @Override 78 public String toString() { 79 return "TestCollection [arrInfo=" + Arrays.toString(arrInfo) + ", lstInfo=" + lstInfo + ", mapInfo=" + mapInfo 80 + ", setInfo=" + setInfo + ", propInfo=" + propInfo + "]"; 81 } 82 83 }View Code
通過Spring的配置文件進行註入,這裡採用setters屬性方式進行註入,如下所示:
1 <!-- 集合數據類型 --> 2 <bean id="test1" class="com.hex.first.TestCollection"> 3 <!-- 數組 --> 4 <property name="arrInfo"> 5 <array> 6 <value>籃球</value> 7 <value>足球</value> 8 <value>網球</value> 9 </array> 10 </property> 11 <!-- 列表 --> 12 <property name="lstInfo"> 13 <list> 14 <value>北京</value> 15 <value>上海</value> 16 <value>天津</value> 17 </list> 18 </property> 19 <!-- 鍵值對,每一個鍵值對為一個entry,有key,value兩個屬性 --> 20 <property name="mapInfo"> 21 <map> 22 <entry> 23 <key> 24 <value>football</value> 25 </key> 26 <value>足球</value> 27 </entry> 28 <entry> 29 <key> 30 <value>basketball</value> 31 </key> 32 <value>籃球</value> 33 </entry> 34 <entry> 35 <key> 36 <value>ping</value> 37 </key> 38 <value>乒乓球</value> 39 </entry> 40 </map> 41 </property> 42 <!-- 數據集 --> 43 <property name="setInfo"> 44 <set> 45 <value>AA</value> 46 <value>BB</value> 47 <value>CC</value> 48 </set> 49 </property> 50 <!-- props也是一個鍵值對 --> 51 <property name="propInfo"> 52 <props> 53 <prop key="boy">男孩</prop> 54 <prop key="girl">女孩</prop> 55 </props> 56 </property> 57 </bean>
以上測試信息如下所示:

1 十月 13, 2019 9:51:09 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh 2 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1f17ae12: startup date [Sun Oct 13 21:51:09 CST 2019]; root of context hierarchy 3 十月 13, 2019 9:51:10 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 4 信息: Loading XML bean definitions from class path resource [applicationContext.xml] 5 Course [courseName=Java, courseHour=120,teacher=Teacher [teacherName=hex, teacherAge=20]] 6 Course [courseName=.Net C#, courseHour=110,teacher=Teacher [teacherName=hex, teacherAge=22]] 7 Course [courseName=C++, courseHour=100,teacher=Teacher [teacherName=zs, teacherAge=21]] 8 TestClass [arrInfo=[籃球, 足球, 網球], lstInfo=[北京, 上海, 天津], mapInfo={football=足球, basketball=籃球, ping=乒乓球}, setInfo=[AA, BB, CC], propInfo={boy=男孩, girl=女孩}]View Code
特殊符號處理
在配置applicationContext.xml實現註入時,如果遇到特殊字元,則需要進行轉義,或者進行CDATA封裝,如下所示:
如字元串:<Java> ,則需要配置成:
1 <bean id="course1" class="com.hex.first.Course"> 2 <property name="courseName" value="<Java>"></property> 3 <property name="courseHour" value="120"></property> 4 <property name="teacher" ref="teacher1"></property> 5 </bean>
或者採用CDATA方式,如下所示:
1 <bean id="teacher1" class="com.hex.first.Teacher"> 2 <property name="teacherName"> 3 <value><![CDATA[<hex>]]></value> 4 </property> 5 <property name="teacherAge" value="20"></property> 6 </bean>
把代碼在CDATA中不需要轉義:解析器會忽略 CDATA 部分中的所有內容
備註
XML中的非法字元如下: