我的spring-data-mongo
我的存储库包没有被<mongo:repositories/>
标记扫描,我有一点奇怪的行为。我使用spring 3.2.3.RELEASE with spring-data-mongo 1.2.1.RELEASE。
我有一个名为edowmis
的项目,其中有2 maven modules
,datalayer
和web
这是webapp。我正在隔离使用datalayer
所以可以忽略其他模块。我有一个数据层的应用程序上下文
所以我想通过编写一个小的单元/集成测试来测试我的设置但是我注意到我不能自动装配我的UserRepository
因为它说没有这样的bean
由于我使用IntelliJ,当事情还可以,我可以看到某些视觉效果。我已将<context:component-scan/>
添加到我的应用程序上下文中,但没有结果。
但是当我添加@Component
注释时,它已经开始识别类。
您可能需要的所有信息都在pastie.org
上 @component
或@Repository
是否真的有必要或我的配置有问题?
答案 0 :(得分:0)
是的,@ Component或@Repository是必需的。扫描只是表明spring应该查找通过注释(@Component,@ Repository,@ Service)标识的类,并将它们作为bean加载。如果您不使用存储库或组件扫描,则必须通过XML配置或Java配置手动实例化所有Spring管理的bean。
你必须告诉spring哪些类变成bean,因为它不假设类路径中的所有内容都应该是一个弹簧管理的bean,这就是你需要使用注释的原因。