类路径:和 Spring,日志 4j

发布于 2024-10-31 23:20:52 字数 909 浏览 0 评论 0原文

各位, 我在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2024-11-07 23:20:52

类路径:/资源/META-INF/spring/log4j.xml

看起来您正在使用 maven。但是maven会将srcresources文件夹“混搭”为一个。因此,如果是 Maven 项目,路径将为: classpath:META-INF/spring/log4j.xml

classpath: 后不带斜杠


如果您在服务器上工作,则应该在 Spring 启动之前初始化日志记录框架,因为 Spring 本身将在启动时使用它来记录信息。

一种方法是在 web.xml 中添加此行(而不需要在 spring 配置中执行此操作)

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/classes/META-INF/log4j.xml</param-value>
</context-param>    
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

当我尝试放置断点并观察函数的值位置时

public static void initLogging(java.lang.String location) throws java.io.FileNotFoundException { /* compiled code */ }

您真的确定吗?魔法是在这一行中完成的:
URL url = ResourceUtils.getURL(resolvedLocation);
所以 resolvedLocation 是你的字符串“classpath:META-INF...”,但返回的 URL 是“翻译后的”路径。

请注意,当资源位置以 classpath: 开头且未找到文件时,getURL 函数会抛出异常。所以我会测试如果我指定一个不存在的文件会发生什么。 ->如果没有发生异常,则 classpath: 中存在拼写错误。

classpath:/resources/META-INF/spring/log4j.xml

Looks like you are using maven. But maven will "mash" the src and resources 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)

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/classes/META-INF/log4j.xml</param-value>
</context-param>    
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

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 */ }

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 with classpath: 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 in classpath:.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文