SpringBoot獲取ApplicationContext的3種方式
ApplicationContext是什么?
簡(jiǎn)單來(lái)說(shuō)就是Spring中的容器,可以用來(lái)獲取容器中的各種bean組件,注冊(cè)監(jiān)聽事件,加載資源文件等功能。
Application Context獲取的幾種方式
1 直接使用Autowired注入
@Component public class Book1 { @Autowired private ApplicationContext applicationContext; public void show (){ System.out.println(applicationContext.getClass()); } }
2 利用 spring4.3 的新特性
使用spring4.3新特性但是存在一定的局限性,必須滿足以下兩點(diǎn):
1) 構(gòu)造函數(shù)只能有一個(gè),如果有多個(gè),就必須有一個(gè)無(wú)參數(shù)的構(gòu)造函數(shù),此時(shí),spring會(huì)調(diào)用無(wú)參的構(gòu)造函數(shù)
2) 構(gòu)造函數(shù)的參數(shù),必須在spring容器中存在
@Component public class Book2 { private ApplicationContext applicationContext; public Book2(ApplicationContext applicationContext){ System.out.println(applicationContext.getClass()); this.applicationContext=applicationContext; } public void show (){ System.out.println(applicationContext.getClass()); } }
3 實(shí)現(xiàn)spring提供的接口 ApplicationContextAware
spring 在bean 初始化后會(huì)判斷是不是ApplicationContextAware的子類,調(diào)用setApplicationContext()方法, 會(huì)將容器中ApplicationContext傳入進(jìn)去
@Component public class Book3 implements ApplicationContextAware { private ApplicationContext applicationContext; public void show (){ System.out.println(applicationContext.getClass()); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }
結(jié)果獲取三次:
class org.springframework.context.annotation.AnnotationConfigApplicationContext class org.springframework.context.annotation.AnnotationConfigApplicationContext class org.springframework.context.annotation.AnnotationConfigApplicationContext
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot如何獲取applicationContext?servletContext
- SpringBoot?容器刷新前回調(diào)ApplicationContextInitializer
- SpringBoot?ApplicationContext接口深入分析
- SpringBoot如何使用applicationContext.xml配置文件
- Springboot如何獲取上下文ApplicationContext
- springboot ApplicationContextInitializer的三種使用方法小結(jié)
- SpringBoot如何使用ApplicationContext獲取bean對(duì)象
- SpringBoot ApplicationContextAware拓展接口使用詳解
相關(guān)文章
springboot實(shí)現(xiàn)添加郵件發(fā)送及壓縮功能
這篇文章主要介紹了springboot實(shí)現(xiàn)添加郵件發(fā)送及壓縮功能 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07詳解idea從git上拉取maven項(xiàng)目詳細(xì)步驟
這篇文章主要介紹了詳解idea從git上拉取maven項(xiàng)目詳細(xì)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08springBoot前后端分離項(xiàng)目中shiro的302跳轉(zhuǎn)問題
這篇文章主要介紹了springBoot前后端分離項(xiàng)目中shiro的302跳轉(zhuǎn)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12SpringBoot+Mybatis實(shí)現(xiàn)Mapper接口與Sql綁定幾種姿勢(shì)
通常我們?cè)谑褂肕ybatis進(jìn)行開發(fā)時(shí),會(huì)選擇xml文件來(lái)寫對(duì)應(yīng)的sql,然后將Mapper接口與sql的xml文件建立綁定關(guān)系,然后在項(xiàng)目中調(diào)用mapper接口就可以執(zhí)行對(duì)應(yīng)的sql,感興趣的可以學(xué)習(xí)一下2021-09-09Spring?boot?運(yùn)用策略模式實(shí)現(xiàn)避免多次使用if的操作代碼
這篇文章主要介紹了Spring?boot?運(yùn)用策略模式實(shí)現(xiàn),避免多次使用if,使用策略模式后,新加一種支付策略時(shí),只需要在策略枚舉中添加新加的策略信息,外加一個(gè)策略類即可,而不再需要添加新的if判斷,需要的朋友可以參考下2022-08-08