在 Spring 中,如何配置 java.util.Logging 以便它可以自动装配?
在我们的 web 应用程序中,我们使用 java.util.Logging(实际上是 JULI,因为我们部署到 Tomcat 6)。日志记录由 WEB-INF/classes 中的logging.properties 文件配置,例如 this 。
我想配置记录器,以便它可以自动连接,例如:
@Autowired
private Logger mylogger;
我搜索了 Spring 论坛、网络,当然还有 Stack Overflow,但我找不到如何设置它。我将不胜感激任何帮助。
谢谢!
In our webapp we're using java.util.Logging (JULI, actually, since we deploy to Tomcat 6). Logging is configured by a logging.properties file in WEB-INF/classes, a la this.
I'd like to configure the logger so it can be autowired, something like:
@Autowired
private Logger mylogger;
I've searched the Spring forums, the web, and of course Stack Overflow and I can't find how to set this up. I'd appreciate any help on this.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种方法是使用 Java 配置风格,因此您将拥有一个像这样的 bean:
然后可以像平常一样将其自动连接到应用程序的其余部分。
One way would be to use the Java Config style, so you'd have one bean like this:
That could then be autowired into the rest of the app as normal.
为了在某些东西(bean)上使用@Autowired,您必须使该bean受弹簧控制。有很多方法可以做到这一点,它们取决于您想要使用的日志框架。
恐怕没有一个“一刀切”的解决方案。
通常,您将使用您选择的日志框架提供的静态初始化程序或对其进行一些抽象(例如commons-logging)。
我找到了一个引用,其中引入了 @Logger 注释,也许这会为您指明您喜欢的方向:
http://jgeeks.blogspot.com/2008/10/auto-injection-of-logger-into-spring.html
In order to use @Autowired on something (a bean) you must make that bean spring-controlled. There are many ways to do this and they depend on the logging framework you want to use.
I'm afraid there isn't a 'one-size-fits-all' solution.
Usually, you would use a static initializer provided by the logging framework of your choice or some abstraction over it (e.g. commons-logging).
I found one reference in which a @Logger annotation is introduced, maybe that points you into a direction of your liking:
http://jgeeks.blogspot.com/2008/10/auto-injection-of-logger-into-spring.html
为了使 Logger 可以使用
@Autowired
进行注入,您必须有一个配置类,在其中配置了使用@Autowired
的所有 Bean。该类将用@Configuration
标记。您必须在配置中添加以下@Bean
:In order to make Logger be injectable with
@Autowired
, you must have a configuration class where you have configured all the Beans with which you use@Autowired
. That class will be marked with@Configuration
. There you must put the following@Bean
in your configuration: