Spring基礎知識(4)- Spring Bean (一)
在 Spring 中,構成應用程式主幹並由 Spring IoC 容器管理的物件稱為 Bean。Bean 是一個由 Spring IoC 容器例項化、組裝和管理的物件。
簡而言之:
(1) Bean 是物件,一個或者多個不限定;
(2) Bean 由 Spring IoC 容器管理;
(3) Bean 是 Spring 應用程式的主要組成部分(或稱為主要的功能模組);
IoC 容器通過獲取 Java 類和該類相關的 Spring 配置元資料來進行例項化、裝配和管理 Bean。
Spring 配置元資料一般包含如下資訊:
(1) 如何建立一個 Bean;
(2) Bean 的作用域、生命週期等資訊;
(3) Bean 的依賴關係、如何裝配等資訊;
Spring 的兩種 Bean 配置方式:
(1) 基於配置檔案 (XML格式、Properties格式) 的配置方式;
(2) 基於註解的配置方式;
即 Spring 配置元資料,可以在配置檔案裡,或在註解中。
1. 基於配置檔案 (XML格式和Properties格式) 的配置方式
(1) Beans 配置檔案 (主配置檔案)
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:context="http://www.springframework.org/schema/context" 5xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-4.0.xsd" >9 10 <bean id="man" class="com.example.Man"> 11 <!-- 建構函式注入 --> 12 <constructor-arg value="Spring constructor method" /> 13 14 <!-- setter 注入 --> 15 <property name="age" value="25"></property> 16 17 </bean> 18 <bean id="person" class="com.example.Person"> 19 <!-- 建構函式注入 --> 20 <constructor-arg ref="man" type="com.example.Man"/> 21 22 <!-- setter 注入 Null, Empty 值 --> 23 <property name="nullValue"><null/></property> 24 <property name="emptyValue" value=""></property> 25 26 <!--使用 <![CDATA[]]> setter 注入特殊符號 --> 27 <property name="literalString"> 28 <value><![CDATA[<www.test.com>]]></value> 29 </property> 30 31 <!-- setter 注入集合<array>, <list>, <map>, <set> --> 32 <property name="array"> 33 <array> 34 <value>Array 1</value> 35 <value>Array 2</value> 36 <value>Array 3</value> 37 </array> 38 </property> 39 <property name="list"> 40 <list> 41 <value>List 1</value> 42 <value>List 2</value> 43 <value>List 3</value> 44 </list> 45 </property> 46 <property name="map"> 47 <map> 48 <entry key="1" value="Map 1"></entry> 49 <entry key="2" value="Map 2"></entry> 50 </map> 51 </property> 52 <property name="set"> 53 <set> 54 <value>Set 1</value> 55 <value>Set 2</value> 56 <value>Set 3</value> 57 </set> 58 </property> 59 60 </bean> 61 62 </beans>
上面的 <bean>標籤就是用於配置 Bean 物件讓 Spring 來建立的,<constructor-arg>標籤就是告訴 Spring 使用建構函式注入,<property>標籤就是告訴 Spring 使用 setter 注入。
Spring 可以通過 2 種方式實現屬性注入:建構函式注入和 setter 注入(又稱設值注入)。
*注:這裡 Beans 配置檔案儲存為 spring-beans.xml,具體使用方式,參考 “Spring基礎知識(2)- 建立 Spring 程式”,下同。
(2) 示例
1 package com.example; 2 3 import java.util.List; 4 import java.util.Map; 5 import java.util.Set; 6 import java.util.Arrays; 7 8 import org.springframework.context.support.ClassPathXmlApplicationContext; 9 import org.springframework.context.ApplicationContext; 10 11 public class App { 12 public static void main( String[] args ) { 13 ApplicationContext context1 = new ClassPathXmlApplicationContext("spring-beans.xml"); 14 Person person = (Person) context1.getBean("person"); 15 person.display(); 16 } 17 } 18 19 class Man { 20 String name; 21 int age; 22 23 // 有其它有參建構函式,所以不要省略 24 public Man() { 25 26 } 27 28 public Man(String name) { 29 this.name = name; 30 } 31 32 public void display() { 33 System.out.println("Man -> display(): name = " + name + ", age = " + age); 34 } 35 36 public void setAge(int age) { 37 this.age = age; 38 } 39 } 40 41 class Person { 42 private Man man; 43 private String nullValue; 44 private String emptyValue; 45 private String literalString; 46 private String[] array; 47 private List<String> list; 48 private Map<String, String> map; 49 private Set<String> set; 50 51 // 有其它有參建構函式,所以不要省略 52 public Person() { 53 54 } 55 56 public Person(Man man) { 57 this.man = man; 58 } 59 60 public void display() { 61 man.display(); 62 System.out.println("Person -> display(): nullValue = " + nullValue 63 + ", emptyValue = " + emptyValue 64 + ", literalString = " + literalString); 65 System.out.println("Person -> display(): array = " + Arrays.toString(array)); 66 System.out.println("Person -> display(): list = " + list); 67 System.out.println("Person -> display(): map = " + map); 68 System.out.println("Person -> display(): set = " + set); 69 } 70 71 public void setNullValue(String nullValue) { 72 this.nullValue = nullValue; 73 } 74 75 public void setEmptyValue(String emptyValue) { 76 this.emptyValue = emptyValue; 77 } 78 79 public void setLiteralString(String literalString) { 80 this.literalString = literalString; 81 } 82 83 public void setArray(String[] array) { 84 this.array = array; 85 } 86 87 public void setList(List<String> list) { 88 this.list = list; 89 } 90 91 public void setMap(Map<String, String> map) { 92 this.map = map; 93 } 94 95 public void setSet(Set<String> set) { 96 this.set = set; 97 } 98 }
2. 基於註解的配置方式
(1) Beans 配置檔案 (主配置檔案)
Spring 預設不使用註解裝配 Bean,因此我們需要在 Spring 的 XML 配置中,通過 <context:component-scan> 元素開啟 Spring Beans的自動掃描功能。
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:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-4.0.xsd" > 9 10 <context:component-scan base-package="com.example" /> 11 12 </beans>
新增 <context:component-scan> 標籤,spring 就會去自動掃描 com.example 包內的 java 檔案,如果掃描到檔案中帶有@Service, @Component, @Repository ,@Controller 等這些註解的類,則把這些類註冊為 bean。
(2) 示例
1 package com.example; 2 3 import java.util.List; 4 import java.util.Map; 5 import java.util.Set; 6 import java.util.Arrays; 7 8 import javax.annotation.Resource; 9 import org.springframework.stereotype.Component; 10 import org.springframework.beans.factory.annotation.Value; 11 12 import org.springframework.context.support.ClassPathXmlApplicationContext; 13 import org.springframework.context.ApplicationContext; 14 15 public class App { 16 public static void main( String[] args ) { 17 ApplicationContext context1 = new ClassPathXmlApplicationContext("spring-beans.xml"); 18 Person2 person2 = (Person2) context1.getBean("person2"); 19 person2.display(); 20 } 21 } 22 23 @Component("man2") 24 class Man2 { 25 @Value("Spring setter method") 26 String name; 27 28 // 有其它有參建構函式,所以不要省略 29 public Man2() { 30 31 } 32 33 public Man2(String name) { 34 this.name = name; 35 } 36 37 public void display() { 38 System.out.println("Man2 -> display(): name = " + name); 39 } 40 } 41 42 @Component("person2") 43 class Person2 { 44 @Resource(name="man2") 45 private Man2 man2; 46 47 private String nullValue; 48 @Value("") 49 private String emptyValue; 50 @Value("<www.spring.com>") 51 private String literalString; 52 53 @Value("#{'1,2,3'.split(',')}") 54 private String[] array; 55 @Value("#{'\"Java\",\"Spring\",\"Idea\"'.split(',')}") 56 private List<String> list; 57 @Value("#{'\"Tree\",\"Dog\",\"River\"'.split(',')}") 58 private Set<String> set; 59 @Value("#{{\"name\":\"Tester\",\"age\": 21}}") 60 private Map<String, String> map; 61 62 // 有其它有參建構函式,所以不要省略 63 public Person2() { 64 65 } 66 67 public Person2(Man2 man2) { 68 this.man2 = man2; 69 } 70 71 public void display() { 72 man2.display(); 73 System.out.println("Person2 -> display(): nullValue = " + nullValue 74 + ", emptyValue = " + emptyValue 75 + ", literalString = " + literalString); 76 System.out.println("Person2 -> display(): array = " + Arrays.toString(array)); 77 System.out.println("Person2 -> display(): list = " + list); 78 System.out.println("Person2 -> display(): set = " + set); 79 System.out.println("Person2 -> display(): map = " + map); 80 } 81 }