使用 web.xml 中的属性 [英] Using properties in web.xml

查看:44
本文介绍了使用 web.xml 中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想控制 web.xml 中的设置,并针对不同的环境使用不同的一次.

I would like to control the settings in web.xml and using different once for different environments.

是否可以在 web.xml 中使用类路径上的属性文件中的属性?像这样:

Is it possible to use a property, from a property file on classpath, in web.xml? Something like this:

 <context-param>
  <param-name>myparam</param-name>
  <param-value>classpath:mypropertyfile.properties['myproperty']</param-value>
 </context-param>

最好的问候

P

推荐答案

没有.但是,您可以传入属性文件并在运行时从中读取.

No. However you can pass the properties file in and read from it at runtime.

<context-param>
    <param-name>propfile</param-name>
    <param-value>myprop.properties</param-value>
</context-param>

如果您有权访问 servlet,那么在运行时加载属性就很简单了.

It is then trivial to load the property at runtime if you have access to the servlet.

Properties properties = new Properties();
GenericServlet theServlet = ...;
String propertyFileName = theServlet.getInitParameter("propfile");
properties.load(getClass().getClassLoader().getResourceAsStream(propertyFileName));
Object myProperty = properties.get("myProperty");

这篇关于使用 web.xml 中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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