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

(171 Views)


Description

This Exception is thrown when a bean depends on other beans or some simple properties that were not specified in the bean factory definition, although dependency checking was enabled.

Just look at below classes for detailed Explanation.

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

Problem:

In the class DemoController we are injecting 'BusinessService' class as dependency and because spring container did not find the bean of 'BusinessService' type, so spring container is unable to instantiate that class, and we are getting the exception

Cause Of Exception:

If you look closely at BusinessService class, we are making one mistake i.e. we are not annotating the class with @Service, and spring container is not in the position to create this bean, so that's why we are getting this exception

Solution:

Just annotate 'BusinessService' class with @Service, and the code works fine

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

Whenever this exception occurs, just decide the role of that particular class and mark the class with corresponding role like:

1. if your class is meant to be Data Access annotate, then that class with @Repository

2. if your class is meant to be Buisness logic, then annotate that class with @Service

3. if your class is meant to be Controller, then annotate that class with @Controller

4. if your class is meant to be Generic Access, then annotate that class with @Component

If you have any other doubt, feel free to ask us in the comments section, we will try to solve it too.

      

Solution Worked 0 UpvotesUpvote

        

Solution Didn't Worked 0 DownvotesDownvote



Comments



Search