无法自动连线方法


问题内容

我收到此错误

org.springframework.beans.factory.BeanCreationException: 
Could not autowire method:

这是我的春天的xml配置。

<bean ...>   
...
    <property name="InfoModel" ref="InfoModel"></property>
</bean>

这是我在Java类中自动装配的代码

  private InfoModel infoModel;

  @Autowired
  public void setInfoModel(InfoModel infoModel) {
    this.infoModel= infoModel;
  }

我错过了什么吗?我怀疑,我应该作出InterfaceInfoModel 以使其自动装配?

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)

问题答案:

如果您这样做@Autowired,则无需在xml中将其标记为属性。只需将InfoModel声明为XML中的bean,然后property为您注入的bean从xml中删除InfoModel

加起来

1 您需要在XML中使用bean定义InfoModel

2 您需要property从XML中删除

3 确保通过添加来驱动上下文注释

<context:annotation-config />