如何在 bean 中为 spring mvc 添加一个 jpa 存储库?
How to add a jpa repository in bean for spring mvc?
我正在使用 Spring MVC
创建一个 API,我在将 controller
、service
和 Repository
集成在一起时遇到了一些问题。我已经将 Controller 与服务集成在一起,但在将服务与存储库集成时遇到了一些问题。
下面是代码以及 bean 配置文件:
控制器:
@Validated
@RestController
@RequestMapping("/api/clients")
public class ClientController {
private ClientService clientService;
@Autowired
public void setClientService(ClientService clientService) {
this.clientService = clientService;
}
}
服务class:
@Service
public class ClientService {
private ClientRepository clientRepository;
@Autowired
public void setClientRepository(ClientRepository clientRepository) {
this.clientRepository = clientRepository;
}
}
存储库接口:
@Repository
public interface ClientRepository extends JpaRepository<ClientModel, String> {
}
Bean 配置文件:
<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>
即使我在 bean 配置中添加存储库,我也会收到接口错误并且 api 错误仍然存在。
错误:
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)}
根据Spring数据文档:
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.
因此请加上<repositories base-package="package that has ClientRepository" />
并确保 xml 中的名称空间具有 spring-jpa.xsd 和 jpa
============ 已编辑 ===============
要添加多个包:
示例:
<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.*" />
每个 Spring 数据模块都包含一个存储库元素,允许您简单地定义一个 Spring 为您扫描的基础包。您可以在
中添加一个存储库包
<?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>
您可以从 spring.io 中了解更多信息。
https://docs.spring.io/spring-data/jpa/docs/1.5.0.RELEASE/reference/html/repositories.html
我正在使用 Spring MVC
创建一个 API,我在将 controller
、service
和 Repository
集成在一起时遇到了一些问题。我已经将 Controller 与服务集成在一起,但在将服务与存储库集成时遇到了一些问题。
下面是代码以及 bean 配置文件:
控制器:
@Validated
@RestController
@RequestMapping("/api/clients")
public class ClientController {
private ClientService clientService;
@Autowired
public void setClientService(ClientService clientService) {
this.clientService = clientService;
}
}
服务class:
@Service
public class ClientService {
private ClientRepository clientRepository;
@Autowired
public void setClientRepository(ClientRepository clientRepository) {
this.clientRepository = clientRepository;
}
}
存储库接口:
@Repository
public interface ClientRepository extends JpaRepository<ClientModel, String> {
}
Bean 配置文件:
<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>
即使我在 bean 配置中添加存储库,我也会收到接口错误并且 api 错误仍然存在。
错误:
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)}
根据Spring数据文档:
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.
因此请加上<repositories base-package="package that has ClientRepository" />
并确保 xml 中的名称空间具有 spring-jpa.xsd 和 jpa
============ 已编辑 ===============
要添加多个包: 示例:
<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.*" />
每个 Spring 数据模块都包含一个存储库元素,允许您简单地定义一个 Spring 为您扫描的基础包。您可以在
<?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>
您可以从 spring.io 中了解更多信息。
https://docs.spring.io/spring-data/jpa/docs/1.5.0.RELEASE/reference/html/repositories.html