类路径:和 Spring,日志 4j
各位, 我在尝试从 spring 初始化 log4j 配置时遇到问题。
<bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod">
<value>org.springframework.util.Log4jConfigurer.initLogging</value>
</property>
<property name="arguments">
<list>
<value>classpath:/resources/META-INF/spring/log4j.xml</value>
</list>
</property>
</bean>
java.io.FileNotFoundException:类路径资源[/resources/META-INF/spring/log4j.xml]无法解析为URL,因为它不存在
当我尝试放置断点并观察函数的值位置时
public static void initLogging(java.lang.String location) throws java.io.FileNotFoundException { /* 编译后的代码 */ }
然后我看到 classpath: token 尚未被替换。
您有什么建议吗?
非常感谢
Dear All,
I have a problem when trying to initialize my log4j configuration from spring.
<bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod">
<value>org.springframework.util.Log4jConfigurer.initLogging</value>
</property>
<property name="arguments">
<list>
<value>classpath:/resources/META-INF/spring/log4j.xml</value>
</list>
</property>
</bean>
java.io.FileNotFoundException: class path resource [/resources/META-INF/spring/log4j.xml] cannot be resolved to URL because it does not exist
When I try to put a breakpoint and watch the value Location of function
public static void initLogging(java.lang.String location) throws java.io.FileNotFoundException { /* compiled code */ }
then I see that the classpath: token has not been replaced.
Do you have any suggestion?
Thank you very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在使用 maven。但是maven会将
src
和resources
文件夹“混搭”为一个。因此,如果是 Maven 项目,路径将为:classpath:META-INF/spring/log4j.xml
在
classpath:
后不带斜杠如果您在服务器上工作,则应该在 Spring 启动之前初始化日志记录框架,因为 Spring 本身将在启动时使用它来记录信息。
一种方法是在 web.xml 中添加此行(而不需要在 spring 配置中执行此操作)
您真的确定吗?魔法是在这一行中完成的:
URL url = ResourceUtils.getURL(resolvedLocation);
所以
resolvedLocation
是你的字符串“classpath:META-INF...”,但返回的 URL 是“翻译后的”路径。请注意,当资源位置以
classpath:
开头且未找到文件时,getURL
函数会抛出异常。所以我会测试如果我指定一个不存在的文件会发生什么。 ->如果没有发生异常,则classpath:
中存在拼写错误。Looks like you are using maven. But maven will "mash" the
src
andresources
folder to one. So in case of an Maven project, the path would be:classpath:META-INF/spring/log4j.xml
without slash after
classpath:
If you work on a server you should initialize the logging framework before Spring starts, because Spring itself will use it to log information while starting.
One way to do so, is to add this lines in the web.xml (than you do not need to do this in the spring configuration)
Are you really sure? The magic is done in the line:
URL url = ResourceUtils.getURL(resolvedLocation);
So
resolvedLocation
is your string "classpath:META-INF..." but the returned URL is the "translated" path.Pay attention to the fact, that the
getURL
function throw an exception when the ressource location starts withclasspath:
and the file is not found. So I would test what happen if I specify a not existing file. -> If no exception occurs, then there is a spelling error inclasspath:
.