Solutions appear 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)}

Problem-solving ideas:

 

1. If you use annotations configuration, you might be missing a comment cn.itcast.crm.service.BaseDictService implemented (@Service or @Component etc.).

 

2. If you are using (only) xml, you might miss <bean> definitions BaseDictService implementation. 

 

3. If you use comments and implementation comments correctly, check the scanning achieve package is located

(Automatic scanning component checks each component <context: component-scan base-package = "XXXX")

Or (auto scan the Spring <context: annotation-config />)

 

4. Have Error Code: examples are as follows,

BaseDictMapper and configuration in the relevant mapper.xml under mapper file, BaseDictService in service-related files and do a spring configuration file to do the corresponding configuration. Service implementation mapper interfaces with the same method declarations (but does not scan the spring interfaces and classes mapper folder), and therefore no examples BeanFactory Spring bean found in the Context eventually cause does not recognize the corresponding Spring bean

public  interface BaseDictMapper {
      // class code according to the data query 
      List <BaseDict> queryBaseDictByDictTypeCode (String dictTypeCode); 
 
}
public  interface BaseDictService {
       // query category code according to 
      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. Are you sure dependent notes associated with the injection. For example, under dubbo service, add a label spring @Service related to dependence but the server does not need Spring, but dubbo of @Service label. Check maven dependence is correct, the modification is completed remember Install, report lower

Simple code:

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. Check the data source, JDBC, etc. There is no configuration errors.

For example, to view specific mapper, spring under dao profiles and db.properties configuration.

 

7. recommended SpringBoot, can reduce a lot of operating configurations, suitable for the development.

Guess you like

Origin www.cnblogs.com/qingmuchuanqi48/p/11933163.html