Spring injection boot download

Im new to Spring, the last couple of days I found out about this. Now I'm trying to do something about it. It seems to me that when loading spring, everything changed. There is no applicationContext file, I have to use @ Bean. OK. In textbooks, the code works, for me it fails. What did I miss?

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

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 suppose that a bean should be defined ... But in the textbooks there are no deviations from the bean .. Or I did not see this.

+5
source share
2 answers

Test class is not recognized as a Spring component. Therefore, you cannot enter it in your GreetingController . To add a Test object to this controller, annotate the Test class using the @Component annotation (or using some other annotation that indicates that your class can be automatically scanned).

+9
source

Missed a complete error. You need @Component on Test .

+3
source

All Articles