For several days I'm trying to create Spring CRUD application. I'm confused. I can't solve this errors.
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientController': Unsatisfied dependency expressed through method 'setClientService' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientService': Unsatisfied dependency expressed through field 'clientRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.kopylov.repository.ClientRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
and this
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientService': Unsatisfied dependency expressed through field 'clientRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.kopylov.repository.ClientRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
ClientController
@Controller public class ClientController { private ClientService clientService; @Autowired @Qualifier("clientService") public void setClientService(ClientService clientService){ this.clientService=clientService; } @RequestMapping(value = "registration/add", method = RequestMethod.POST) public String addUser(@ModelAttribute Client client){ this.clientService.addClient(client); return "home"; } }
ClientServiceImpl
@Service("clientService") public class ClientServiceImpl implements ClientService{ private ClientRepository clientRepository; @Autowired @Qualifier("clientRepository") public void setClientRepository(ClientRepository clientRepository){ this.clientRepository=clientRepository; } @Transactional public void addClient(Client client){ clientRepository.saveAndFlush(client); } }
ClientRepository
public interface ClientRepository extends JpaRepository<Client, Integer> { }
I looked through a lot of similar questions, but no one answer to them can't help me.
The best solution is to properly isolate beans. The DispatcherServlet is responsible for routing and handling requests so all related beans should go into its context. The ContextLoaderListener , which loads the root context, should initialize any beans the rest of your application needs: services, repositories, etc.
UnsatisfiedDependencyException gets thrown when, as the name suggests, some bean or property dependency isn't satisfied. This may happen when a Spring application tries to wire a bean and can't resolve one of the mandatory dependencies.
BeanCreationException: Error creating bean with name happens when a problem occurs when the BeanFactory creates a bean. If the BeanFactory encounters an error when creating a bean from either bean definition or auto-configuration, the BeanCreationException will be thrown.
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"; } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With