Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring boot dependency injection

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?

@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.

like image 257
5er Avatar asked Jul 16 '15 16:07

5er


People also ask

What is Spring boot dependency injection?

Dependency Injection is a fundamental aspect of the Spring framework, through which the Spring container “injects” objects into other objects or “dependencies”. Simply put, this allows for loose coupling of components and moves the responsibility of managing components onto the container.

Does Spring boot have dependency injection?

Dependency Injection is the main functionality provided by Spring IOC(Inversion of Control). The Spring-Core module is responsible for injecting dependencies through either Constructor or Setter methods.

What is difference between @autowired and @inject?

@Inject and @Autowired both annotations are used for autowiring in your application. @Inject annotation is part of Java CDI which was introduced in Java 6, whereas @Autowire annotation is part of spring framework. Both annotations fulfill same purpose therefore, anything of these we can use in our application.

Is @autowired dependency injection?

Spring @Autowired annotation is used for automatic dependency injection. Spring framework is built on dependency injection and we inject the class dependencies through spring bean configuration file.


2 Answers

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

like image 91
Branislav Lazic Avatar answered Oct 11 '22 21:10

Branislav Lazic


Missed the full error. You need @Component on Test.

like image 42
spencergibb Avatar answered Oct 11 '22 22:10

spencergibb