从日志追加器类访问Spring bean [英] Accessing spring bean from logging appender class

查看:137
本文介绍了从日志追加器类访问Spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有log4j DailyRollingFileAppender类,在该类中,我需要检查setFile()方法的数据库值以确定用于记录的文件.

I have log4j DailyRollingFileAppender class in which setFile() method I need to check database value to decide which file to used for logging.

DailyRollingFileAppender class 

public void setFileName()
{
    isLoginEnabled = authenticationManager.checkLoginLogging();
}

"authenticationManager"是用于使用spring依赖项注入功能进行数据库调用的类的对象.

Here 'authenticationManager' is object of class used to make database call using spring dependency injection feature.

spring-beans.xml
<bean id="dailyRollingFileAppender" class="com.common.util.DailyRollingFileAppender">
 <property name="authenticationManager">
     <ref bean="authenticationManager"/>
 </property>
</bean>

<bean id="authenticationManager" class="com.security.impl.AuthenticationManagerImpl">
    <property name="userService">
        <ref bean="userService"/>
</property>
</bean>

现在,当我启动我的应用程序log4j时,它将首先被启动,并且由于尚未调用spring-beans,因此它将在setFileName()方法中引发NullPointerException. 那么有没有办法我可以打电话给'authenticationManager.checkLoginLogging();'从DailyFileAppender类中获取,以便在log4j加载时应该能够获取数据库值?

Now when I start my application log4j gets initiated first and since spring-beans is yet to invoked it throws NullPointerException in method setFileName(). So is there a way I can make call to 'authenticationManager.checkLoginLogging();' from DailyFileAppender class so that when log4j loads it should able to get database value?

推荐答案

几年后,但我希望这对某人有帮助.

A few years late, but I hope this is of help to someone.

我追求类似的功能-我有一个自定义的附加程序,我想使用自动装配的bean使用我们构建的服务来执行一些日志记录.通过使附加器实现ApplicationContextAware接口,并使通常正常自动连线的字段变为静态,我可以将弹簧控制的Bean注入到log4j实例化的附加器实例中.

I was after similar functionality - I have a custom appender, and i wanted to use an autowired bean to perform some logging using a service we'd built. By making the appender implement the ApplicationContextAware interface, and making the field that i'd normally autowire static, i'm able to inject the spring-controlled bean into the instance of the appender that log4j has instantiated.

@Component
public class SslErrorSecurityAppender extends AppenderSkeleton implements ApplicationContextAware {

    private static SecurityLogger securityLogger;

    @Override
    protected void append(LoggingEvent event) {
        securityLogger.log(new SslExceptionSecurityEvent(SecurityEventType.AUTHENTICATION_FAILED, event.getThrowableInformation().getThrowable(), "Unexpected SSL error"));
    }

    @Override
    public boolean requiresLayout() {
        return false;
    }

    @Override
    public synchronized void close() {
        this.closed = true;
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        if (applicationContext.getAutowireCapableBeanFactory().getBean("securityLogger") != null) {
            securityLogger = (SecurityLogger) applicationContext.getAutowireCapableBeanFactory().getBean("securityLogger");
        }
    }
}

这篇关于从日志追加器类访问Spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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