Java 至少需要1个符合此依赖项autowire候选条件的bean,未找到符合此依赖项类型的bean

Java 至少需要1个符合此依赖项autowire候选条件的bean,未找到符合此依赖项类型的bean,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,我正在尝试在我的应用程序上执行Gradle构建,但由于以下异常,构建失败, 你能提出可能的原因吗 原因: org.springframework.beans.factory.unsatifiedDependencyException: 创建名为的bean时出错 'org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration': 通过方法“setConfigurers”表示的

我正在尝试在我的应用程序上执行Gradle构建,但由于以下异常,构建失败, 你能提出可能的原因吗

原因: org.springframework.beans.factory.unsatifiedDependencyException: 创建名为的bean时出错 'org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration': 通过方法“setConfigurers”表示的未满足的依赖关系 参数0:创建名为“requestConfig”的bean时出错: 通过字段“requestInterceptor”表示的未满足的依赖关系: 没有[com.cspt.interceptor.RequestInterceptor]类型的合格bean 为依赖项[com.cspt.interceptor.RequestInterceptor]找到: 至少需要1个符合以下条件的bean:autowire候选 这种依赖性。依赖项批注: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; 嵌套异常是 org.springframework.beans.factory.noSuchBean定义异常:否 限定类型为[com.cspt.interceptor.RequestInterceptor]的bean 为依赖项[com.cspt.interceptor.RequestInterceptor]找到: 至少需要1个符合以下条件的bean:autowire候选 这种依赖性。依赖项批注: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; 嵌套异常是

org.springframework.beans.factory.unsatifiedDependencyException: 创建名为“requestConfig”的bean时出错:未满足依赖关系 通过字段“requestInterceptor”表示:没有符合条件的bean 找到依赖项的类型[com.cspt.interceptor.RequestInterceptor] [com.cspt.interceptor.RequestInterceptor]:至少需要1个bean 符合此依赖项的自动连线候选项的条件。附属国 注释: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; 嵌套异常是 org.springframework.beans.factory.noSuchBean定义异常:否 限定类型为[com.cspt.interceptor.RequestInterceptor]的bean 为依赖项[com.cspt.interceptor.RequestInterceptor]找到: 至少需要1个符合以下条件的bean:autowire候选 这种依赖性。依赖项批注: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我已经在RequestConfig中执行了RequestInterceptor的自动连接

RequestConfig类:

package com.cspt.config;

import com.cspt.interceptor.RequestInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
class RequestConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private RequestInterceptor requestInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // Register interceptor class and Exclude request on the registered class
        registry.addInterceptor(requestInterceptor).addPathPatterns("/**").excludePathPatterns("/api/users/search","/api/systemuser/search", "/api/svn/search", "/api/project/search/**");
    }
}
RequestInterceptor类:

package com.cspt.interceptor;

import com.cspt.orm.domain.UserAccessLogs;
import com.cspt.orm.services.UserAccessLogsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

@Slf4j
@Component
public class RequestInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private UserAccessLogsService userAccessLogsService;

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object object) {
        try {
            String fullUrl;

            //returns the part of the full URL before query string separator character '?'
            StringBuilder requestURL = new StringBuilder(request.getRequestURL().toString());

            //returns the part of the full URL after query string separator character '?'
            String queryString = request.getQueryString();

            //returns URI
            String action = request.getRequestURI();

            //returns Remote Address of the User
            String remoteAddress = request.getRemoteAddr();

            //returns logged in user
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            String userName = auth.getName();

            if (queryString == null) {
                fullUrl = requestURL.toString();
            } else {
                fullUrl = requestURL.append('?').append(queryString).toString();
            }

            LOG.info("Request parameters. Timestamp: {}, User: {}, Remote Address: {}, Action: {}, Full URL: {} ", LocalDateTime.now(), userName, remoteAddress, action, fullUrl);

            //Save records
            UserAccessLogs userAccessLogs = new UserAccessLogs(LocalDateTime.now(), userName, remoteAddress, action, fullUrl);
            userAccessLogsService.save(userAccessLogs);

        } catch (Exception e) {
            LOG.error("Error while adding access logs for user ", e);
        }

        return true;
    }

}

您的componentScan路径是什么?非常感谢,在定义componentScan的测试包中缺少我的新包条目。这对我现在起作用。您可能希望添加一个带有原因/解决方案的答案,并接受它。