出現org.springframework.beans.factory.NoSuchBeanDefinitionException 的解決思路


Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.itcast.crm.service.BaseDictService 
cn.itcast.crm.controller.CustomerController.baseDictService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.itcast.crm.service.BaseDictService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
嚴重: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.itcast.crm.service.BaseDictService cn.itcast.crm.controller.CustomerController.baseDictService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.itcast.crm.service.BaseDictService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

解決問題的思路:

 

1.如果您使用注釋進行配置,則可能會缺少cn.itcast.crm.service.BaseDictService實現的注釋(@Service或@Component等)。

 

2.如果您使用(僅)xml,您可能會錯過BaseDictService實現的<bean>定義。 

 

3.如果您使用注釋並且實現注釋正確,請檢查掃描實現所在的包

(檢查各自組件的自動掃描組件<context:component-scan base-package = “XXXX”)

或者(Spring自動掃描<context:annotation-config/>)

 

4.代碼有無錯誤:例子如下,

BaseDictMapper在mapper文件下並在相關的mapper.xml進行相關配置,BaseDictService在service文件下並做了相關的spring配置文件做了對應的配置。Service實現具有相同申明方法的mapper接口(而spring並不能掃描到mapper文件夾下的接口和類),因此BeanFactory在Spring Context中沒有找到bean的實例,最終導致Spring無法識別相應的bean

public interface BaseDictMapper {
     //根據類別代碼查詢數據
      List<BaseDict> queryBaseDictByDictTypeCode(String dictTypeCode);
 
}
public interface BaseDictService {
      //根據類別代碼查詢
      List<BaseDict> queryBaseDictByDictTypeCode(String dictTypeCode);
}
public class BaseDictServiceImpl implements BaseDictMapper {    //錯誤:應該實現為BaseDictService
      @Autowired
      private BaseDictMapper baseDictMapper;
      @Override
      public List<BaseDict> queryBaseDictByDictTypeCode(String  dictTypeCode)  {
            List<BaseDict> list =  this.baseDictMapper.queryBaseDictByDictTypeCode(dictTypeCode);
            return list;
      }
}

5.是否確實與注入相關便簽的依賴。比如dubbo服務下,添加了spring的相關依賴但是服務端並不需要Spring的@Service標簽,而是dubbo的@Service標簽。檢查maven依賴是否正確,修改完畢記得Install,report下

簡單代碼:

pom.xml

<dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
        <!-- dubbo相關 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        ...
    </dependencies>

service:

import com.alibaba.dubbo.config.annotation.Service;
import com.chunxiansheng.mapper.TbBrandMapper;
import com.chunxiansheng.pojo.TbBrand;
import com.chunxiansheng.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
 
import java.util.List;
 
@Service
public class BrandServiceImpl implements BrandService {
 
    @Autowired
    private TbBrandMapper brandMapper;
    ...
}

6.檢查數據源,JDBC等有沒有配置錯誤。

例如具體查看mapper,dao下的spring配置文件和db.properties的配置。

 

7.推薦使用SpringBoot,可以減少很多配置的操作,適合開發。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM