简体   繁体   English

Maven多模块项目中的自动连接依赖项

[英]Autowired dependencies in a Maven multi-module project

I have a single Maven project that has the "core" and the "web" pieces in it. 我有一个Maven项目,其中包含“核心”和“网络”部分。 I'm attempting to convert it into multiple modules. 我正在尝试将其转换为多个模块。

I've taken the "web" packages and put them into the new project. 我采用了“网络”包并将它们放入新项目中。 I've added the core project as a dependency and I can reference its classes within Eclipse. 我已经将核心项目添加为依赖项,我可以在Eclipse中引用它的类。 The "core" project correctly shows up in WEB-INF\\lib in the "web" project when I build it. 当我构建它时,“核心”项目在“web”项目中正确显示在WEB-INF\\lib中。

The problem comes when classes from the "core" project are @Autowired in the "web" project. 当“核心”项目中的类在“web”项目中被@Autowired时,问题@Autowired出现了。

In this particular case, I'm autowiring a service class from "core" into one of the web service classes in the "web" project. 在这种特殊情况下,我将服务类从“核心”自动装配到“web”项目中的一个Web服务类中。 If I add the package that the core service is in to my context:component-scan in my "web" application config, it finds that service, but then the service references a repository, which references an entity, which is looking for an entity manager factory that I've got configured in my application-context in my "core" project. 如果我将核心服务所在的包添加到我的context:component-scan在我的“web”应用程序配置中进行context:component-scan ,它会找到该服务,但随后该服务引用一个存储库,该存储库引用一个实体,该实体正在寻找一个实体经理工厂,我已经在我的“核心”项目的应用程序上下文中配置。

So it seems that maybe the context:component-scan in the "core" project isn't happening maybe? 所以似乎可能是context:component-scan “核心”项目中的context:component-scan可能不会发生? Or maybe the classes that are picked up are not being made available to the "web" project? 或者,被选中的课程可能无法用于“网络”项目?

I'm entirely new to multi-module projects, so if anyone can get me started in the right direction to resolve this, I'd greatly appreciate it. 我对多模块项目完全不熟悉,所以如果有人能让我开始朝着正确的方向着手解决这个问题,我将非常感激。

I'm using m2e in Eclipse with the Run Jetty Run plugin if that matters; 如果重要的话,我在Eclipse中使用m2e和Run Jetty Run插件; however, I seem to have the same issues when I do a mvn jetty:run from the command line. 但是,当我使用mvn jetty:run时,我似乎遇到了同样的问题mvn jetty:run

You will need to load the core module application-context from your web module application context. 您需要从Web模块应用程序上下文加载核心模块应用程序上下文。 Otherwise it will not be aware of beans defined in the core application context. 否则,它将不会知道核心应用程序上下文中定义的bean。 Take a look the answers for Application context from other maven module cannot be loaded and Spring import application context from another project for proper solutions. 看看来自其他maven模块的Application上下文的答案无法加载Spring从另一个项目导入应用程序上下文以获得正确的解决方案。
You can also have a look at this post nicely describing how to create a multi-module Spring project using Maven. 您还可以看一下这篇文章,很好地描述了如何使用Maven创建一个多模块的Spring项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM