[英]What's the difference between how @Component and @Repository / @Service annotations are processed?
I've stumbled upon a rather strange issue today with Spring 3.0: 今天我在Spring 3.0中偶然发现了一个相当奇怪的问题:
There's an abstract class A
and its concrete implementation A_Impl
. 有一个抽象类
A
及其具体实现A_Impl
。 A_Impl
is annotated as @Repository
and is auto-scanned by Spring ( <context:component-scan>
and <context:annotation-config/>
are both declared in context). A_Impl
被注释为@Repository
和是自动扫描由弹簧( <context:component-scan>
和<context:annotation-config/>
都在上下文中声明)。 A
and A_Impl
are deployed in separate JARs (not sure if that matters). A
和A_Impl
部署在单独的JAR中(不确定是否重要)。 Everything works just fine. 一切正常。
Now, I was reviewing that code and @Repository
didn't seem like a good fit semantically (the class in question has nothing to do with persistence) so - in my infinite wisdom - I've decided to change that to more generic @Component
. 现在,我正在审查代码,
@Repository
在语义上似乎不太合适(这个类与持久性无关)所以 - 在我的无限智慧中 - 我决定将其更改为更通用的@Component
。 Needless to say, everything blew up leaving me looking like a complete idiot. 毋庸置疑,一切都爆发了,让我看起来像一个完全白痴。 The error (which occurred during Spring context initialization) was Spring's
ClassPathResource.getInputStream()
method complaining about A
class not being there (it is, I've manually checked; plus regular class loader finds it just fine) 错误(在Spring上下文初始化期间发生)是Spring的
ClassPathResource.getInputStream()
方法抱怨A
类不存在(它是,我已经手动检查;加上常规类加载器发现它很好)
Nothing else has changed . 没有其他改变 。 If I swap
@Component
for @Repository
context initializes, if I swap them back it doesn't with the above error. 如果我将
@Component
交换为@Repository
上下文初始化,如果我将它们交换回来,则不会出现上述错误。 Spring documentation claims there's no difference between @Component
and @Repository
which is clearly a damned lie :-) So I wonder - what is the difference? Spring文档声称没有差别之间
@Component
和@Repository
这显然是一个该死的谎言:-)所以我想- 有什么区别?
I've been using @Component
without troubles. 我一直在使用
@Component
没有麻烦。
The only thing (although not-so-intelligent one) that comes to my mind as possibility is that your @Component
might not be the spring one. 我想到的唯一一件事(尽管不是那么聪明的人)可能是你的
@Component
可能不是春天的。 Tapestry, for example, has an annotation named the same way. 例如,Tapestry有一个以相同方式命名的注释。 Other frameworks may also have it.
其他框架也可能有它。 So check your imports.
所以检查你的进口。
|Annotation | Meaning |
+------------+-----------------------------------------------------+
| @Component | generic stereotype for any Spring-managed component |
| @Repository| stereotype for persistence layer |
| @Service | stereotype for service layer |
| @Controller| stereotype for presentation layer (spring-mvc) |
Use of @Service
and @Repository
annotations are important from database connection perspective. 从数据库连接的角度来看,使用
@Service
和@Repository
注释很重要。
@Service
for all your web service type of DB connections @Service
用于所有Web服务类型的数据库连接 @Repository
for all your stored proc DB connections @Repository
用于所有存储的proc数据库连接 If you do not use the proper annotations, you may face commit exceptions overridden by rollback transactions. 如果您不使用正确的注释,则可能会遇到由回滚事务覆盖的提交异常。 You will see exceptions during stress load test that is related to roll back JDBC transactions.
您将在压力负载测试期间看到与回滚JDBC事务相关的异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.