Org.springframework.beans.factory.UnsatisfiedDependencyException: error creating bean named "demoRestController"

I'm new to Spring, and I'm trying to make a learning application, but I'm having problems with Autowiring, I'm adding my code. I am working on loading Spring.

Spring Download Code

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

LoginBean.java

 @Service @Component public class LoginBean { private String userId; private String pwd; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } 

DemoRestController.java

 @RestController @EnableAutoConfiguration @RequestMapping("/demo") @Component public class DemoRestController { private final LoginBean loginBean; @Autowired public DemoRestController(LoginBean loginBean) { this.loginBean=loginBean; } @RequestMapping(value = "/login/{id},{pwd}", method = RequestMethod.GET, produces = "application/json") public @ResponseBody LoginBean loginService(@PathVariable String id, @PathVariable String pwd) { //LoginBean loginBean = new LoginBean(); loginBean.setUserId(id); loginBean.setPwd(pwd); return loginBean; } 

I tried the following scripts to make my @Autowired work:

  • @Autowired to LoginBean loginBean;
  • A getter setter was created from LoginBean in the Controller class and auto-level setters;
  • Created Controller constructor and autwired, as indicated in the above code;

Below is the error that I am getting

 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demoRestController': Unsatisfied dependency expressed through constructor argument with index 0 of type [com.ag.digital.demo.bean.LoginBean]: No qualifying bean of type [com.ag.digital.demo.bean.LoginBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ag.digital.demo.bean.LoginBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE] at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE] at com.ag.digital.demo.boot.DemoApplication.main(DemoApplication.java:14) [classes/:na] Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ag.digital.demo.bean.LoginBean] 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:1373) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] ... 19 common frames omitted 
+15
source share
3 answers

Your DemoApplication class is in the com.ag.digital.demo.boot package, and your LoginBean class is in the com.ag.digital.demo.bean package. By default, components (classes annotated with @Component ) are found if they are in the same package or subpackages of your main DemoApplication application DemoApplication . This means that LoginBean not detected, so dependency injection fails.

There are several ways to solve your problem:

  • Move LoginBean to com.ag.digital.demo.boot or com.ag.digital.demo.boot .
  • Configure packages that are scanned for components using the scanBasePackages @SpringBootApplication attribute, which must be located on DemoApplication .

A few other things that do not cause a problem but are not entirely correct with the code you posted:

  • @Service is a specialization of @Component , so you don't need both on LoginBean
  • Similarly, @RestController is a specialization of @Component , so you don't need both on DemoRestController
  • DemoRestController is an unusual place for @EnableAutoConfiguration . This annotation is usually found in your main application class ( DemoApplication ), either directly or through @SpringBootApplication , which is a combination of @ComponentScan , @Configuration and @EnableAutoConfiguration .
+40
source

Description This exception is thrown when the bean depends on other bean components or some simple properties that were not specified in the definition of the bean factory, although dependency checking has been enabled.

Just look at the classes below for a detailed explanation.

 @Controller public class DemoController{ @Autowired private BusinessService service; //some methods } public class BusinessService { //some methods } 

Problem: in the DemoController class, we implement the BusinessService class as a dependency, and since the Spring container did not find a component of type BusinessService, therefore the Spring container cannot create an instance of this class, and we get an exception

The reason for the exception: if you look closely at the BusinessService class, we make one mistake, that is, we do not annotate the class using @Service, and the Spring container is not able to create this component, so we get this exception

Solution: Just annotate the BusinessService class with @Service and the code works fine.

 //annotating the class with @Service @Service public class BusinessService { //some methods } 

Whenever this exception occurs, simply define the role of this particular class and mark the class with the appropriate role, for example:

  1. if your class is designed to annotate data access, then this class is with @Repository

  2. if your class is for Buisness, then annotate this class with @Service

  3. if your class is for Controller, then annotate this class with @Controller

  4. if your class is for sharing, then annotate this class with @Component

For more information click on this: https://www.quora.com/How-can-I-do-to-automatics-reset-auto-increment-values-in-MySQL-table/answer/Shadab-Kazi- 17

0
source

SOS, 171.4 MHz radio waves, need help, SOS

0
source

All Articles