SpringBoot獲取ApplicationContext的3種方式
ApplicationContext是什么?
簡(jiǎn)單來(lái)說(shuō)就是Spring中的容器,可以用來(lái)獲取容器中的各種bean組件,注冊(cè)監(jiān)聽(tīng)事件,加載資源文件等功能。
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新特性但是存在一定的局限性,必須滿(mǎn)足以下兩點(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的子類(lèi),調(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)文章
Spring下Filter過(guò)濾器配置全局異常處理的詳細(xì)步驟
這篇文章主要介紹了Spring下Filter過(guò)濾器配置全局異常處理的詳細(xì)步驟,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Java之不通過(guò)構(gòu)造函數(shù)創(chuàng)建一個(gè)對(duì)象問(wèn)題
這篇文章主要介紹了Java之不通過(guò)構(gòu)造函數(shù)創(chuàng)建一個(gè)對(duì)象問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
java子類(lèi)調(diào)用父類(lèi)的方法中包含子類(lèi)重寫(xiě)的實(shí)例方法
在本篇文章里小編給大家整理了關(guān)于java子類(lèi)調(diào)用父類(lèi)的方法中包含子類(lèi)重寫(xiě)的實(shí)例方法以及相關(guān)知識(shí)點(diǎn),需要的朋友們可以學(xué)習(xí)下。2019-09-09
理解java和python類(lèi)變量以及類(lèi)的成員變量
這篇文章主要幫助大家理解java和python類(lèi)變量以及類(lèi)的成員變量,用實(shí)例進(jìn)行解析,感興趣的朋友可以參考一下2016-02-02
Java基礎(chǔ)類(lèi)庫(kù)之StringBuffer類(lèi)用法詳解
String類(lèi)是在所有開(kāi)發(fā)項(xiàng)目開(kāi)發(fā)之中一定會(huì)使用的一個(gè)功能類(lèi)。雖然String類(lèi)很好用,但也有弊端——內(nèi)容不允許頻繁修改,所以為了解決問(wèn)題,我們提供了StringBuffer類(lèi)。本文就來(lái)講講StringBuffer類(lèi)的用法2022-07-07

