简体   繁体   English

非servlet从web.xml获取属性

[英]Non-servlets obtaining properties from web.xml

Are there any drawbacks to non-servlets java classes obtaining property values from web.xml? 非servlet Java类从web.xml获取属性值是否有缺点?

There are not many examples online; 网上没有很多例子; I was wondering if this was an advisable path? 我想知道这是否是一条明智的道路?

It's just an unnecessary dependency. 这只是不必要的依赖。 It's easier to get property values from a properties file in the classpath and it doesn't require the code to run in a servlet container. 从类路径中的属性文件中获取属性值更加容易,并且不需要代码在servlet容器中运行。

Reading the properties from web.xml will require your classes to access a ServletConfig , so your classes will all be dependent on servlets. 从web.xml读取属性将需要您的类访问ServletConfig ,因此您的所有类都将依赖于servlet。 You will be a lot better off with a properties file. 使用属性文件会使您更好。

One pattern for non-servlet classes to lookup something already defined in web.xml is through the use of JNDI properties(For instance, some shared/cluster level attribute that defines whether this is a Dev/Test/Production environment). 非servlet类用于查找web.xml中已定义内容的一种模式是通过使用JNDI属性(例如,某些共享/群集级别属性来定义这是否是Dev / Test / Production环境)。

The use cases are more granular and different from the ones defined in properties file. 用例更加细化,与属性文件中定义的用例不同。 For vast majority of usecases, looking up from a property file is sufficient. 对于绝大多数用例,从属性文件中查找就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM