org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘demoRestController’

We Are Going To Discuss About org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘demoRestController’. So lets Start this Java Article.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘demoRestController’

Advertisements
  1. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demoRestController'

    In my case, this is erroring out during my unit test, apparently, not only do I have to add the new package on the Main class, but also on the Test configuration class where in the ComponentScan annotation specific to the test classes, I just added

  2. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demoRestController'

    In my case, this is erroring out during my unit test, apparently, not only do I have to add the new package on the Main class, but also on the Test configuration class where in the ComponentScan annotation specific to the test classes, I just added

Solution 1

Advertisements

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 a sub-package of your main application class DemoApplication. This means that LoginBean isn’t being found so dependency injection fails.

There are a couple of ways to solve your problem:

  1. Move LoginBean into com.ag.digital.demo.boot or a sub-package.
  2. Configure the packages that are scanned for components using the scanBasePackages attribute of @SpringBootApplication that should be on DemoApplication.

A few of other things that aren’t causing a problem, but are not quite right with the code you’ve posted:

  • @Service is a specialisation of @Component so you don’t need both on LoginBean
  • Similarly, @RestController is a specialisation of @Component so you don’t need both on DemoRestController
  • DemoRestController is an unusual place for @EnableAutoConfiguration. That annotation is typically found on your main application class (DemoApplication) either directly or via @SpringBootApplication which is a combination of @ComponentScan, @Configuration, and @EnableAutoConfiguration.

Original Author Andy Wilkinson Of This Content

Solution 2

Advertisements

In my case, this is erroring out during my unit test, apparently, not only do I have to add the new package on the Main class, but also on the Test configuration class where in the ComponentScan annotation specific to the test classes, I just added

com.bpg.csp.cbs.common.util

since this is the new package I added on my project. A snippet of my full test configuration code is below

@Profile("junit")
@Configuration
@ComponentScan(basePackages = {
        "com.bpg.services.booking.gds.adapter.impl.config",
        "com.bpg.csp.cbs.common.util"
})
public class TestConfiguration
{
}

Original Author iamjoshua Of This Content

Solution 3

Advertisements

To me it happened in DogController that autowired DogService that autowired DogRepository. Dog class used to have field name but I changed it to coolName, but didn’t change methods in DogRepository: Dog findDogByName(String name). I change that method to Dog findDogByCoolName(String name) and now it works.

Original Author parsecer Of This Content

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment