關(guān)于Springboot如何獲取IOC容器
Springboot項(xiàng)目中獲取IOC容器的方式
在Springboot項(xiàng)目中如果要獲取IOC容器目前有兩種方法。
方法一(不實(shí)用,不推薦):
在Springboot項(xiàng)目中都會存在一個SpringApplication的啟動類,我們通過以下代碼啟動IOC容器。
SpringApplication.run(Application.class, args);
其實(shí)run方法會將創(chuàng)建的IOC容器作為返回值返回,那么我們就可以通過聲明一個ApplicationContext對象來接收run方法的返回值。
public class SpringApplication { public static void main(String[] args) { ApplicationContext applicationContext = SpringApplication.run(Application.class, args); Object startSerive = applicationContext.getBean("startSerive"); } }
但是,使用這種方法會遇到各種各樣的問題,所以我們通常使用第二種方法。
方法二(強(qiáng)烈推薦):
通過編寫實(shí)現(xiàn)了ApplicationContextAware
的工具類來獲取IOC容器,當(dāng)實(shí)現(xiàn)了ApplicationContextAware
的類在容器中被初始化和加載后,會自動調(diào)用ApplicationContextAware
中的setApplicationContext
方法,將IOC容器傳入setApplicationContext
方法的形參中。
以下是用于獲取IOC容器的工具類:
public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; public SpringContextUtil() { } /** * 設(shè)置上下文 * @param applicationContext * @throws BeansException */ @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if (SpringContextUtil.applicationContext == null) { SpringContextUtil.applicationContext = applicationContext; } } /** * 獲取上下文 * @return */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * 通過名字獲取上下文中的bean * @param name * @return */ public static Object getBean(String name){ return applicationContext.getBean(name); } /** * 通過類型獲取上下文中的bean * @param requiredType * @return */ public static Object getBean(Class<?> requiredType){ return applicationContext.getBean(requiredType); } }
上面這個工具類只有在被IOC容器加載完之后才會調(diào)用setApplicationContext,那么該怎么把工具類放到IOC容器中呢?我們使用@Import注解來實(shí)現(xiàn),具體使用方法請看下面代碼:
@SpringBootApplication @Import({SpringContextUtil.class}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
注:不使用@Import也是可以的,例如在SpringContextUtil類上面標(biāo)注@Component等類似的注解也是可以的。
@Import注解須知:
1.@Import只能用在類上 ,@Import通過快速導(dǎo)入的方式實(shí)現(xiàn)把實(shí)例加入spring的IOC容器中
2.加入IOC容器的方式有很多種,@Import注解可以用于導(dǎo)入第三方包 ,當(dāng)然@Bean注解也可以,但是@Import注解快速導(dǎo)入的方式更加便捷
總結(jié)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
SpringBoot Controller返回圖片的三種方式
在互聯(lián)網(wǎng)的世界里,圖片無處不在,它們是信息傳遞的重要媒介,也是視覺盛宴的一部分,而在Spring Boot項(xiàng)目中,如何優(yōu)雅地處理和返回圖片數(shù)據(jù),則成為了開發(fā)者們不得不面對的問題,今天,就讓我們一起來探索Spring Boot Controller的神奇轉(zhuǎn)換,需要的朋友可以參考下2024-07-07SpringBoot使用ip2region獲取地理位置信息的方法
這篇文章主要介紹了SpringBoot使用ip2region獲取地理位置信息的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06關(guān)于logback.xml和logback-spring.xml的區(qū)別及說明
這篇文章主要介紹了關(guān)于logback.xml和logback-spring.xml的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06BigDecimal的toString()、toPlainString()和toEngineeringString()區(qū)
使用BigDecimal進(jìn)行打印的時候,經(jīng)常會對BigDecimal提供的三個toString方法感到好奇,以下整理3個toString方法的區(qū)別及用法,需要的朋友可以參考下2023-08-08Mybatis的sql語句執(zhí)行異常后打印到日志問題
文章介紹了一種Mybatis異常日志打印方案,主要通過Mybatis攔截器獲取執(zhí)行的sql語句,并利用ThreadLocal存儲,以避免多線程下的sql語句覆蓋問題,當(dāng)異常發(fā)生時,從ThreadLocal中取出sql語句并打印到單獨(dú)的日志文件中,方便數(shù)據(jù)恢復(fù),該方案經(jīng)過壓力測試2024-10-10關(guān)于springboot的接口返回值統(tǒng)一標(biāo)準(zhǔn)格式
這篇文章主要介紹了關(guān)于springboot的接口返回值統(tǒng)一標(biāo)準(zhǔn)格式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05