我想使用@Inject通过构造器注入一个Provider
在测试代码时报如下错误,这个错误我能看懂,意思是说找不到构造器
但一开始不知道如何解决,就在网上一顿搜啊,全是理论,没一个实践案例
网上没找到解决方案怎么办,看书啊!
最后发现自己犯了一个低级错误:Spring容器默认是禁用注解装配的。如果要使用基于注解的自动装配,就必须在配置文件中通过<context:annotation-config />来启用!
所以在配置文件中加入<context:annotation-config />后,问题解决!
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tom' defined in class path resource [com/springinaction/provider/spring-provider.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.springinaction.provider.KnifeJuggler]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.springinaction.provider.KnifeJuggler.<init>()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.springinaction.common.BaseBeanFactory.getBean(BaseBeanFactory.java:11)
at com.springinaction.provider.KnifeJugglerMain.main(KnifeJugglerMain.java:13)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.springinaction.provider.KnifeJuggler]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.springinaction.provider.KnifeJuggler.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:70)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:958)
... 14 more
Caused by: java.lang.NoSuchMethodException: com.springinaction.provider.KnifeJuggler.<init>()
at java.lang.Class.getConstructor0(Class.java:2706)
at java.lang.Class.getDeclaredConstructor(Class.java:1985)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:65)
... 15 more
java代码
private Set<Knife> knives;
@Inject
public KnifeJuggler(Provider<Knife> KnifeProvider) {
knives = new HashSet<Knife>();
for (int i = 0; i < 5; i++) {
knives.add(KnifeProvider.get());
}
}
Spring配置文件如下
<bean id="tom" class="com.springinaction.provider.KnifeJuggler" />