Spring框架初始化解析
一、Spring能做什么?
Spring的主要目的是使J2EE易用和促進(jìn)好編程習(xí)慣。
倒置控制容器 Spring的設(shè)計(jì)核心是 org.springframework.beans 包, 為與JavaBeans一起工作而設(shè)計(jì)。 這個(gè)包一般不直接被用戶使用, 但作為基礎(chǔ)為更多的其他功能服務(wù). 下一個(gè)較高層面的抽象是"Bean Factory"。 Spring bean factory 是一個(gè)普通的Factory,它使對象能夠按名稱獲取,并且能管理對象之間的關(guān)系。 Bean factories 支持兩種對象模式: . Singleton:在此模式中,有一個(gè)具有特定名稱的共享對象實(shí)例,它在查找時(shí)被獲取。這是默認(rèn)的,而且是最為經(jīng)常使用的。它對于無狀態(tài)對象是一種理想的模式。 .Prototype:在此模式中,每次獲取將創(chuàng)建一個(gè)獨(dú)立的對象。
二、spring啟動加載及實(shí)現(xiàn)方式
第一種:通過注解@PostConstruct 和 @PreDestroy 方法 實(shí)現(xiàn)初始化和銷毀bean之前進(jìn)行的操作
第二種:通過 在xml中定義init-method 和 destory-method方法
第三種:通過bean實(shí)現(xiàn)InitializingBean和 DisposableBean接口
第四種:寫一個(gè)類,實(shí)現(xiàn)BeanPostProcessor接口,這個(gè)接口有兩個(gè)方法。
(1):postProcessBeforeInitialization方法,在spring中定義的bean初始化前調(diào)用這個(gè)方法
(2):postProcessAfterInitialization方法,在spring中定義的bean初始化后調(diào)用這個(gè)方法
或?qū)崿F(xiàn)
InstantiationAwareBeanPostProcessor,是BeanPostProcessor的子接口
Spring 容器加載完成后執(zhí)行
從spring監(jiān)聽器作為入口。
org.springframework.web.context.ContextLoaderListener
找到初始化spring的方法
/** * Initialize the root web application context. */ @Override public void contextInitialized(ServletContextEvent event) { initWebApplicationContext(event.getServletContext()); }
進(jìn)入initWebApplicationContext 方法
if (this.context == null) { this.context = createWebApplicationContext(servletContext); } if (this.context instanceof ConfigurableWebApplicationContext) { ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context; if (!cwac.isActive()) { // The context has not yet been refreshed -> provide services such as // setting the parent context, setting the application context id, etc if (cwac.getParent() == null) { // The context instance was injected without an explicit parent -> // determine parent for root web application context, if any. ApplicationContext parent = loadParentContext(servletContext); cwac.setParent(parent); } configureAndRefreshWebApplicationContext(cwac, servletContext); } }
ApplicationListener
1、編寫一個(gè)實(shí)現(xiàn)ApplicationListener的listener類,
import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Service; @Service public class StartupListenerimplements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { if(event.getApplicationContext().getParent() == null)//root application context 沒有parent,他就是老大. { //需要執(zhí)行的邏輯代碼,當(dāng)spring容器初始化完成后就會執(zhí)行該方法。 System.out.println("\n\n\n\n\n______________\n\n\n加載了\n\n_________\n\n"); } //或者下面這種方式 if(event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext")) { System.out.println("\n\n\n_________\n\n加載一次的 \n\n ________\n\n\n\n"); } } }
2、在配置文件(applicationContext-servlet.xml)中設(shè)置Service掃描的包
<!-- 注冊@Controller 、@Service--> <context:component-scan base-package="com.test.controller" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>
3、部署啟動項(xiàng)目,即可在加載完spring后就打印出“加載”
applicationontext和使用MVC之后的webApplicationontext會兩次調(diào)用上面的方法,如何區(qū)分這個(gè)兩種容器呢?
但是這個(gè)時(shí)候,會存在一個(gè)問題,在web項(xiàng)目中(springmvc),系統(tǒng)會存在兩個(gè)容器,一個(gè)是rootapplicationcontext,另一個(gè)就是我們自己的projectName-servletcontext(作為rootapplicationcontext的子容器)。
這種情況下,就會造成onApplicationEvent方法被執(zhí)行兩次。為了避免上面提到的問題,我們可以只在rootapplicationcontext初始化完成后調(diào)用邏輯代碼,其他的容器的初始化完成,則不做任何處理,修改后代碼
如下:
@Override public void onApplicationEvent(ContextRefreshedEvent event) { if(event.getApplicationContext().getParent() == null){//root application context 沒有parent,他就是老大. //需要執(zhí)行的邏輯代碼,當(dāng)spring容器初始化完成后就會執(zhí)行該方法。 } }
初始化的順序是:
Constructor > @PostConstruct > InitializingBean > init-method
總結(jié)
以上就是本文關(guān)于Spring框架初始化解析的全部內(nèi)容,希望對大家有所幫助。如有問題可以隨時(shí)留言,小編會及時(shí)回復(fù)大家的。感謝朋友們對本站的支持!
相關(guān)文章
java 中如何獲取字節(jié)碼文件的相關(guān)內(nèi)容
這篇文章主要介紹了java 中如何獲取字節(jié)碼文件的相關(guān)內(nèi)容的相關(guān)資料,需要的朋友可以參考下2017-04-04Spring?Boot集成Milvus快速入門demo示例詳解
Milvus是一種高性能向量數(shù)據(jù)庫,支持從筆記本到大型分布式系統(tǒng)的多環(huán)境運(yùn)行,它以開源和云服務(wù)形式提供,是LFAI & Data Foundation的項(xiàng)目,采用Apache 2.0許可,Milvus特別支持高并行化和解耦的系統(tǒng)架構(gòu),使其能夠隨數(shù)據(jù)增長而擴(kuò)展,支持各種復(fù)雜搜索功能,滿足企業(yè)級AI應(yīng)用需求2024-09-09基于maven install 沒反應(yīng)的解決方法
下面小編就為大家?guī)硪黄趍aven install 沒反應(yīng)的解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06idea 無法創(chuàng)建Scala class 選項(xiàng)的原因分析及解決辦法匯總
這篇文章主要介紹了idea 無法創(chuàng)建Scala class 選項(xiàng)的解決辦法匯總,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Idea?編譯并運(yùn)行?Spark?3.1.1?源碼的方法
這篇文章主要介紹了Idea?編譯并運(yùn)行?Spark?3.1.1源碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11Java實(shí)例講解枚舉enum的實(shí)現(xiàn)
枚舉法的本質(zhì)就是從所有候選答案中去搜索正確的解,枚舉算法簡單粗暴,他暴力的枚舉所有可能,盡可能地嘗試所有的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06關(guān)于QueryWrapper,實(shí)現(xiàn)MybatisPlus多表關(guān)聯(lián)查詢方式
這篇文章主要介紹了關(guān)于QueryWrapper,實(shí)現(xiàn)MybatisPlus多表關(guān)聯(lián)查詢方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。2022-01-01