Spring中基于xml配置管理Bean的步驟
前言
在之前的學(xué)習(xí)中我們知道,容器是一個(gè)空間的概念,一般理解為可盛放物體的地方。在Spring容器通常理解為BeanFactory或者ApplicationContext。我們知道spring的IOC容器能夠幫我們創(chuàng)建對象,對象交給spring管理之后我們就不用手動(dòng)去new對象。
那么Spring是如何管理Bean的呢?

一、概念
簡而言之,Spring bean是Spring框架在運(yùn)行時(shí)管理的對象。Spring bean是任何Spring應(yīng)用程序的基本構(gòu)建塊。你編寫的大多數(shù)應(yīng)用程序邏輯代碼都將放在Spring bean中。
Spring bean的管理包括:
- 創(chuàng)建一個(gè)對象
- 提供依賴項(xiàng)(例如其他bean,配置屬性)
- 攔截對象方法調(diào)用以提供額外的框架功能
- 銷毀一個(gè)對象
Spring bean是框架的基本概念。作為Spring的用戶,你應(yīng)該對這個(gè)核心抽象有深刻的理解。
二、創(chuàng)建Bean對象的三種方式
2.1、使用默認(rèn)構(gòu)造函數(shù)創(chuàng)建方式
2.1.1、定義Bean
public class UserServiceImpl {
}2.1.2、主配置文件中配置bean
<!-- 方式一:使用默認(rèn)構(gòu)造函數(shù)方式創(chuàng)建Bean --> <beans> <bean id="userService" class="cn.bdqn.UserServiceImpl"></bean> </beans>
2.1.3、測試Bean
@Test
public void testUserService() throws Exception{
// 1、讀取主配置文件信息,獲取核心容器對象
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
// 2、從容器中根據(jù)id獲取對象(bean)
UserServiceImpl userService = (UserServiceImpl) ac.getBean("userService");
// 3、打印bean
System.out.println(userService);
}2.1.4、注意點(diǎn)
此種方式采用的就是通過默認(rèn)構(gòu)造函數(shù)的方式創(chuàng)建Bean,假設(shè)我們給UserServiceImpl添加了一個(gè)帶參的構(gòu)造方法,則運(yùn)行會報(bào)錯(cuò),原因在于當(dāng)我們?yōu)槟硞€(gè)類自定義構(gòu)造方法的時(shí)候,Java編譯器便不會為該類提供默認(rèn)的不帶參數(shù)的構(gòu)造方法了。
2.2、使用工廠中的實(shí)例方法創(chuàng)建方式
2.2.1、定義工廠
// UserService的工廠,作用是創(chuàng)建UserServiceBean對象
public class UserServiceImplFactory {
public UserServiceImpl createUserService(){
return new UserServiceImpl();
}
}2.2.2、定義Bean
public class UserServiceImpl {
}2.2.3、主配置文件中配置Bean
<beans>
<!-- 方式二:使用工廠中提供的實(shí)例方法創(chuàng)建Bean -->
<!-- 第一步:把該工廠定義出來 -->
<bean id="userServiceFactory" class="cn.bdqn.UserServiceImplFactory"/>
<!-- 第二步:定義Bean(通過userServiceFactory中提供的實(shí)例方法)-->
<bean id="userService" factory-bean="userServiceFactory"
factory-method="createUserService"/>
</beans>2.2.4、測試
@Test
public void testUserService() throws Exception{
// 1、讀取主配置文件信息,獲取核心容器對象
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
// 2、從容器中根據(jù)id獲取對象(bean)
UserServiceImpl userService = (UserServiceImpl) ac.getBean("userService");
// 3、打印bean
System.out.println(userService);
}2.3、使用工廠中的靜態(tài)方法創(chuàng)建方式
2.3.1、定義工廠
// UserService的工廠,作用是創(chuàng)建UserServiceBean對象
public class UserServiceImplFactory {
public static UserServiceImpl createUserService(){
return new UserServiceImpl();
}
}2.3.2、定義Bean
public class UserServiceImpl {
}2.3.3、主配置文件中配置Bean
<beans>
<!-- 方式三:使用工廠中提供的靜態(tài)方法創(chuàng)建Bean -->
<!-- 定義Bean(通過工廠類的靜態(tài)方法創(chuàng)建) -->
<bean id="userService" class="cn.bdqn.UserServiceImplFactory"
factory-method="createUserService">
</bean>
</beans>2.3.4、測試
@Test
public void testUserService() throws Exception{
// 1、讀取主配置文件信息,獲取核心容器對象
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
// 2、從容器中根據(jù)id獲取對象(bean)
UserServiceImpl userService = (UserServiceImpl) ac.getBean("userService");
// 3、打印bean
System.out.println(userService);
}三、Bean對象的作用域
3.1、說明
? Spring對Bean的默認(rèn)的作用域(作用范圍)是singleton【單例】
3.2、作用域類型
- singleton:單例的(默認(rèn)值),只會new一次。
- prototype:多例的,用到一次就會new一次。
- request:作用于web應(yīng)用的請求范圍,Spring創(chuàng)建這個(gè)類之后,將這個(gè)類存到request范圍內(nèi)。
- session:應(yīng)用于web項(xiàng)目的會話范圍,Spring創(chuàng)建這個(gè)類之后,將這個(gè)類存到session范圍內(nèi)。
- global-session:作用于集群環(huán)境的會話范圍(全局會話范圍),當(dāng)不是集群環(huán)境時(shí),它就是session。
3.3、注意細(xì)節(jié)
實(shí)際開發(fā)中用得最多的就是singleton和prototype,在整合struts2的時(shí)候使用prototype,在整合SpringMVC的時(shí)候使用singleton。
3.4、如何修改Bean的作用域
bean標(biāo)簽的scope屬性,作用:指定bean的作用范圍。
3.5、測試
3.5.1、測試singleton單例
public class UserServiceImpl {
}public class UserServiceImpl {
}@Test
public void testUserService() throws Exception{
// 1、讀取主配置文件信息,獲取核心容器對象
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
// 2、從容器中根據(jù)id獲取對象(bean)
UserServiceImpl userService1 = (UserServiceImpl) ac.getBean("userService");
UserServiceImpl userService2 = (UserServiceImpl) ac.getBean("userService");
// 3、打印bean
System.out.println(userService1 == userService2); // true
}3.5.2、測試prototype多例
@Test
public void testUserService() throws Exception{
// 1、讀取主配置文件信息,獲取核心容器對象
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
// 2、從容器中根據(jù)id獲取對象(bean)
UserServiceImpl userService1 = (UserServiceImpl) ac.getBean("userService");
UserServiceImpl userService2 = (UserServiceImpl) ac.getBean("userService");
// 3、打印bean
System.out.println(userService1 == userService2); // true
}<bean id="userService" class="cn.bdqn.UserServiceImpl" scope="prototype"/>
@Test
public void testUserService() throws Exception{
// 1、讀取主配置文件信息,獲取核心容器對象
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
// 2、從容器中根據(jù)id獲取對象(bean)
UserServiceImpl userService1 = (UserServiceImpl) ac.getBean("userService");
UserServiceImpl userService2 = (UserServiceImpl) ac.getBean("userService");
// 3、打印bean
System.out.println(userService1 == userService2); // false
}四、Bean對象的生命周期
4.1、單例對象
4.1.1、說明
出生:當(dāng)容器創(chuàng)建時(shí)對象出生
活著:只要容器還在,對象一直活著
死亡:容器銷毀,對象消亡
4.1.2、測試
4.1.2.1、定義Bean
public class UserServiceImpl {
public UserServiceImpl(){
System.out.println("對象的構(gòu)造方法執(zhí)行了");
}
public void init(){
System.out.println("對象初始化了");
}
public void destroy(){
System.out.println("對象銷毀了");
}
}4.1.2.2、主配置文件中配置Bean
<beans>
<bean id="userService" class="cn.bdqn.UserServiceImpl"
scope="singleton" init-method="init" destroy-method="destroy"/>
</beans>4.1.2.3、測試
@Test
public void testUserService() throws Exception{
// 1、讀取主配置文件信息,獲取核心容器對象
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
ac.close();
}
// 結(jié)果:對于單例對象來說,只要容器創(chuàng)建了,那么對象就創(chuàng)建了。類似于立即加載。4.1.2.4、測試結(jié)果
對象的構(gòu)造方法執(zhí)行了
對象初始化了
對象銷毀了
總結(jié):單例對象的生命周期和容器相同
4.2、多例對象
4.2.1、說明
出生:當(dāng)我們使用對象時(shí)spring框架為我們創(chuàng)建
活著:對象只要是在使用過程中就一直活著。
死亡:當(dāng)對象長時(shí)間不用,且沒有別的對象引用時(shí),由Java的垃圾回收器回收
4.2.2、測試
4.2.2.1、定義Bean
public class UserServiceImpl {
public UserServiceImpl(){
System.out.println("對象的構(gòu)造方法執(zhí)行了");
}
public void init(){
System.out.println("對象初始化了");
}
public void destroy(){
System.out.println("對象銷毀了");
}
}4.2.2.2、主配置文件中配置Bean
<beans>
<bean id="userService" class="cn.bdqn.UserServiceImpl"
scope="prototype" init-method="init" destroy-method="destroy"/>
</beans>4.2.2.3、測試1
@Test
public void testUserService() throws Exception{
// 1、讀取主配置文件信息,獲取核心容器對象
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
ac.close();
}
// 結(jié)果:什么都不輸出,說明容器啟動(dòng)的時(shí)候,對于多例對象來說并不會創(chuàng)建4.2.2.4、測試2
@Test
public void testUserService() throws Exception{
// 1、讀取主配置文件信息,獲取核心容器對象
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
UserServiceImpl userService = (UserServiceImpl) ac.getBean("userService");
System.out.println(userService);
ac.close();
}
/**
結(jié)果:
對象的構(gòu)造方法執(zhí)行了
對象初始化了
說明:
對于多例對象來說,只有等到真正使用到該對象的時(shí)候才會創(chuàng)建。類似于懶加載。
**/? 對于多例的Bean,Spring框架是不負(fù)責(zé)管理的
五、總結(jié)


到此這篇關(guān)于Spring中基于xml配置管理Bean的步驟的文章就介紹到這了,更多相關(guān)spring 管理Bean內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)操作系統(tǒng)中的最佳置換Optimal算法
這篇文章主要介紹了java實(shí)現(xiàn)操作系統(tǒng)中的最佳置換Optimal算法 ,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Mybatis使用foreach批量更新數(shù)據(jù)報(bào)無效字符錯(cuò)誤問題
這篇文章主要介紹了Mybatis使用foreach批量更新數(shù)據(jù)報(bào)無效字符錯(cuò)誤問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
學(xué)習(xí)Java之如何對時(shí)間進(jìn)行格式化
當(dāng)我們在默認(rèn)情況下構(gòu)造出來的時(shí)間對象,它的時(shí)間格式并不適合我們閱讀,并且在開發(fā)時(shí),pc端、Android端、iOS端等展示的時(shí)間格式可能也并不完全一樣,本文就從這幾個(gè)問題給大家介紹如何對時(shí)間進(jìn)行格式化,感興趣的同學(xué)可以借鑒一下2023-05-05
java實(shí)現(xiàn)建造者模式(Builder Pattern)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)建造者模式Builder Pattern,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
Java 凍結(jié)或解除凍結(jié)Excel中的行和列的方法
這篇文章主要介紹了Java 凍結(jié)或解除凍結(jié)Excel中的行和列的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03

