亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

springboot如何獲取applicationContext?servletContext

 更新時(shí)間:2023年01月12日 10:36:15   作者:ypp91zr  
這篇文章主要介紹了springboot如何獲取applicationContext?servletContext問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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)文章

最新評(píng)論