我有log4j DailyRollingFileAppender类,其中setFile()方法我需要检查数据库值以决定用于记录的文件.
DailyRollingFileAppender class
public void setFileName()
{
isLoginEnabled = authenticationManager.checkLoginLogging();
}
Run Code Online (Sandbox Code Playgroud)
这里'authenticationManager'是类的对象,用于使用spring依赖注入功能进行数据库调用.
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>
Run Code Online (Sandbox Code Playgroud)
现在当我启动我的应用程序时,log4j首先被启动,并且因为spring-beans尚未被调用,所以它会在方法setFileName()中抛出NullPointerException.那么我有没有办法调用'authenticationManager.checkLoginLogging();' 来自DailyFileAppender类,以便在log4j加载时它应该能够获取数据库值吗?
小智 8
迟了几年,但我希望这对某人有所帮助.
我有类似的功能 - 我有一个自定义appender,我想使用自动装配的bean使用我们构建的服务执行一些日志记录.通过使appender实现ApplicationContextAware接口,并使我通常自动静态的字段,我能够将弹簧控制的bean注入log4j已实例化的appender的实例中.
@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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2381 次 |
| 最近记录: |