在Spring管理的Web应用程序内的Log4J Appender中检索Spring管理的bean的可用选项是什么? [英] What are the available options to retrieve Spring-managed beans in a Log4J Appender inside a Spring-managed web application?

查看:182
本文介绍了在Spring管理的Web应用程序内的Log4J Appender中检索Spring管理的bean的可用选项是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的构建负责人在理论上有个好主意-构造一个自定义Log4J附加器,该附加器接收Spring托管的bean,并使用它们将错误记录到标准日志文件之外的其他各种来源。但是,除了创建在启动时使用应用程序上下文(只需一会儿代码)初始化的单例外,我似乎没有想到在Log4J附加程序中检索Spring托管Bean的任何其他选择。

My current build lead has a great idea in theory - construct a custom Log4J appender that takes in Spring-managed beans and uses them to log errors to various other sources than just the standard log file. However, other than creating a singleton initialized at startup with the application context (code in just a moment), I can't seem to think of any other options of retrieving a Spring managed bean in a Log4J appender.

public class SpringSingleton implements ApplicationContextAware {
    private static ApplicationContext context;
    public SpringSingleton() {
        super();
    }
    public static ApplicationContext getContext() {
        return SpringSingleton.context;
    }
    public void setApplicationContext(ApplicationContext context) {
        if(SpringSingleton.context != null) {
            throw new IllegalStateException("Context is already set!");
        }
        SpringSingleton.context = context;
    }
}

理想情况下,可以像设置bean中的bean一样设置这些属性通过依赖注入实现Spring-不管初始化多少个追加程序,bean引用都将永远不会改变。有任何想法吗?

Ideally, these properties could be set just like beans in Spring via dependency injection - the bean references will never change, no matter how many appenders are initialized. Any ideas?

推荐答案

由于log4j必须在之前初始化,因此您将遇到boostrap问题。 >春天。是否使用自定义配置或Log4j的标准初始化程序,它必须在应用程序上下文启动之前启动。

You're going to have a boostrap problem since log4j has to be initialized before Spring. Whether you're using a custom configuration or Log4j's standard initializer, it has to be up before application context is.

现在,您可以在理论上使自定义附加程序懒惰地初始化自己(或者通过您上面建议的方法,或者通过使附加程序本身成为半单身人士-例如,附加程序类具有静态实例字段,该字段由 afterPropertiesSet()方法填充;

Now, you could in theory make your custom appenders "lazily" initialize themselves (either via approach you've suggested above or by making appenders themselves "semi" singletons - e.g. appender class has a static instance field which gets populated by afterPropertiesSet() method; that way you can create appender itself as bean within Spring) but it seems somewhat messy and inconsistent.

另一种方法是在初始化Spring上下文后动态重新配置Log4j。例如编写侦听器进行捕获 ContextStartedEvent ,从上下文中获取所有 Appender 类型的bean,并将它们添加到Log4j配置中。这也将使您可以将附加程序创建为bean,但可以避免出现单例混乱。

Another approach is to dynamically reconfigure Log4j once Spring context is initialized; e.g. write a listener to catch a ContextStartedEvent, obtain all beans of type Appender from the context and add them to Log4j configuration. This will also allow you to create your appenders as beans but avoid singleton mess somewhat.

这篇关于在Spring管理的Web应用程序内的Log4J Appender中检索Spring管理的bean的可用选项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆