我们如何在 Spring 中记录每个 bean 实例化?
有没有办法。?
你可以使用一个 BeanPostProcessor
@Component
public class LogBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
LOGGER.log(String.format("Bean instantiated with name %s and class %s", beanName, bean.getClass().getSimpleName()));
return bean;
}
}
Run Code Online (Sandbox Code Playgroud)
Dar*_*hta -1
您可以使用 Spring 的事件监听器(此处进行了解释)来监听事件。我相信您需要听的事件是ContextRefreshedEvent,例如:
@Component
public class MyListener
implements ApplicationListener<ContextRefreshedEvent> {
public void onApplicationEvent(ContextRefreshedEvent event) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7104 次 |
| 最近记录: |