源码

java – spring boot依赖注入


我是Spring的新手,过去几天我一直在学习它.现在我想用它做点什么.在我看来,春季靴子一切都发生了变化.
没有applicationContext文件,我应该使用@Bean.好.在教程中代码正在运行,对我来说它失败了.我错过了什么?

@SpringBootApplication
public class Application {

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

控制器:

@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";
  }
}

错误:

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

我假设必须定义bean …但是在教程中没有bean的defenicion ..或者我没有看到它.

(0)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/java-spring-bootyilaizhuru.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:11月 11, 2019 at 04:53 下午

热评文章