springboot如何獲取applicationContext?servletContext
springboot獲取applicationContext servletContext
今天在做一個quartz定時任務的時候,要獲取servletContext。
想的是獲取到request也可以,但這個定時任務不會發(fā)起請求,是定時從數據庫查數據,所以request不符合場景。
然后就想到了servletContext。
但是在過程中用了很多種方式都獲取不到。因為是在普通類,沒有controller這種request。
網上的其他方式配置
1.servletContext servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();
這種不行,不知道是不是版本問題,目前已失效。
2.在web.xml里面配置
<listener> ? ? ? ? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
然后獲取,也不可行,不需要另行配置xml,因為使用的spring 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)建時間:2018年10月17日 上午11:59:11 * @Description: TODO(用一句話描述該文件做什么) */ @Component @WebListener public class SpringBeanTool implements ApplicationContextAware, ServletContextListener { /** * 上下文對象實例 */ 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; } /** * 通過name獲取 Bean. * * @param name * @return */ public Object getBean(String name) { return getApplicationContext().getBean(name); } /** * 通過class獲取Bean. * * @param clazz * @param <T> * @return */ public <T> T getBean(Class<T> clazz) { return getApplicationContext().getBean(clazz); } /** * 通過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) { } }
這樣其他地方照樣可以直接注入隨時獲取使用
一個是實現了監(jiān)聽器在監(jiān)聽器初始化的時候獲取ServletContext ,一個是spring初始化的時候獲取ApplicationContext
當然也可以只實現監(jiān)聽器就可以的。也可以獲取到ApplicationContext,spring早就為我們寫好了,WebApplicationContextUtils.getRequiredWebApplicationContext();
看源碼
獲取到的是WebApplicationContext,WebApplicationContext繼承ApplicationContext
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
DynamicDataSource怎樣解決多數據源的事務問題
這篇文章主要介紹了DynamicDataSource怎樣解決多數據源的事務問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07Java 圖文并茂講解主方法中的String[] args參數作用
很多老鐵不清楚JAVA主方法中main()里面的的參數是什么意思,以及有什么作用,接下來給大家用最通俗易懂的話來講解,還不清楚的朋友來看看吧2022-04-04Spring security如何重寫Filter實現json登錄
這篇文章主要介紹了Spring security 如何重寫Filter實現json登錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09