无法自动assembly方法
我收到了这个错误
org.springframework.beans.factory.BeanCreationException: Could not autowire method:
这是我的spring的xml配置。
...
这是我的代码,我在Java类中自动assembly它
private InfoModel infoModel; @Autowired public void setInfoModel(InfoModel infoModel) { this.infoModel= infoModel; }
我错过了什么。 我怀疑我应该创建一个InfoModel Interface
才能使其自动assembly?
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,并从已注入InfoModel
bean中删除xml中的property
加起来
1您需要在XML中为InfoModel
定义bean
2您需要从XML中删除property
3确保通过添加驱动上下文注释
如果堆栈跟踪说没有所述类型的匹配bean,那就错了。
将InfoModel bean添加到spring应用程序上下文中,例如通过在相同的xml配置中声明bean:
顺便说一句。 你不应该大写bean标识符的第一个字母,遵循与变量相同的命名约定,即。 lowerCamelCase。 自动assembly和显式注入依赖关系也是多余的。
我想你应该在私人信息之前写@Autowired ….宣言。 并从jigar中提到的xml中删除条目。
您是否使用类型级别注释(如@Component
InfoModel
, @Service
@Repository
或InfoModel
正确注释了InfoModel
类。 所有这些都非常相似。 如果您的InfoModel
没有正确注释,您将收到这个着名的“No matching bean …”错误。 尝试使用@Component
注释您的InfoModel
类,如下所示:
@Component public class InfoModel { // code }