Spring-data-mongo:MongoRepository没有连线,除非我添加@Component注释

时间:2013-07-17 12:24:02

标签: spring java-ee autowired spring-data-mongodb

我的spring-data-mongo我的存储库包没有被<mongo:repositories/>标记扫描,我有一点奇怪的行为。我使用spring 3.2.3.RELEASE with spring-data-mongo 1.2.1.RELEASE。

我有一个名为edowmis的项目,其中有2 maven modulesdatalayerweb这是webapp。我正在隔离使用datalayer所以可以忽略其他模块。我有一个数据层的应用程序上下文 datalayer config

所以我想通过编写一个小的单元/集成测试来测试我的设置但是我注意到我不能自动装配我的UserRepository因为它说没有这样的bean unit test

由于我使用IntelliJ,当事情还可以,我可以看到某些视觉效果。我已将<context:component-scan/>添加到我的应用程序上下文中,但没有结果。 UserRepository not bein scanned

但是当我添加@Component注释时,它已经开始识别类。 Component being scanned

您可能需要的所有信息都在pastie.org

@component@Repository是否真的有必要或我的配置有问题?

1 个答案:

答案 0 :(得分:0)

是的,@ Component或@Repository是必需的。扫描只是表明spring应该查找通过注释(@Component,@ Repository,@ Service)标识的类,并将它们作为bean加载。如果您不使用存储库或组件扫描,则必须通过XML配置或Java配置手动实例化所有Spring管理的bean。

你必须告诉spring哪些类变成bean,因为它不假设类路径中的所有内容都应该是一个弹簧管理的bean,这就是你需要使用注释的原因。