java spring中的注解@Repository如何工作? [英] how annotation @Repository in java spring work?
问题描述
我有这样的代码:
存储库
@Repository
public interface EquipmentRepository extends JpaRepository<Equipment, Integer>{
Equipment findById(int id);
}
服务
@Service
public class EquipmentServiceImpl implements EquipmentService {
@Autowired
EquipmentRepository equipmentRepository;
@Override
public Equipment findById(int id) {
return equipmentRepository.findById(id);
}
}
我想知道为什么我可以调用"interface EquipmentRepository"方法. EquipmentRepository是一个接口,对吗?
I wonder that why i can call a method of 'interface EquipmentRepository'. EquipmentRepository is a interface, Right ?
推荐答案
Spring存储库负责将DAO导入到DI容器中,并且还将未经检查的异常导入Spring DataAccessException
中. Spring Repository批注使用@Component批注进行元注释,以便将版本库类用于组件扫描.
Spring Repository is responsible for importing the DAO's into the DI container and also it makes the unchecked exceptions into Spring DataAccessException
. The Spring Repository annotation is meta annotated with the @Component annotation so that the repository classes will be taken up for component scanning.
实施传统Java EE模式的团队,例如数据访问 对象"也可以将这种构造型应用于DAO类,尽管要小心 应该采取理解数据访问之间的区别 在执行对象和DDD样式的存储库之前.该注释是 一个通用的刻板印象,各个团队可能会缩小他们的 语义和适当的使用.
Teams implementing traditional Java EE patterns such as "Data Access Object" may also apply this stereotype to DAO classes, though care should be taken to understand the distinction between Data Access Object and DDD-style repositories before doing so. This annotation is a general-purpose stereotype and individual teams may narrow their semantics and use as appropriate.
这样注释的类可用于Spring DataAccessException
与a结合使用时的翻译
PersistenceExceptionTranslationPostProcessor
.带注释的类是
还阐明了它在整个应用程序体系结构中的作用
出于工具,方面等目的.
A class thus annotated is eligible for Spring DataAccessException
translation when used in conjunction with a
PersistenceExceptionTranslationPostProcessor
. The annotated class is
also clarified as to its role in the overall application architecture
for the purpose of tooling, aspects, etc.
来源: JavaDoc
,但在您的情况下,您还扩展了Spring Data JPA的JpaRepository
. Spring Data自动提供常见CRUD操作的实现. JpaRepository
扩展了CrudRepository
接口,该接口具有为所有基本的crud操作声明的方法.
but in your case you are also extending the JpaRepository
of Spring Data JPA. Spring Data automatically provides implementations of common CRUD operations. The JpaRepository
extends the interface CrudRepository
which has the methods declared for all basic crud operations.
public interface EquipmentRepository extends JpaRepository<Account, Long> { … }
定义此接口有两个目的:
Defining this interface serves two purposes:
- 首先,通过扩展
JpaRepository
我们得到了一堆通用CRUD 我们类型中的方法,可以保存设备,删除它们并 等等. - 第二,这将允许Spring Data JPA存储库基础结构 扫描此接口的类路径并为创建一个Spring bean 它.
- First, by extending
JpaRepository
we get a bunch of generic CRUD methods into our type that allows saving Equipments, deleting them and so on. - Second, this will allow the Spring Data JPA repository infrastructure to scan the classpath for this interface and create a Spring bean for it.
@EnableJpaRepositories
扫描com.acme.repositories
下的所有软件包以查找扩展JpaRepository
的接口,并为其创建一个Spring Bean,并由SimpleJpaRepository
的实现作为后盾(Spring数据通过此类提供了CRUD存储库的默认实现) .
The @EnableJpaRepositories
scans all packages below com.acme.repositories
for interfaces extending JpaRepository
and creates a Spring bean for it that is backed by an implementation of SimpleJpaRepository
(spring data provides default imlpementations of CRUD repository through this class).
这就是为什么即使您尚未定义方法,也可以通过此设置进行粗体操作.
So that is why even when you haven't defined the method , you are able to do crud operations through this setup.
引用: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.repositories
这篇关于java spring中的注解@Repository如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!