org springframework beans factory UnsatisfiedDependencyException Error creating bean with name demoRestController

0 votes

I am new to Spring and I am trying to make an application for learning but I am getting problems with Autowiring.I am adding my code. I am working on spring boots.

Spring Boot 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 @Autowired to LoginBean loginBean, created getter setter of LoginBean in Controller class and autowired setters along with creating the constructor of Controller and autowired, as given in above code;

 Below is the error which 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
Feb 23, 2022 in Java by Soham
• 9,710 points
36,715 views

1 answer to this question.

0 votes
To answer your doubt, the DemoApplication class is in the comag.digital.demo.boot package and your LoginBean class is in the com.ag.digital.demo.bean package. By default components are found if they are in the same package or a sub-package of your main application class DemoApplication. This means that LoginBean isn't being found so dependency injection fails.

To solve your problem, you need to Move LoginBean into com.ag.digital.demo.boot or a sub-package and then configure the packages that are scanned for components using the scanBasePackages attribute of @SpringBootApplication that should be on DemoApplication.
answered Feb 23, 2022 by Aditya
• 7,680 points

Related Questions In Java

0 votes
0 answers
0 votes
0 answers

UnsatisfiedDependencyException: Error creating bean with name

 I'm trying to create a Spring CRUD ...READ MORE

May 21, 2022 in Java by Kichu
• 19,040 points
52,306 views
0 votes
2 answers

I'm getting following error, while i'm running code in windows xp service pack 3 with JDK 6 version.

Till you send the code, I would ...READ MORE

answered May 16, 2018 in Java by Meci Matt
• 9,460 points
2,686 views
0 votes
1 answer

Hosting spring boot app

There are a couple of things wrong ...READ MORE

answered Sep 11, 2018 in AWS by Priyaj
• 58,020 points
2,482 views
0 votes
1 answer

Validate a jwt token from cognito

Here is a documentation on how to ...READ MORE

answered Oct 31, 2018 in AWS by Priyaj
• 58,020 points
3,999 views
0 votes
1 answer
0 votes
1 answer

How to configure port for a Spring Boot application?

Hello @kartik, You can set port in java ...READ MORE

answered May 22, 2020 in Java by Niroj
• 82,840 points
2,986 views
+1 vote
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP