Spring constructor injection of SLF4J logger - how to get injection target class?

后端 未结 8 2044
太阳男子
太阳男子 2020-12-25 15:20

I\'m trying to use Spring to inject a SLF4J logger into a class like so:

@Component
public class Example {

  private final Logger logger;

  @Autowired
  pu         


        
8条回答
  •  北荒
    北荒 (楼主)
    2020-12-25 15:56

    To make your code more Spring aware use the InjectionPoint to define the loggers, i.e.:

    @Bean
    @Scope("prototype")
    public Logger logger(InjectionPoint ip) {
        return Logger.getLogger(ip.getMember().getDeclaringClass());
    }
    

    @Scope("prototype") is needed here to create 'logger' bean instance every time method is called.

提交回复
热议问题