简体   繁体   中英

How to add a jpa repository in bean for spring mvc?

I am creating an API using Spring MVC and I am facing few issues in integrating controller , service and Repository together. I have integrated Controller with service but facing few issues in integrating service with repository.

Here's the code along with the bean config file:

Controller:

@Validated
@RestController
@RequestMapping("/api/clients")
public class ClientController {



    private ClientService clientService;
    @Autowired
    public void setClientService(ClientService clientService) {
        this.clientService = clientService;
    }
}

Service class:

@Service
public class ClientService {


    private ClientRepository clientRepository;

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

Repository interface:

@Repository
public interface ClientRepository extends JpaRepository<ClientModel, String> {

}

Bean config file:

        <bean id="clientController" class="com.practo.hms.api.clients.ClientController">
        <property name="clientService" value="clientService"/>
        </bean>
        <bean id="clientService" class="com.practo.hms.api.clients.ClientService">
        </bean>

Even if I add the repository in the bean config,I get a interface error and the api error persists.

Error:

 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.practo.hms.api.clients.ClientRepository' available: expected at least 1 bean which  
 qualifies as autowire candidate. Dependency annotations:  
 {@org.springframework.beans.factory.annotation.Autowired(required=true)}

According to Spring Data Documentation:

1.2.3 Creating repository instances

In this section you create instances and bean definitions for the repository interfaces defined. One way to do so is using the Spring namespace that is shipped with each Spring Data module that supports the repository mechanism although we generally recommend to use the Java-Config style configuration. XML configuration

Each Spring Data module includes a repositories element that allows you to simply define a base package that Spring scans for you.

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.springframework.org/schema/data/jpa"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

  <repositories base-package="com.acme.repositories" />

</beans:beans>

In the preceding example, Spring is instructed to scan com.acme.repositories and all its subpackages for interfaces extending Repository or one of its subinterfaces. For each interface found, the infrastructure registers the persistence technology-specific FactoryBean to create the appropriate proxies that handle invocations of the query methods. Each bean is registered under a bean name that is derived from the interface name, so an interface of UserRepository would be registered under userRepository. The base-package attribute allows wildcards, so that you can define a pattern of scanned packages.

Therefore please add <repositories base-package="package that has ClientRepository" />

And make sure that your namespaces in the xml have spring-jpa.xsd, and jpa

============ Edited ===============

To add multiple packages: Example:

<repositories base-package="org.apache.fineract.commands.domain" />
<repositories base-package="org.apache.fineract.infrastructure.*.domain" />
<repositories base-package="org.apache.fineract.accounting.*.domain" />
<repositories base-package="org.apache.fineract.useradministration.domain" />
<repositories base-package="org.apache.fineract.organisation.*.domain" />
<repositories base-package="org.apache.fineract.portfolio.*" />
<repositories base-package="org.apache.fineract.mix.domain" />
<repositories base-package="org.apache.fineract.scheduledjobs.domain" />
<repositories base-package="org.apache.fineract.template.domain" />
<repositories base-package="org.apache.fineract.infrastructure.campaigns.sms.domain" />
<repositories base-package="org.apache.fineract.adhocquery.domain" />
<repositories base-package="org.apache.fineract.notification.domain"/>
<repositories base-package="org.apache.fineract.infrastructure.campaigns.email.domain"/>
<repositories base-package="org.cs.commands.domain" />
<repositories base-package="org.cs.infrastructure.*.domain" />
<repositories base-package="org.cs.portfolio.*" />
<repositories base-package="org.cs.message.*" />

Each Spring Data module includes a repository element that allows you to simply define a base package that Spring scans for you. You can add a repository package inside the <beans:beans />

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.springframework.org/schema/data/jpa"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

  <repositories base-package="com.practo.hms.api.clients.repositories" />

</beans:beans>

You can learn more from spring.io.

https://docs.spring.io/spring-data/jpa/docs/1.5.0.RELEASE/reference/html/repositories.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM