控制servlet中的类路径 [英] Controlling the classpath in a servlet

查看:113
本文介绍了控制servlet中的类路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的servlet应用程序包含许多库.jars,其中一些包含嵌入的log4j.xml或log4j.properties文件。我想确保log4j首先找到我的log4j.xml!我已经尝试在servlet中搜索各种类路径元素的优先级的一些规范(例如,WEB-INF / classes总是在WEB-INF / lib之前吗?),或者某种方式来配置或调整servlet的类加载器以便给定的资源目录出现在类路径的早期。到目前为止,我已经画了一个空白。有关确保servlet .war文件通过类加载器加载正确的log4j.xml的任何建议吗?

解决方案

据我所知类路径中的资源选择是非确定性的(从应用程序开发人员的角度来看)。即使一致地加载相同的文件,行为也可能会改变:
1.升级当前容器的版本时。
2.如果你切换容器。



最简单的解决方案是从库罐中删除嵌入式log4j配置文件。嵌入log4j配置几乎绝不是一个好主意,因为它会导致你在这里看到的问题...



它们是你开发的第三方罐子还是罐子? / p>

My servlet application includes a number of library .jars, some of which contain embedded log4j.xml or log4j.properties files. I'd like to ensure that log4j finds my log4j.xml first! I've tried searching for some specification of the priorities of the various classpath elements in a servlet (e.g. does WEB-INF/classes always precede WEB-INF/lib?), or some way to configure or tweak the servlet's classloader so that a given resource directory appears early in the classpath. So far, I've drawn a blank. Any suggestions on ensuring that a servlet .war file loads the correct log4j.xml via the classloader?

解决方案

As far as I understand the resource selection from the classpath is non-deterministic (from the point of view of the app developer). Even if the same file is loaded consistently the behaviour could change: 1. When you upgrade the version of your current container. 2. If you switch containers.

The simplest solution will be to remove embedded log4j config files from library jars. It is almost never a good idea to embed log4j config's as it leads to the problem you are seeing here...

Are they third party jars or jars you developed?

这篇关于控制servlet中的类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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