当spring 对象@Autowired 注入失败或者创建对象Bean失败、No qualifying bean/Error creating bean 的失败情形分析和解决方案

   错误信息

今天开发的过程中突然出现如下错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.smilx.ipayment.dao.XkioskFileMapper' available: 
           expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1716)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1272)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1226)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
	... 63 more
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ipaymentCheckController': 
           Unsatisfied dependency expressed through field 'XkioskFileMapper'; 
           nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
           No qualifying bean of type 'com.smilx.ipayment.dao.SmilxXkioskFileMapper' available: expected at least 1 bean which qualifies as autowire candidate. 
        Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)

 以上错误信息大致意思是:No qualifying bean of type 'com.smilx.ipayment.dao.XkioskFileMapper' available 这个dao层的mapper 依赖注入失败!

   分析及解决办法:

     既然是注入失败首先要检查的就是扫描 Mapper 接口和容器管理配置,Dao层的路径是否配置正确!

        然后检查是否有重名的Bean!

        还有可能在@Service注解的类中直接使用Spring注入的对象,导致加@Service注解的类创建Bean不成功,如下

    @Autowired
    private CommonPropertiesUtil util;
    @Autowired
    private CommonService commonService;

    String SECRET_SEX = util.getSecretSex()//直接使用的注入对象util;
    String SECRET_USER = util.getSecretUser();

  把String SECRET_USER = util.getSecretUser()从类中移到方法中运行正常了!

       在这里把自己遇到的问题和解决方案做个记录备份,希望也能解决你的问题

       还有其他情况欢迎评论讨论!

posted @ 2021-01-14 19:38  元小疯  阅读(2399)  评论(0编辑  收藏  举报