我可以自动装配命名记录器吗?

时间:2013-05-09 14:41:34

标签: java spring logging autowired

我正在使用根据类名命名的非静态记录器:

protected Logger logger = LoggerFactory.getLogger(getClass());

我可以用某种方式配置弹簧,使用@Autowired设置正确的记录器吗?

@Autowired
protected Logger logger;

我可以使用factory-method进行记录器初始化,但我不知道如何将类名作为参数传递。对于基于setter的依赖注入,spring必须知道类名,因为它包含对bean的引用。我可以以某种方式访问​​它吗?还有另一种方式吗?

4 个答案:

答案 0 :(得分:4)

为了使Logger可以注入@Autowired,您必须拥有一个配置类,您可以在其中配置使用@Autowired的所有Bean。该课程将标有@Configuration。在那里,您必须在配置中添加以下@Bean

@Configuration
public class WebConfiguration {

    @Bean
    @Scope("prototype")
    public Logger produceLogger(InjectionPoint injectionPoint) {
        Class<?> classOnWired = injectionPoint.getMember().getDeclaringClass();
        return LoggerFactory.getLogger(classOnWired);
    }
}

答案 1 :(得分:0)

您可以使用@Qualifier注释来执行此操作。当然,这意味着您已经在应用程序上下文中添加了Logger个对象。

将此配置导入您的应用程序上下文将允许您这样做:

@Configuration
public class LoggerConfig {
    @Bean
    public Logger myClassLogger() {
        return LoggerFactory.getLogger(MyClass.class);
    }

    @Bean
    public Logger myOtherClassLogger() {
        return LoggerFactory.getLogger(MyOtherClass.class);
    }
}

然后在使用Logger

的课程中
@Component
public class MyClass {
    @Autowired
    @Qualifier("myClassLogger")
    private Logger logger;

    //...
}

@Component
public class MyOtherClass {
    @Autowired
    @Qualifier("myOtherClassLogger")
    private Logger logger;

    //...
}

答案 2 :(得分:0)

您可以使用@Inject和BeanFactoryPostProcessor注入它

@Inject
Logger logger;

您可以在此处找到更多详细信息:Using java annotation to inject logger dependency

答案 3 :(得分:0)

按如下所述使用自动连接的记录器:

package de.senatov.wflow.config;

import org.slf4j.Logger;

@Configuration
public class WebFlowConfig extends AbstractFacesFlowConfiguration {


    @Autowired
    private Logger log;



    @Bean
    public FlowDefinitionRegistry flowRegistry() {

        log.debug("flowRegistry()");
        return getFlowDefinitionRegistryBuilder(flowBuilderServices()).addFlowLocation("/WEB-INF/flows/booking/booking-flow.xml", "booking")
                                                                      .addFlowLocation("/WEB-INF/flows/main/main-flow.xml", "main").build();
    }
.....
.... 

1)插入小类:

package de.senatov.wflow.loggable;


import org.slf4j.Logger;
import org.springframework.beans.factory.InjectionPoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import static java.util.Optional.of;
import static org.slf4j.LoggerFactory.getLogger;

@Configuration
public class LoggingConfiguration {
    @Bean
    @Scope("prototype")
    public Logger logger(InjectionPoint ip) {

        try {
            return getLogger(of(ip.getMember())
                                     .map(o -> o.getDeclaringClass())
                                     .orElseThrow(IllegalArgumentException::new));
        }
        catch (Exception e) {
            System.err.printf("slf4j autowired Exception occured : %s%n", e.getMessage());
            throw e;
        }
    }
}
相关问题