Wednesday 13 April 2016

Autowire Logger Object In Spring

You could do it with a @Qualifier annotation. Of course that means that you have added Loggerobjects to your application context already.
Importing this config into your application context would allow you to do that:
@Configuration
public class LoggerConfig {
    @Bean
    public Logger myClassLogger() {
        return LoggerFactory.getLogger(MyClass.class);
    }

    @Bean
    public Logger myOtherClassLogger() {
        return LoggerFactory.getLogger(MyOtherClass.class);
    }
}
And then in your classes that uses the Logger:
@Component
public class MyClass {
    @Autowired
    @Qualifier("myClassLogger")
    private Logger logger;

    //...
}

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

    //...
}

No comments: