问题描述
我使用带有 Logback
的 Spring Boot 1.5.2
,它是使用 logback-spring.xml
配置的.在那里,我定义了一个自定义类型的 appender(RollingFileAppender
的子类),并希望注入一对 bean.
I'm using Spring Boot 1.5.2
with Logback
, which is configured using a logback-spring.xml
. There, I define an appender of a custom type (subclass of RollingFileAppender
) and would like to get a pair of beans injected.
这可能吗?我天真地尝试注释 appender @Component
等,但由于它是由 Logback/Joran
创建的,所以它当然不起作用.有什么技巧可以应用吗?
Is this possible? I naively tried annotating the appender @Component
etc. but as it is created by Logback/Joran
, it of course doesn't work. Is there a trick I can apply?
如果不可能,那么实现我的目标的规范方法是什么(将 bean 从应用程序上下文插入到 appender 中)?
If not possible, what would be the canonical way of achieving my goal (inserting beans from the application context into an appender)?
推荐答案
无法执行您正在尝试执行的操作.Logback 在创建应用程序上下文之前初始化,因此不需要执行依赖注入.
It isn't possible to do what you are trying to do. Logback is initialised before the application context is created so there's nothing to perform the dependency injection.
也许您可以问另一个问题来描述您希望 appender 能够做什么?可能有一种解决方案不涉及将 Spring 管理的 bean 注入其中.
Perhaps you could ask another question describing what you'd like your appender to be able to do? There may be a solution that doesn't involve injecting Spring-managed beans into it.
这篇关于使用 Spring Boot 将依赖注入到 Logback Appenders的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!