Vraptor3聲稱: java
VRaptor works inside Spring and uses ApplicationContext from your application once available. Then, all the Spring functionalities and modules work with VRaptor without any VRaptor configuration. spring
其實並不是如此,有這樣的問題:
當一個類同時又無參數構造函數及有參數構造函數時,使用Spring的@Autowired會獲得空值,且不會拋出注入失敗的異常。示例代碼以下(來自JForum3裏的代碼): app
public class Category implements Serializable { public Category(){} @Autowired public Category(CategoryRepository repository) { this.repository = repository; } }
這段代碼裏,repository 一直是null。 ide
緣由是: 函數
Vraptor3用br.com.caelum.vraptor.ioc.spring.InjectionBeanPostProcessor覆蓋了Spring中註解織入的方法org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor ui
代碼以下: this
class InjectionBeanPostProcessor extends AutowiredAnnotationBeanPostProcessor { // in case we are required to change the injection annotation: // public InjectionBeanPostProcessor() { // this.setAutowiredAnnotationType(In.class); // } @Override @SuppressWarnings({ "unchecked", "rawtypes" }) public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException { Constructor[] candidates = super.determineCandidateConstructors(beanClass, beanName); if (candidates == null) { Constructor constructor = checkIfThereIsOnlyOneNonDefaultConstructor(beanClass); if (constructor != null) { candidates = new Constructor[]{constructor}; } } return candidates; } @SuppressWarnings({ "rawtypes" }) private Constructor checkIfThereIsOnlyOneNonDefaultConstructor(Class beanClass) { Constructor[] constructors = beanClass.getDeclaredConstructors(); if (constructors.length == 1) { if (constructors[0].getParameterTypes().length > 0) { return constructors[0]; } } return null; } }簡單粗暴的判斷被注入類的構造函數數量,若是有多個構造函數,直接返回null。因此你會發現明明寫了注入,也沒有報錯,但就是沒值。