简体   繁体   English

Spring XML-自动连接的Logger不能确定目标类名称

[英]Spring XML - autowired Logger doesn't determine target class name

I've added log4j Logger into Spring xml configuration to use it later as a component: 我已将log4j Logger添加到Spring xml配置中,以在以后用作组件:

<bean id="logger" class="org.apache.log4j.Logger" factory-method="getLogger">
    <constructor-arg type="java.lang.String" value="LoggerName"/>
</bean>

seems everything works fine, but logs are don't point to the target classes where they're have been autowired: 似乎一切正常,但日志未指向已自动接线的目标类:

2016-06-30 09:54:23 DEBUG LoggerName:29 - Account does not exists

How to setup the Logger bean properly to let it determine the target class names like this: 如何正确设置Logger bean,以使其确定目标类名称,如下所示:

2016-06-30 09:54:23 DEBUG AccountValidator:29 - Account does not exists

where AccountValidator is actual class that the Logger has been autowired into? AccountValidatorAccountValidator是Logger自动连接到的实际类?

In my opinion you should instanciate a logger per class : 我认为您应该为每个班级实例化一个记录器:

private static final Logger logger = Logger.getLogger(AccountValidator.class);

If you absolutely need to do it with injection you can use CDI's injection point mechanism. 如果您绝对需要通过注入来执行此操作,则可以使用CDI的注入点机制。 This post is about what you want : 这篇文章是关于你想要的:

What is the Spring DI equivalent of CDI's InjectionPoint? Spring DI等同于CDI的InjectionPoint?

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

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