@Component和@Repository / @Service注释的处理方式有何区别? [英] What's the difference between how @Component and @Repository / @Service annotations are processed?

查看:109
本文介绍了@Component和@Repository / @Service注释的处理方式有何区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天偶然发现了Spring 3.0的一个相当奇怪的问题:

I've stumbled upon a rather strange issue today with Spring 3.0:

有一个抽象类 A 及其具体实现 A_Impl A_Impl 注释为 @Repository 并由Spring自动扫描(< context: component-scan> < context:annotation-config /> 都在上下文中声明。 A A_Impl 部署在单独的JAR中(不确定是否重要)。一切正常。

There's an abstract class A and its concrete implementation 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 and A_Impl are deployed in separate JARs (not sure if that matters). Everything works just fine.

现在,我正在审查那段代码而 @Repository 似乎不太好语义上适合(有问题的类与持久性无关)所以 - 在我的无限智慧中 - 我决定将其更改为更通用的 @Component 。不用说,一切都爆炸了,让我看起来像一个完全白痴。错误(在Spring上下文初始化期间发生)是Spring的 ClassPathResource.getInputStream()方法抱怨 A 类不存在那里(它是,我已经手动检查;加上常规类加载器发现它很好)

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. 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)

没有其他任何改变。如果我交换 @Component @Repository 上下文初始化,如果我将它们交换回来则不会出现上述错误。 Spring文档声明 @Component @Repository 之间没有区别这显然是一个该死的谎言:-)所以我想知道 - 的区别是什么?

Nothing else has changed. If I swap @Component for @Repository context initializes, if I swap them back it doesn't with the above error. Spring documentation claims there's no difference between @Component and @Repository which is clearly a damned lie :-) So I wonder - what is the difference?

推荐答案

我一直在使用 @Component 没有麻烦。

I've been using @Component without troubles.

我想到的唯一一件事(虽然不是那么聪明的)是你的 @Component 可能不是春天的。例如,Tapestry有一个以相同方式命名的注释。其他框架也可能有它。所以检查你的进口。

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. Tapestry, for example, has an annotation named the same way. Other frameworks may also have it. So check your imports.

这篇关于@Component和@Repository / @Service注释的处理方式有何区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆