春季错误原因:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'test'的bean时出错

时间:2019-01-15 03:32:46

标签: java spring gradle

我在gradle构建过程中遇到此错误,整日都在寻找解决方案,但没有运气。使用gradle的弹簧靴。我正在构建简单的测试Rest api,基本上,当调用/ test时,它将创建一个日志记录对象并将其发送到另一个api(单独的项目及其运行中)。以下是完整的错误和我的源文件。

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'test': Unsatisfied dependency expressed through field 'testClient'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'my.com.cybersolution.skb.testproject.client.TestClient' 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:596)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:127)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
    ... 49 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'my.com.cybersolution.skb.testproject.client.TestClient' 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:1646)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1205)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1166)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
    ... 67 more

TestProjectApplication.java

package my.com.cybersolution.skb.testproject; 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;    
@SpringBootApplication
public class TestProjectApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestProjectApplication.class, args);
    }

}

TestController.java

package my.com.cybersolution.skb.testproject.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import my.com.cybersolution.skb.testproject.client.TestClient;
import my.com.cybersolution.skb.testproject.object.LoggingObject;

@Component("test")
@RestController
@RequestMapping("/test")
@CrossOrigin
public class TestController {
    @Autowired
    private TestClient testClient;

    @GetMapping
    public String getLoggingList() {
        LoggingObject loggingObject = new LoggingObject();
        loggingObject.setMethod("GET");
        loggingObject.setUrl("test.org");
        loggingObject.setLogDate(new Date());

        testClient.sendMessage(loggingObject);

        return "test success";
    }
}

TestClient.java

package my.com.cybersolution.skb.testproject.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import my.com.cybersolution.skb.testproject.object.LoggingObject;

@FeignClient(name = "eloggingClient", url = "localhost:9090")
public interface TestClient {
    @PostMapping("/logging")
    public boolean sendMessage(@RequestBody LoggingObject logging);
}

LoggingObject.java

package my.com.cybersolution.skb.testproject.object;

import java.util.Date;

public class LoggingObject {
    private Long logId;

    private String method;

    private String url;

    private Date logDate;

    public LoggingObject() {

    }

    public LoggingObject(String method, String url) {
        this.method = method;
        this.url = url;
    }

    public Long getLogId() {
        return logId;
    }

    public void setLogId(Long logId) {
        this.logId = logId;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Date getLogDate() {
        return logDate;
    }

    public void setLogDate(Date logDate) {
        this.logDate = logDate;
    }
}

1 个答案:

答案 0 :(得分:1)

FeignClient需要知道接口的位置。使用它来做到这一点-

@EnableFeignClients(basePackages = {"my.com.cybersolution.skb.testproject.client"})