Spring中@Inject 如何通过构造器注入Provider

bing_lang 2013-10-15 10:05:56
测试代码时报如下错误,这个错误我能看懂,意思是说找不到构造器
但不知道如果解决,因为我想使用@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" />


...全文
84 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧