Spring constructor injection of SLF4J logger - how to get injection target class?
I want to achieve similar functionality but in Spring boot.
I tried the solution given for this question but it seems postProcessBeanFactory
method of BeanFactoryPostProcessor
never gets invoked in my Spring-Boot application
Below is the implementation of method:
@SuppressWarnings("unused")
public class LoggerBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
String [] beanClasses = beanFactory.getBeanDefinitionNames();
for(String beanName : beanClasses){
Object beanObject = beanFactory.getBean(beanName);
if(beanObject.getClass().isAnnotationPresent(Loggable.class)){
try {
Field loggerField = beanObject.getClass().getDeclaredField("logger");
loggerField.setAccessible(true);
loggerField.set(beanObject, LoggerFactory.getLogger(beanObject.getClass()));
}catch (NoSuchFieldException | IllegalAccessException e){
e.printStackTrace();
}
}
}
}