如何从任何方法访问ServletContext实例? [英] How to gain access to a ServletContext instance from any method?

查看:129
本文介绍了如何从任何方法访问ServletContext实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从方法访问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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆