第18章 Spring核心之IOC
18.1 Sring概述
18.1.1 初識Sring
Spring 有可能成為所有企業(yè)應(yīng)用程序的一站式服務(wù)點鳍征,然而,Spring 是模塊化的,允許你挑選和選擇適用于你的模塊参滴,不必要把剩余部分也引入拉宗。下面的部分對在 Spring 框架中所有可用的模塊給出了詳細的介紹。
Spring 框架提供約 20 個模塊渡冻,可以根據(jù)應(yīng)用程序的要求來使用戚扳。
Spring 體系結(jié)構(gòu)
Spring 模塊可以分類成:數(shù)據(jù)訪問和集成、Web遠程調(diào)用族吻、面向切面編程帽借、Instrumentation,Spring核心容器超歌、測試(參考資料Spring4實戰(zhàn))
總共六大模塊砍艾,可以分類成以下。
- 數(shù)據(jù)訪問和集成:
JDBC:spring-jdbc-4.3.18.RELEASE.jar
Transaction:spring-tx-4.3.18.RELEASE.jar
ORM:spring-orm-4.3.18.RELEASE.jar
OXM:spring-oxm-4.3.18.RELEASE.jar
Messaging:spring-messaging-4.3.18.RELEASE.jar
JMS:spring-jms-4.3.18.RELEASE.jar
- Web遠程調(diào)用:
Web:spring-web-4.3.18.RELEASE.jar
Web Servlet:spring-webmvc-4.3.18.RELEASE.jar
Web portlet:spring-webmvc-portlet-4.3.18.RELEASE.jar
Web Socket:spring-websocket-4.3.18.RELEASE.jar
- 面向切面編程:(核心)
Aop:spring-aop-4.3.18.RELEASE.jar
Aspects:spring-aspects-4.3.18.RELEASE.jar
- Instrumentation(工具):這個使用場景非常有限巍举,主要提供了為JVM添加代理的功能
Instrumentation:spring-instrument-4.3.18.RELEASE.jar
Instrument Tomcat:spring-instrument-tomcat-4.3.18.RELEASE.jar
- Spring 核心容器:(核心)
Beans:spring-beans-4.3.18.RELEASE.jar
Core:spring-core-4.3.18.RELEASE.jar
Context:spring-context-4.3.18.RELEASE.jar
Expression:spring-expression-4.3.18.RELEASE.jar
Context support:spring-context-support-4.3.18.RELEASE.jar
- 測試:
Test:spring-test-4.3.18.RELEASE.jar
18.1.2 Spring的獲取
從 http://repo.spring.io/release/org/springframework/spring
下載最新版本的 Spring 框架的二進制文件脆荷。
18.1.3 簡單配置Spring
Spring的jar包相關(guān)功能說明
jar包名稱 | 說 明 |
---|---|
spring.jar | 整個Spring模塊,包含表中所有jar的功能 |
spring-core.jar | Spring的核心模塊懊悯,包含Ioc容器 |
spring-aop.jar | Spring的Aop模塊 |
spring-context.jar | Sping的上下文蜓谋,包含ApplicationContext容器 |
spring-dao.jar | Spring的DAO模塊,包含對DAO與JDBC的支持 |
spring-orm.jar | Sping的ORM模塊炭分,支持Hibernate桃焕、JDBC的支持 |
spring-web.jar | Sping的Web模塊,包含Web application context |
spring-webmvc.jar | Spring的MVC框架模塊 |
18.1.4 使用BeanFactory管理bean
- 1.創(chuàng)建web項目捧毛,導(dǎo)入jar包
commons-logging-1.0.4.jar
观堂、spring.jar
- 2.創(chuàng)建User.java 實體類
public class User {
private String name;//用戶姓名
private Integer age;//年齡
private String sex;//性別
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
- 3.創(chuàng)建操作類Manger.java,此處使用Junit Test測試
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Manger {
@Test
public void main() {
Resource resource = new ClassPathResource("applicationContext.xml"); //裝載配置文件
BeanFactory factory = new XmlBeanFactory(resource);
User user = (User) factory.getBean("user"); //獲取Bean
System.out.println("用戶姓名——"+user.getName());//輸出用戶的姓名
System.out.println("用戶年齡——"+user.getAge());//輸出用戶的年齡
System.out.println("用戶性別——"+user.getSex());//輸出用戶的性別
}
}
- 4.創(chuàng)建applicationContext.xml呀忧,在源碼目錄下Src目錄
<?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-2.5.xsd">
<!-- User Bean -->
<bean name="user" class="com.hwp.spring.User">
<property name="name">
<value>小強</value>
</property>
<property name="age">
<value>26</value>
</property>
<property name="sex">
<value>男</value>
</property>
</bean>
</beans>
<property> 中的name要和實體類對應(yīng)上师痕,<bean>中class要完整的包名、類名
18.1.5 ApllicationContext的應(yīng)用
Spring ApplicationContext 容器
Application Context 是 BeanFactory 的子接口而账,也被成為 Spring 上下文胰坟。
Application Context 是 spring 中較高級的容器。和 BeanFactory 類似福扬,它可以加載配置文件中定義的 bean腕铸,將所有的 bean 集中在一起,當(dāng)有請求的時候分配 bean铛碑。 另外狠裹,它增加了企業(yè)所需要的功能,比如汽烦,從屬性文件中解析文本信息和將事件傳遞給所指定的監(jiān)聽器涛菠。這個容器在 org.springframework.context.ApplicationContext interface 接口中定義。
ApplicationContext 包含 BeanFactory 所有的功能,一般情況下俗冻,相對于 BeanFactory礁叔,ApplicationContext 會更加優(yōu)秀。當(dāng)然迄薄,BeanFactory 仍可以在輕量級應(yīng)用中使用琅关,比如移動設(shè)備或者基于 applet 的應(yīng)用程序。
最常被使用的 ApplicationContext 接口實現(xiàn):
FileSystemXmlApplicationContext:該容器從 XML 文件中加載已被定義的 bean讥蔽。在這里涣易,你需要提供給構(gòu)造器 XML 文件的完整路徑。
ClassPathXmlApplicationContext:該容器從 XML 文件中加載已被定義的 bean冶伞。在這里新症,你不需要提供 XML 文件的完整路徑,只需正確配置 CLASSPATH 環(huán)境變量即可响禽,因為徒爹,容器會從 CLASSPATH 中搜索 bean 配置文件。
WebXmlApplicationContext:該容器會在一個 web 應(yīng)用程序的范圍內(nèi)加載在 XML 文件中已被定義的 bean芋类。
實例
- 1.創(chuàng)建web工程隆嗅,導(dǎo)包
commons-logging-1.0.4.jar和spring.jar
2.創(chuàng)建package,com.hwp.spring梗肝,在文件夾下創(chuàng)建HelloWorld.java和MainApp.java兩個類
HelloWorld.java
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
- 2.MainApp.java
public class Manger {
//此處使用Junit測試
@Test
public void main() {
ApplicationContext context = new FileSystemXmlApplicationContext
("src\\Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
- 3.在源碼目錄下即Src目錄下榛瓮,創(chuàng)建Beans.xml
<?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-3.0.xsd">
<bean id="helloWorld" class="com.hwp.spring.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
</beans>
- 4.運行后控制臺輸出
Your Message : Hello World!
18.2 依賴注入
18.2.1 什么是控制反轉(zhuǎn)與依賴注入
依賴注入有3種實現(xiàn)類型
- 1.接口注入(Spring不支持)
基于接口將調(diào)用與實現(xiàn)分離铺董。這種依賴注入方式必須實現(xiàn)容器所規(guī)定的接口巫击,使程序代碼和容器的API綁定在一起,這不是理想的依賴注入方式精续。 - 2.Setter注入(Spring支持)
基于JavaBean的Setter方法為屬性賦值坝锰。在實際開發(fā)中得到了最廣泛的應(yīng)用。
public class HelloWorld {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
- 3.構(gòu)造器注入(Spring支持)
基于構(gòu)造方法為屬性賦值重付。容器通過調(diào)用類的構(gòu)造方法
public class HelloWorld {
private String message;
public HelloWorld(String message) {
super();
this.message = message;
}
}
18.2.2 bean的配置
<?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-3.0.xsd">
<bean id="helloWorld" class="com.hwp.spring.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
</beans>
id屬性為bean的名稱顷级,class屬性為對應(yīng)的類名,這樣通過BeanFactory容器的getBean("test")方法就可以獲取到該類的實例确垫。
18.2.3 Setter注入
- 1.創(chuàng)建User.java 實體類
public class User {
private String name;//用戶姓名
private Integer age;//年齡
private String sex;//性別
... //省略Setter()和Getter()方法
}
- 2.創(chuàng)建applicationContext.xml弓颈,在源碼目錄下Src目錄
<?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-2.5.xsd">
<!-- User Bean -->
<bean name="user" class="com.hwp.spring.User">
<property name="name">
<value>小強</value>
</property>
<property name="age">
<value>26</value>
</property>
<property name="sex">
<value>男</value>
</property>
</bean>
</beans>
- 3.創(chuàng)建操作方法
public class Manger {
@Test
public void main() {
Resource resource = new ClassPathResource("applicationContext.xml"); //裝載配置文件
BeanFactory factory = new XmlBeanFactory(resource);
User user = (User) factory.getBean("user"); //獲取Bean
System.out.println("用戶姓名——"+user.getName());//輸出用戶的姓名
System.out.println("用戶年齡——"+user.getAge());//輸出用戶的年齡
System.out.println("用戶性別——"+user.getSex());//輸出用戶的性別
}
}
控制臺輸出信息
用戶姓名——小強
用戶年齡——26
用戶性別——男
如果當(dāng)JavaBean的某個元素是List集合類型或數(shù)組時,需要使用<list>標(biāo)簽為List集合類型或數(shù)組的每一個元素賦值
示例代碼
List
<!-- 配置List屬性 -->
<bean id="person3" class="com.kejian.spring.bean.collectionbean.PersonForList">
<property name="name" value="Peter"></property>
<property name="age" value="30"></property>
<property name="cars">
<list>
<ref bean="car1"/>
<ref bean="car2"/>
</list>
</property>
</bean>
Map
<!-- 配置Map屬性 -->
<bean id="person4" class="com.kejian.spring.bean.collectionbean.PersonForMap">
<property name="name" value="Mike"></property>
<property name="age" value="35"></property>
<property name="cars">
<map>
<entry key="ACar" value-ref="car1"></entry>
<entry key="BCar" value-ref="car2"></entry>
</map>
</property>
</bean>
properties
<!-- 通過props來配置properties -->
<bean id="datasource" class="com.kejian.spring.bean.collectionbean.Datasource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">root</prop>
<prop key="url">jdbc:mysql.///test</prop>
<prop key="driver">jdbc.mysql.driver</prop>
</props>
</property>
</bean>
18.2.4 構(gòu)造器注入
- 1.Javabean
public class User{
private String name;
private int age;
private String sex;
public User(String name,int age,String sex){
this.name=name;
this.age=age;
this.sex=sex;
}
public void printlnfo(){
System.out.println("用戶姓名——"+name);//輸出用戶的姓名
System.out.println("用戶年齡——"+age);//輸出用戶的年齡
System.out.println("用戶性別——"+sex);//輸出用戶的性別
}
}
- 2.創(chuàng)建applicationContext.xml删掀,在源碼目錄下Src目錄
<?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-2.5.xsd">
<!-- User Bean -->
<bean name="user" class="com.hwp.spring.User">
<constructor-arg>
<value>小強</value>
</constructor-arg>
<constructor-arg>
<value>20</value>
</constructor-arg>
<constructor-arg>
<value>男</value>
</constructor-arg>
</bean>
</beans>
容器通過多個<constructor-arg>標(biāo)簽完成了對構(gòu)造方法的傳參翔冀,但是如果標(biāo)簽的賦值順序與構(gòu)造方法中參數(shù)的順序類型不同,程序會產(chǎn)生異常披泪,可以使用<constructor-arg>元素的index屬性和type屬性解決此類問題纤子。
示例代碼
<beans>
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value="2001 index="0"/>
<constructor-arg type="java.lang.String" value="Zara" index="1"/>
</bean>
</beans>
18.2.5 引用其他的bean
- 1.創(chuàng)建web項目,導(dǎo)入jar包,commons-logging-1.0.4.jar控硼、spring-webmvc.jar和spring.jar
- 2.創(chuàng)建javabean泽论,User.java
public class User {
private String name;//用戶姓名
private Integer age;//年齡
private String sex; //性別
... //省略Setter、Getter方法
public void println() {
System.out.println("名字---"+name);
System.out.println("年紀(jì)---"+age+"");
System.out.println("性別---"+sex);
}
}
- 創(chuàng)建Manger.java 繼承于AbstractController類
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class Manger extends AbstractController {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
user.println();
return null;
}
}
- 4.創(chuàng)建applicationContext.xml
<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-2.5.xsd">
<!-- User Bean -->
<bean id="user" class="com.hwp.spring.User">
<property name="name" >
<value>小強</value>
</property>
<property name="age">
<value>26</value>
</property>
<property name="sex">
<value>男</value>
</property>
</bean>
<!-- 注入JavaBean -->
<bean name="/main.do" class="com.hwp.spring.Manger">
<property name="user">
<ref local="user"/>
</property>
</bean>
</beans>
- 5.設(shè)置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>18_3spring</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
- 6.編寫index.jsp
<!-- 主要代碼 -->
<body>
<a href="http://localhost:8080/18_3spring/main.do">執(zhí)行JavaBean的注入</a>
</body>
代碼部署后卡乾,點擊超鏈接翼悴,控制臺輸出信息
名字---小強
年紀(jì)---26
性別---男
18.2.6 匿名內(nèi)部javabean的創(chuàng)建
18.3 自動裝配
18.3.1 按bean名稱裝配
<bean>元素的byname屬性以屬性名區(qū)分自動裝配,
- 1.定義User類
public class User{
private String name; //用戶名
private int age; //年齡
private String sex; //性別
... // 省略的Setter()和Getter()方法
}
- 2.定義PrintlInfo類幔妨,將User對象注入到PrintInfo對象中抄瓦,按我的個人理解是實體類嵌套一層實體類
public class PrintlInfo{
private User user;
... //省略Setter()、Getter()方法
public void PrintUser(){
System.out.println("打印的屬性");
System.out.println("----------");
System.out.println("姓名---"+user.getName()); //輸出用戶的姓名
System.out.println("年紀(jì)---"+user.getAge()); //輸出用戶的年齡
System.out.println("性別---"+user.getSex()); //輸出用戶的性別
}
}
- 3.applicationContext.xml陶冷,存放在在源碼目錄下即Src目錄下
<?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-2.5.xsd">
<!-- User Bean -->
<bean id="user" class="com.mr.user.User">
<property name="name">
<value>小強</value>
</property>
<property name="age">
<value>26</value>
</property>
<property name="sex">
<value>男</value>
</property>
</bean>
<bean autowire="byName" id="printInfo" class="com.mr.user.PrintInfo" />
</beans>
- 4.測試類钙姊,此處使用junit測試
public class Manger {
@Test
public void main() {
Resource resource = new ClassPathResource("applicationContext.xml"); //裝載配置文件
BeanFactory factory = new XmlBeanFactory(resource);
PrintInfo printInfo = (PrintInfo) factory.getBean("printInfo"); //獲取Bean
printInfo.PrintUser();//輸出JavaBean的屬性值
}
}
測試,控制臺輸出
PrintInfo打印的User屬性
用戶姓名--小強
用戶年齡--26
用戶性別--男
18.3.2 按bean類型裝配
按類型裝配埂伦,除了applicationContext.xml中以下代碼變動煞额,其他的代碼一樣
<bean autowire="byType" id="printInfo" class="com.mr.user.PrintInfo" />
18.3.3 自動裝配的其他方式
在Spring中還有另外3種自動裝配方式,通過設(shè)置autowire的不同屬性值來實現(xiàn)沾谜。
- 1.no屬性
即不啟用自動裝配膊毁。Autowire默認的值。不使用Autowire基跑,引用關(guān)系顯示聲明婚温,spring的reference也建議不用autoware,因為這會破壞模塊關(guān)系的可讀性媳否。 - 2.constructor屬性
通byType一樣栅螟,也是通過類型查找依賴對象。與byType的區(qū)別在于它不是使用Seter方法注入篱竭,而是使用構(gòu)造子注入力图。如果容器中沒有找到與構(gòu)造器參數(shù)類型一致的bean,那么拋出異常掺逼。 - 3.autodetect屬性
在byType和constructor之間自動的選擇注入方式吃媒。通過bean類的自省機制(introspection)來決定是使用constructor還是byType方式進行自動裝配。如果發(fā)現(xiàn)默認的構(gòu)造器吕喘,那么將使用byType方式,否則采用
constructor氯质。
Sping自動裝配的優(yōu)缺點
優(yōu)點 - 自動裝配可以大大地減少屬性和構(gòu)造器參數(shù)的指派病梢。
- 自動裝配也可以在解析對象時更新配置胃珍。
缺點
- 在property和constructor-arg設(shè)置中的依賴總是重載自動裝配梁肿,我們無法對原始類型(如int觅彰,long,boolean等就是首字母小寫的那些類型)填抬,還有String烛芬,Classes做自動裝配。這是受限于設(shè)計飒责。
- 自動裝配跟直接裝配(explicit wiring)相比較宏蛉,在準(zhǔn)確性方便還是差那么點,雖然沒有明確地說明揍堰,但是Spring還是盡量避免這種模棱兩可的情況嗅义,導(dǎo)致出現(xiàn)沒預(yù)料到的結(jié)果。
- Spring容器生成文檔的工具可能會不能使用裝配的信息蝙眶。
- 容器中多個bean的定義可能要對setter和構(gòu)造器參數(shù)做類型匹配才能做依賴注入褪那,雖然對于array,collection和map來說不是啥問題霹崎,但是對于只有單一值的依賴來講冶忱,這就有點講不清楚了囚枪,所以如果沒有唯一的bean定義劳淆,那只能拋出異常
18.4 bean的作用域
18.4.1 了解Spring中的bean
Bean 定義
被稱作 bean 的對象是構(gòu)成應(yīng)用程序的支柱也是由 Spring IoC 容器管理的。bean 是一個被實例化沛鸵,組裝缆八,并通過 Spring IoC 容器所管理的對象奈辰。這些 bean 是由用容器提供的配置元數(shù)據(jù)創(chuàng)建的乱豆,例如,已經(jīng)在先前章節(jié)看到的瑟啃,在 XML 的表單中的 定義蛹屿。
bean 定義包含稱為配置元數(shù)據(jù)的信息岩榆,下述容器也需要知道配置元數(shù)據(jù):
- 如何創(chuàng)建一個 bean
- bean 的生命周期的詳細信息
bean 的依賴關(guān)系
上述所有的配置元數(shù)據(jù)轉(zhuǎn)換成一組構(gòu)成每個 bean 定義的下列屬性朗恳。
屬性 | 描述 |
---|---|
class | 這個屬性是強制性的,并且指定用來創(chuàng)建 bean 的 bean 類油航。 |
name | 這個屬性指定唯一的 bean 標(biāo)識符怀浆。在基于 XML 的配置元數(shù)據(jù)中执赡,你可以使用 ID 和/或 name 屬性來指定 bean 標(biāo)識符。 |
scope | 這個屬性指定由特定的 bean 定義創(chuàng)建的對象的作用域奠伪,它將會在 bean 作用域的章節(jié)中進行討論首懈。 |
constructor-arg | 它是用來注入依賴關(guān)系的究履,并會在接下來的章節(jié)中進行討論。 |
properties | 它是用來注入依賴關(guān)系的藐俺,并會在接下來的章節(jié)中進行討論。 |
autowiring mode | 它是用來注入依賴關(guān)系的蜜葱,并會在接下來的章節(jié)中進行討論耀石。 |
lazy-initialization mode | 延遲初始化的 bean 告訴 IoC 容器在它第一次被請求時滞伟,而不是在啟動時去創(chuàng)建一個 bean 實例。 |
initialization 方法 | 在 bean 的所有必需的屬性被容器設(shè)置之后野崇,調(diào)用回調(diào)方法乓梨。它將會在 bean 的生命周期章節(jié)中進行討論清酥。 |
destruction 方法 | 當(dāng)包含該 bean 的容器被銷毀時,使用回調(diào)方法臭觉。它將會在 bean 的生命周期章節(jié)中進行討論蝠筑。 |
18.4.2 singleton的作用域
singleton 是默認的作用域揩懒,也就是說已球,當(dāng)定義 Bean 時,如果沒有指定作用域配置項退疫,則 Bean 的作用域被默認為 singleton鸽素。
當(dāng)一個bean的作用域為Singleton馍忽,那么Spring IoC容器中只會存在一個共享的bean實例,并且所有對bean的請求坝冕,只要id與該bean定義相匹配瓦呼,則只會返回bean的同一實例央串。
也就是說,當(dāng)將一個bean定義設(shè)置為singleton作用域的時候稳摄,Spring IoC容器只會創(chuàng)建該bean定義的唯一實例厦酬。
Singleton是單例類型瘫想,就是在創(chuàng)建起容器時就同時自動創(chuàng)建了一個bean的對象国夜,不管你是否使用,他都存在了旋廷,每次獲取到的對象都是同一個對象礼搁。注意馒吴,Singleton作用域是Spring中的缺省作用域。你可以在 bean 的配置文件中設(shè)置作用域的屬性為 singleton豪治,如下所示:
<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="singleton">
<!-- collaborators and configuration for this bean go here -->
</bean>
18.4.3 prototype的作用域
18.5 對bean的特殊處理
18.5.1 初始化與銷毀
18.5.2 自定義屬性編輯器
Spring內(nèi)置的屬性編輯器說明
屬性名稱 | 說 明 | 屬性名稱 | 說 明 |
---|---|---|---|
ByteArrayPropertyEdior | 字節(jié)數(shù)組編輯器 | CustomDateEditor | Date類型編輯器 |
CharacterEdior | 字符類型編輯器 | CustomNumberEditor | 數(shù)值類型編輯器 |
CharArrayEdior | 字符編輯器 | FileEditor | 文件類型編輯器 |
ClassEdiotor | 字節(jié)數(shù)組編輯器 | InputStramEditor | 輸入流類型編輯器 |
CustomBooleanEditor | Boolean類型編輯器 | LocaleEditor | 地域信息編輯器 |
CustomCollectionEditor | Collection集合類型編輯器 | PropertiesEditor | 屬性類型編輯器 |