我的应用程序有三层:
- REST 接口(interface)(JBOSS RestEasy)
- 服务层(Spring 服务)
- 持久层(Spring bean)
在持久层我将实现两个类(它们实现相同的接口(interface)):一个是MemoryStore
,另一个是FileSystemStore
。
这些实现应该用@Repository
注解吗?
这两个类将没有任何数据库访问权限。它可以稍后出现在 DatabaseStore 的实现中,但现在不是这样了。
更一般地说,注释 @Repository
必须用于任何持久性 Bean 还是仅用于那些访问数据库的人?
最佳答案
类(class)javadoc说:
A class thus annotated [with @Respository] 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.
如果您需要特殊的异常转换(来自 JDBC、Hibernate 或其他)或您自己的技术(但这意味着您必须扩展 spring 才能知道这一点),我相信您必须使用 @Repository 注释您的类,否则,只需使用@Component 进行注释即可。
在您的特定情况下,我认为使用@Repository 进行注释是正确的,因为您的界面是为数据持久性而设计的。
关于java - 我应该为负责在文件系统上持久保存数据的 bean 使用 @Repository Spring 注释吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12585037/