UnsatisfiedDependencyException: Error creating bean with name

We Are Going To Discuss About UnsatisfiedDependencyException: Error creating bean with name. So lets Start this Java Article.

UnsatisfiedDependencyException: Error creating bean with name

  1. UnsatisfiedDependencyException: Error creating bean with name

    The ClientRepository should be annotated with @Repository tag.
    With your current configuration Spring will not scan the class and have knowledge about it. At the moment of booting and wiring will not find the ClientRepository class.

  2. UnsatisfiedDependencyException: Error creating bean with name

    The ClientRepository should be annotated with @Repository tag.
    With your current configuration Spring will not scan the class and have knowledge about it. At the moment of booting and wiring will not find the ClientRepository class.

Solution 1

The ClientRepository should be annotated with @Repository tag.
With your current configuration Spring will not scan the class and have knowledge about it. At the moment of booting and wiring will not find the ClientRepository class.

EDIT
If adding the @Repository tag doesn’t help, then I think that the problem might be now with the ClientService and ClientServiceImpl.

Try to annotate the ClientService (interface) with @Service. As you should only have a single implementation for your service, you don’t need to specify a name with the optional parameter @Service("clientService"). Spring will autogenerate it based on the interface’ name.

Also, as Bruno mentioned, the @Qualifier is not needed in the ClientController as you only have a single implementation for the service.

ClientService.java

@Service
public interface ClientService {

    void addClient(Client client);
}

ClientServiceImpl.java (option 1)

@Service
public class ClientServiceImpl implements ClientService{

    private ClientRepository clientRepository;

    @Autowired
    public void setClientRepository(ClientRepository clientRepository){
        this.clientRepository=clientRepository;
    }

    @Transactional
    public void addClient(Client client){
        clientRepository.saveAndFlush(client);
    }
}

ClientServiceImpl.java (option 2/preferred)

@Service
public class ClientServiceImpl implements ClientService{

    @Autowired
    private ClientRepository clientRepository;

    @Transactional
    public void addClient(Client client){
        clientRepository.saveAndFlush(client);
    }
}

ClientController.java

@Controller
public class ClientController {
    private ClientService clientService;

    @Autowired
    //@Qualifier("clientService")
    public void setClientService(ClientService clientService){
        this.clientService=clientService;
    }

    @RequestMapping(value = "registration", method = RequestMethod.GET)
    public String reg(Model model){
        model.addAttribute("client", new Client());
        return "registration";
    }

    @RequestMapping(value = "registration/add", method = RequestMethod.POST)
    public String addUser(@ModelAttribute Client client){
        this.clientService.addClient(client);
    return "home";
    }
}

Original Author Alex Roig Of This Content

Solution 2

I know it seems too late, but it may help others in future.

I face the same error and the problem was that spring boot did not read my services package so add:

@ComponentScan(basePackages = {"com.example.demo.Services"}) (you have to specify your own path to the services package) and in the class demoApplication (class that have main function) and for service interface must be annotated @Service and the class that implement the service interface must be annotated with @Component, then autowired the service interface.

Original Author ben fadhel Ichraf Of This Content

Solution 3

Try adding @EntityScan(basePackages = “insert package name here”) on top of your main class.

Original Author Shubham Chopra Of This Content

Solution 4

If you are using Spring Boot, your main app should be like this (just to make and understand things in simple way) –

package aaa.bbb.ccc;
@SpringBootApplication
@ComponentScan({ "aaa.bbb.ccc.*" })
public class Application {
.....

Make sure you have @Repository and @Service appropriately annotated.

Make sure all your packages fall under – aaa.bbb.ccc.*

In most cases this setup resolves these kind of trivial issues. Here is a full blown example. Hope it helps.

Original Author Ajay Kumar Of This Content

Conclusion

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