如何从任何方法访问ServletContext实例? [英] How to gain access to a ServletContext instance from any method?
问题描述
有没有办法从方法访问ServletContext而不将ServletContext作为参数传递?
我需要一个可以工作的通用配置类在几个环境中并根据环境加载配置。
例如,在Web应用程序中,我需要从web.xml获取配置或者存储在WEB-INF中的配置文件。
但是为了知道应用程序是否在Web应用程序中运行,我需要以某种方式访问ServletContext。 / p>
任何想法?
有一个包含servlet上下文的类静态字段从 ServletContextListener
初始化它
Is there any way to get access to the ServletContext from a method without passing the ServletContext as an argument?
I need to have a generic Configuration class that can work on several environments and load the config in a way depending on the environment.
For example, when in a web app, i need to get the configuration from either web.xml or a config file stored in WEB-INF.
But in order to know if the app is running in a web app i need to gain access to the ServletContext somehow.
Any ideas?
Have a class that holds servlet context as static field initilize it from ServletContextListener
这篇关于如何从任何方法访问ServletContext实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!