如何在BeanDefinitionRegistryPostProcessor(BDRPP)内访问ApplicationContext?以下是我的BDRPP。
尝试添加 @Autowired 注解,甚至让我的 CustomBDRPP 实现了 ApplicationContextAware 接口,但是 ApplicationContext 没有被注入或初始化。
这怎么能实现呢?
public class MyCustomBeansFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
// Need to access ApplicationContext here
System.out.println("Got Application Context: " + applicationContext);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
}
}
尝试添加 @Autowired 注解,甚至让我的 CustomBDRPP 实现了 ApplicationContextAware 接口,但是 ApplicationContext 没有被注入或初始化。
public class MyCustomBeansFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware {
//@Autowired
private ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext context) {
applicationContext = context;
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
System.out.println("Got Application Context: " + applicationContext);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
}
}
输出:
Got Application Context: null
这怎么能实现呢?
postProcessBeanDefinitionRegistry
方法,而不是我。 - Neo