springboot如何獲取applicationContext?servletContext
springboot獲取applicationContext servletContext
今天在做一個(gè)quartz定時(shí)任務(wù)的時(shí)候,要獲取servletContext。
想的是獲取到request也可以,但這個(gè)定時(shí)任務(wù)不會(huì)發(fā)起請(qǐng)求,是定時(shí)從數(shù)據(jù)庫(kù)查數(shù)據(jù),所以request不符合場(chǎng)景。
然后就想到了servletContext。
但是在過(guò)程中用了很多種方式都獲取不到。因?yàn)槭窃谄胀?,沒(méi)有controller這種request。
網(wǎng)上的其他方式配置
1.servletContext servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();
這種不行,不知道是不是版本問(wèn)題,目前已失效。
2.在web.xml里面配置
<listener> ? ? ? ? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
然后獲取,也不可行,不需要另行配置xml,因?yàn)槭褂玫膕pring boot
解決辦法
package com.pinyu.system.config; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import org.springframework.util.Assert; /** * @author ypp 創(chuàng)建時(shí)間:2018年10月17日 上午11:59:11 * @Description: TODO(用一句話描述該文件做什么) */ @Component @WebListener public class SpringBeanTool implements ApplicationContextAware, ServletContextListener { /** * 上下文對(duì)象實(shí)例 */ private ApplicationContext applicationContext; private ServletContext servletContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } /** * 獲取applicationContext * * @return */ public ApplicationContext getApplicationContext() { return applicationContext; } /** * 獲取servletContext * * @return */ public ServletContext getServletContext() { return servletContext; } /** * 通過(guò)name獲取 Bean. * * @param name * @return */ public Object getBean(String name) { return getApplicationContext().getBean(name); } /** * 通過(guò)class獲取Bean. * * @param clazz * @param <T> * @return */ public <T> T getBean(Class<T> clazz) { return getApplicationContext().getBean(clazz); } /** * 通過(guò)name,以及Clazz返回指定的Bean * * @param name * @param clazz * @param <T> * @return */ public <T> T getBean(String name, Class<T> clazz) { Assert.hasText(name, "name為空"); return getApplicationContext().getBean(name, clazz); } @Override public void contextInitialized(ServletContextEvent sce) { this.servletContext = sce.getServletContext(); } @Override public void contextDestroyed(ServletContextEvent sce) { } }
這樣其他地方照樣可以直接注入隨時(shí)獲取使用
一個(gè)是實(shí)現(xiàn)了監(jiān)聽(tīng)器在監(jiān)聽(tīng)器初始化的時(shí)候獲取ServletContext ,一個(gè)是spring初始化的時(shí)候獲取ApplicationContext
當(dāng)然也可以只實(shí)現(xiàn)監(jiān)聽(tīng)器就可以的。也可以獲取到ApplicationContext,spring早就為我們寫(xiě)好了,WebApplicationContextUtils.getRequiredWebApplicationContext();
看源碼
獲取到的是WebApplicationContext,WebApplicationContext繼承ApplicationContext
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java計(jì)算方差、標(biāo)準(zhǔn)差(均方差)實(shí)例代碼
在本篇文章里小編給大家分享了關(guān)于java計(jì)算方差、標(biāo)準(zhǔn)差(均方差)實(shí)例代碼以及相關(guān)知識(shí)點(diǎn),需要的朋友們可以參考下。2019-08-08超簡(jiǎn)潔java實(shí)現(xiàn)雙色球若干注隨機(jī)號(hào)碼生成(實(shí)例代碼)
這篇文章主要介紹了超簡(jiǎn)潔java實(shí)現(xiàn)雙色球若干注隨機(jī)號(hào)碼生成(實(shí)例代碼),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04DynamicDataSource怎樣解決多數(shù)據(jù)源的事務(wù)問(wèn)題
這篇文章主要介紹了DynamicDataSource怎樣解決多數(shù)據(jù)源的事務(wù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Java程序員必須知道的5個(gè)JVM命令行標(biāo)志
這篇文章主要介紹了每個(gè)Java程序員必須知道的5個(gè)JVM命令行標(biāo)志,需要的朋友可以參考下2015-03-03Java 圖文并茂講解主方法中的String[] args參數(shù)作用
很多老鐵不清楚JAVA主方法中main()里面的的參數(shù)是什么意思,以及有什么作用,接下來(lái)給大家用最通俗易懂的話來(lái)講解,還不清楚的朋友來(lái)看看吧2022-04-04Spring security如何重寫(xiě)Filter實(shí)現(xiàn)json登錄
這篇文章主要介紹了Spring security 如何重寫(xiě)Filter實(shí)現(xiàn)json登錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09