无法自动装配方法 [英] Could not autowire method
问题描述
我收到此错误
org.springframework.beans.factory.BeanCreationException:
无法自动装配方法:
这是我的spring的xml配置。
< bean ...>
...
< property name =InfoModelref =InfoModel>< / property>
< / bean>
这是我在Java类中自动装配的代码
私人InfoModel infoModel;
@Autowired
public void setInfoModel(InfoModel infoModel){
this.infoModel = infoModel;
}
我错过了什么。我怀疑我应该制作 InfoModel 的接口
以使其自动装配?
由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到[com.model.InfoModel]类型的匹配bean:预期至少有1个bean有资格作为autowire候选者对于这种依赖。依赖注释:{}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory) .java:789)
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredMethodElement.inject (AutowiredAnnotationBeanPostProcessor.java:547)
如果你这样做 @Autowired
您不需要在xml中将其标记为属性。只需将InfoModel声明为XML格式的bean,并从xml中删除属性
,其中已注入 InfoModel
总结
1 您需要在XML中定义bean定义 InfoModel
2 您需要删除属性
来自XML
3 确保通过添加
<$来驱动您的上下文注释p $ p>
< context:annotation-config />
I am getting this error
org.springframework.beans.factory.BeanCreationException:
Could not autowire method:
This is my spring's xml configuration.
<bean ...>
...
<property name="InfoModel" ref="InfoModel"></property>
</bean>
Here is my code where I am autowiring this in Java class
private InfoModel infoModel;
@Autowired
public void setInfoModel(InfoModel infoModel) {
this.infoModel= infoModel;
}
Am I missing something. I suspect that I should make an Interface
of InfoModel in order to make it autowire?
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.model.InfoModel] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:547)
if you do @Autowired
you don't need to mark it as property in xml. just declare InfoModel as bean in XML and remove property
from xml for you bean where you have injected InfoModel
Summing up
1 You need a bean definition in your XML for InfoModel
2 You need to remove property
from XML
3 Make sure you have made your context annotation driven by adding
<context:annotation-config />
这篇关于无法自动装配方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!