UnsatisfiedDependencyException: error creating bean named

For several days, I have been trying to create a Spring CRUD application. I'm confused. I can not solve these errors.

org.springframework.beans.factory.UnsatisfiedDependencyException: error creating bean named "clientController": invalid dependency expressed through parameter "setClientService" 0; The nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: error while creating a bean named "clientService": invalid dependency expressed through the "clientRepository" field; The nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: There is no qualification bean of type "com.kopylov.repository.ClientRepository": at least 1 bean is expected that qualifies as a candidate for auto-reversal. Dependency Annotations: {@ org.springframework.beans.factory.annotation.Autowired (required = true)}

and this one

org.springframework.beans.factory.UnsatisfiedDependencyException: error creating bean named "clientService": invalid dependency expressed through the "clientRepository" field; The nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: There is no qualification bean of type "com.kopylov.repository.ClientRepository": at least 1 bean is expected that qualifies as a candidate for auto-reversal. 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 not a single answer to them can help me.

+38
java spring spring-mvc
source share
19 answers

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"; } } 
+17
source share

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

+4
source share

This may happen because the pojos you use do not have the exact constructor the service needs. That is, try to generate all the constructors for pojo or objects (model object) that your serviceClient uses so that the client can be created correctly. In your case, regenerate the constructors (with arguments) for your client object (this is the object of your model).

+3
source share

Given that package scans are correctly installed, either using an XML configuration or using an annotation-based configuration.

You will need @Repository in your ClientRepository implementation to allow Spring to use it in @Autowired . Since this is not here, we can only assume that the missing.

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

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

Finally, you do not need @Qualifier , there is only one class that implements the definition of bean, so if you have several options for implementing ClientService and ClientRepository , you can remove @Qualifier

+1
source share

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

+1
source share

If you are using Spring Boot, your main application should be like this (just to understand and understand things in a simple way) -

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

Make sure you have @Repository and @Service with the appropriate comments.

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

In most cases, this setup solves such trivial problems. Here is a complete example. Hope it helps.

+1
source share

Add @Repository Note to JPA Spring Data Repository

0
source share

According to the documentation you have to set the XML configuration:

 <jpa:repositories base-package="com.kopylov.repository" /> 
0
source share

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

InvalidQueryException: keyspace 'mykeyspace' does not exist

I created a keyspace in Kassandra and solved the problem.

 CREATE KEYSPACE mykeyspace WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }; 
0
source share

I ran into the same problem, and that was because I missed the mark of my DAO class with Entity annotations. I tried below and the error was resolved.

 /** *'enter code here' */ @Entity <-- was missing earlier public class Topic { @Id String id; String name; String desc; . . . } 
0
source share

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

I had this error, which occurred due to a mismatch of the primary key data type between the database table and the entity ...

0
source share

If you describe the field as a criterion in the method definition ("findBy"), you must pass this parameter to the method, otherwise you will get the exception "Unsatisfied dependency expressed through the method parameter".

 public interface ClientRepository extends JpaRepository<Client, Integer> { Client findByClientId(); ////WRONG !!!! Client findByClientId(int clientId); /// CORRECT } 

* I assume that your client entity has a clientId attribute.

0
source share

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

I am facing the same error and the problem was that spring loading did not read my services package, so add:

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

0
source share

Check if your file is missing from the configuration or context XML file.

0
source share

This was a version of the incompatibility, where their inclusion of lettuce was. When I ruled out, it worked for me.

 <!--Spring-Boot 2.0.0 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> 
0
source share

it is possible before the conflict of cans 。。。

0
source share

Just add the @Service annotation to the top of the service class

0
source share

There is another instance , still the same error that will be shown after you do all of the above. when changing your codes, respectively of the mentioned solutions, be sure to keep the originals. So you can easily return. Go and check the location of the base package of the manager-servlet configuration file again. Does it check all relevant packages at application startup.

 <context:component-scan base-package="your.pakage.path.here"/> 
0
source share

The application must be located in the same directory as the scanned package:

enter image description here

-one
source share

All Articles