简体   繁体   English

如何处理@Component和@Repository / @Service注释之间的区别是什么?

[英]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). AA_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注释很重要。

  1. Use @Service for all your web service type of DB connections @Service用于所有Web服务类型的数据库连接
  2. Use @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.

相关问题 Spring 中的@Component、@Repository 和@Service 注解有什么区别? - What's the difference between @Component, @Repository & @Service annotations in Spring? @Entity 和 @Repository 注释有什么区别? - What is the difference between @Entity and @Repository annotations? @RepositoryRestController和@Repository注释之间有什么区别? - What is the difference between @RepositoryRestController and the @Repository annotations? Seedstack 中的 Repository 和 Finder 有什么区别? - What's the difference between a Repository and a Finder in Seedstack? "Spring 中的@Repository 和@RepositoryDe​​finition 有什么区别?" - What's the difference between @Repository and @RepositoryDefinition in Spring? 与使用存储库相比,使用服务有什么区别? Spring 开机 - What's the difference between using a Service compared to using a repository? Spring Boot @CascadeOnDelete和CascadeType.REMOVE批注有什么区别? - What's the difference between @CascadeOnDelete and CascadeType.REMOVE annotations? Play控制器的@After和@Finally注释之间有什么区别? - What's the difference between @After and @Finally annotations for Play controllers Repository 和 Service 层的区别 - Difference between Repository and Service layer @Repository @Component批注的NoSuchBeanDefinitionException - NoSuchBeanDefinitionException for @Repository @Component annotations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM