我正在尝试在Spring Java Config中添加BeanPostProcessor。对于通过组件扫描创建的bean(@Configuration,@ RestController,..),似乎只有但只有
我在Java配置中创建的Bean不是。
E.g。
@Configuration
public class MyConfiguration
{
@Bean
public MyBean myBean()
{
return new MyBean();
}
@Bean
public static MyBPP myBeanPostProcessor()
{
return new MyBPP();
}
}
注意我确实将BeanPostProcessor的方法声明为静态(参见http://forum.spring.io/forum/spring-projects/container/123899-beanpostprocessor-with-bean-annotation-not-working)。
因此创建了一个MyBean实例,但BeanPostProcessor从不处理它。
答案 0 :(得分:4)
尝试我的测试,它对我有用
class MyBPP implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) {
System.out.println(bean);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
return bean;
}
}
@Configuration
public class MyConfiguration {
@Bean
public MyBean myBean() {
return new MyBean();
}
@Bean
public MyBPP myBeanPostProcessor() {
return new MyBPP();
}
public static void main(String[] args) {
new AnnotationConfigApplicationContext(MyConfiguration.class);
}
}