预计至少有1个可作为此依赖项的自动装配候选者的bean,未找到依赖项类型的合格Bean

时间:2018-08-09 07:19:20

标签: java spring spring-mvc spring-boot

我试图在我的应用程序上执行Gradle构建,但是由于以下Exception导致构建失败, 你能建议这可能是什么原因吗?

  

原因:   org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名称为bean的错误   'org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration $ EnableWebMvcConfiguration':   通过方法“ setConfigurers”表达的不满意的依赖性   参数0:创建名称为'requestConfig'的bean时出错:   通过字段“ requestInterceptor”表示的不满意依赖性:   没有类型为[com.cspt.interceptor.RequestInterceptor]的合格bean   找到依赖项[com.cspt.interceptor.RequestInterceptor]:   预期至少有1个符合条件的自动装配的bean   这种依赖性。依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(required = true)};   嵌套异常为   org.springframework.beans.factory.NoSuchBeanDefinitionException:否   类型为[com.cspt.interceptor.RequestInterceptor]的合格Bean   找到依赖项[com.cspt.interceptor.RequestInterceptor]:   预期至少有1个符合条件的自动装配的bean   这种依赖性。依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(required = true)};   嵌套异常是

     

org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名称为'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.NoSuchBeanDefinitionException:否   类型为[com.cspt.interceptor.RequestInterceptor]的合格Bean   找到依赖项[com.cspt.interceptor.RequestInterceptor]:   预期至少有1个符合条件的自动装配的bean   这种依赖性。依赖注释:   {@ 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;
    }

}

0 个答案:

没有答案