我今天偶然发现了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注释的处理方式有何区别?
发布评论