java spring boot 依赖注入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31459838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:39:07  来源:igfitidea点击:

spring boot dependency injection

javaspringspring-mvcspring-boot

提问by 5er

Im new to Spring, last couple of days I've been learning about it. Now Im trying to do something with it. It seems to me that with spring boot everything has changed. There is no applicationContext file, I should use @Bean. Ok. In in tutorials the code is working, for me it fails. What did I miss?

我是 Spring 的新手,最近几天我一直在学习它。现在我想用它做点什么。在我看来,有了 Spring Boot,一切都变了。没有 applicationContext 文件,我应该使用@Bean。行。在教程中,代码正在运行,对我来说它失败了。我错过了什么?

@SpringBootApplication
public class Application {

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

the controller:

控制器:

@RestController
public class GreetingController {

private final Test test;

@Autowired
public GreetingController(Test test){
    this.test = test;
}

@RequestMapping("/greeting")
  public String greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
    return "greeting" + test.getTest();
  }
}


class Test {

  public String getTest() {
    return "tetst";
  }
}

error:

错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [hello.Test] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 18 more

I assume that bean has to be defined... But in tutorials there is no defenicion of bean.. Or I didnt see it.

我认为 bean 必须被定义......但在教程中没有 bean 的防御......或者我没有看到它。

回答by Branislav Lazic

Testclass is not recognized as a Spring component. Therefore, you cannot inject it in your GreetingController. In order to inject Testobject in that controller, annotate Testclass with like @Componentannotation (or with some other annotation that indicates that your class can be auto scanned).

Test类不被识别为 Spring 组件。因此,您不能将其注入您的GreetingController. 为了Test在该控制器中注入对象,请Test使用类似@Component注释(或使用其他一些表明您的类可以自动扫描的注释)来注释类。

回答by spencergibb

Missed the full error. You need @Componenton Test.

错过了完整的错误。你需要@ComponentTest.