Spring中@Inject 如何通过构造器注入Provider
测试代码时报如下错误,这个错误我能看懂,意思是说找不到构造器
但不知道如果解决,因为我想使用@Inject通过构造器注入Provider
我提供了源码,在后面,哪位大侠帮看看呗
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" />