隨着@Configuration
類
public static class Child {}
public static class Processor implements BeanPostProcessor {
@Autowired
public Child child;
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return null; // Spring would complain if this was executed
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return null; // Spring would complain if this was executed
}
}
@Configuration
public static class Config {
@Bean
public static Processor processor() {
return new Processor();
}
@Bean
public Child child() {
return new Child();
}
}
public static void main(String[] args) throws IOException, ParseException, JAXBException, URISyntaxException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
Processor processor = context.getBean(Processor.class);
System.out.println(processor.child);
}
的BeanPostProcessor
不「存在」,但這樣無法處理所創建的其他的bean(這是需要由@Autowired
來完成這個bean)。該javadoc狀態
的ApplicationContexts可以在自己的 bean定義自動檢測BeanPostProcessor的豆子,並將其應用到隨後創建的任何豆。
粗礦。
隨着XML
<context:component-scan base-package="test"></context:component-scan>
<bean id="processor" class="test.Main.Processor"></bean>
<bean id="child" class="test.Main.Child"></bean>
ClassPathXmlApplicationContext xmlContext = new ClassPathXmlApplicationContext("context.xml");
processor = xmlContext.getBean(Processor.class);
System.out.println(processor.child);
另請參閱http://stackoverflow.com/questions/1201726/tracking-down-cause-of-springs-not-eligible-for-auto-proxying/19688634#19688634如果您得到「Bean不符合條件由所有BeanPostProcessors處理「 –