The client repository must be annotated with the @Repository tag. With your current configuration, Spring does not scan the class and does not know about it. At the time of loading and posting, the ClientRepository class will not be found.
EDIT If adding the @Repository tag @Repository not help, then I think that the problem may now be with ClientService and ClientServiceImpl .
Try annotating the ClientService (interface) with @Service . Since your service requires only one implementation, you do not need to specify a name with the optional @Service("clientService") parameter. Spring will auto-generate it based on the interface name.
Also, as Bruno noted, @Qualifier not required in the ClientController , since you only have one implementation for this 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"; } }
Alex roig
source share