UnsatisfiedDependencyException: Error creating bean with name

后端 未结 19 850
遇见更好的自我
遇见更好的自我 2020-11-30 01:47

For several days I\'m trying to create Spring CRUD application. I\'m confused. I can\'t solve this errors.

org.springframework.beans.factory.Unsatisfi

相关标签:
19条回答
  • 2020-11-30 02:19

    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";
        }
    }
    
    0 讨论(0)
  • 2020-11-30 02:21

    I had the exactly same issue, with a very very long stack trace. At the end of the trace I saw this:

    InvalidQueryException: Keyspace 'mykeyspace' does not exist

    I created the keyspace in cassandra, and solved the problem.

    CREATE KEYSPACE mykeyspace
      WITH REPLICATION = { 
       'class' : 'SimpleStrategy', 
       'replication_factor' : 1 
      };
    
    0 讨论(0)
  • 2020-11-30 02:22

    I just added the @Repository annotation to Repository interface and @EnableJpaRepositories ("domain.repositroy-package") to the main class. It worked just fine.

    0 讨论(0)
  • 2020-11-30 02:22

    Considering that your package scanning is correctly set either through XML configuration or annotation based configuration.

    You will need a @Repository on your ClientRepository implementation as well to allow Spring to use it in an @Autowired. Since it's not here we can only suppose that's what's missing.

    As a side note, it would be cleaner to put your @Autowired/@Qualifier directly on your member if the setter method is only used for the @Autowired.

    @Autowired
    @Qualifier("clientRepository")
    private ClientRepository clientRepository;
    

    Lastly, you don't need the @Qualifier is there is only one class implementing the bean definition so unless you have several implementation of ClientService and ClientRepository you can remove the @Qualifier

    0 讨论(0)
  • 2020-11-30 02:22

    Check out the table structure of Client table, if there is a mismatch between table structure in db and the entity, you would get this error..

    I had this error which was coming due to datatype mismatch of primary key between db table and the entity ...

    0 讨论(0)
  • 2020-11-30 02:23

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

    0 讨论(0)
提交回复
热议问题