Spring context:component-scan 无法在 POM 中引用的另一个 Eclipse 项目依赖项中找到组件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15058200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring context:component-scan fails to find components in another Eclipse project dependency referenced in POM
提问by Josh
I am working on a GWT web application split across two Eclipse Projects (myclient & myservice).
我正在开发一个跨越两个 Eclipse 项目(myclient 和 myservice)的 GWT Web 应用程序。
The myclient project references the myservice project via a dependency in the POM.
myclient 项目通过 POM 中的依赖项引用 myservice 项目。
<dependency>
<groupId>com.myproject</groupId>
<artifactId>myservices</artifactId>
<version>1.0.0</version>
</dependency>
The myclient project has a WAR directory src/main/webapp. The output folder for the myclient project is src/main/webapp/WEB-INF/classes.
myclient 项目有一个 WAR 目录 src/main/webapp。myclient 项目的输出文件夹是 src/main/webapp/WEB-INF/classes。
The myclient project has a Spring descriptor application-context.xml with the following
myclient 项目有一个 Spring 描述符 application-context.xml,其内容如下
<context:component-scan base-package="com.myproject.myclient, com.myproject.myservices"/>
and the web.xml
和 web.xml
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>
I have several files in the myservices project annotated as spring @Component, @Service, @Configuration but these are not picked up by the component scan when I run the GWT application in Eclipse. As a test I experimented with placing an @Component in the myclient project and this was successfully created.
我在 myservices 项目中有几个文件被注释为 spring @Component、@Service、@Configuration,但是当我在 Eclipse 中运行 GWT 应用程序时,组件扫描没有选择这些文件。作为测试,我尝试在 myclient 项目中放置一个 @Component 并成功创建。
I believe the following log entry during application startup indicates the source of the problem
我相信应用程序启动期间的以下日志条目表明了问题的根源
org.springframework.core.io.support.PathMatchingResourcePatternResolver - Resolved location pattern [classpath*:com/myproject/myservices/**/*.class] to resources []
the location pattern for the myclient project resolves to all the resources on the classpath but for myservices no resources are found.
myclient 项目的位置模式解析为类路径上的所有资源,但对于 myservices 没有找到资源。
I experimented with building the myservices project JAR and placing this JAR into the src/main/webapp/WEB-INF/lib folder of the myclient project. When I do this the component scanning works. However for development I don't want to have to build and copy a JAR everytime I make changes to the myservices project. I imagine that the component scanning should work on a project referenced through the POM without having to the build that project but after much experimenting I have been unable to get this working.
我尝试构建 myservices 项目 JAR 并将此 JAR 放入 myclient 项目的 src/main/webapp/WEB-INF/lib 文件夹中。当我这样做时,组件扫描工作。但是,对于开发,我不想每次对 myservices 项目进行更改时都必须构建和复制 JAR。我想组件扫描应该在通过 POM 引用的项目上工作,而不必构建该项目,但经过多次试验,我一直无法使其正常工作。
回答by gamerson
Be sure that in the deployment assembly (right click your web project and select "deployment assembly" of your myclient project it is configured to deploy the jar that is outputted by the myservices project. If you are using maven, the m2e, m2e-wtp project configurators should do this deployment assembly setup automatically.
确保在部署程序集(右键单击您的web项目并选择您的myclient项目的“部署程序集”)配置为部署myservices项目输出的jar。如果您使用的是maven,则m2e,m2e-wtp项目配置器应自动执行此部署程序集设置。
Once you have deployment assembly settings properly configured, now when you deploy a project to your server using the Eclispe server adapter publish mechanism, everything should get deployed and the myservices jar would get placed in the right spot for your myclient project.
一旦您正确配置了部署程序集设置,现在当您使用 Eclispe 服务器适配器发布机制将项目部署到您的服务器时,一切都应该被部署,并且 myservices jar 将被放置在您的 myclient 项目的正确位置。
But make sure you the latest version of m2e-wtpinstalled. This way your configuration in your pom.xml and deployment assembly will get correctly configured.
但请确保您安装了最新版本的m2e-wtp。这样,您在 pom.xml 和部署程序集中的配置将得到正确配置。
回答by Szymon Jednac
Try splitting your application-context.xml into 2 separate files:
尝试将 application-context.xml 拆分为 2 个单独的文件:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:service-context.xml,classpath:client-context.xml</param-value>
</context-param>
myservices/src/main/resources/service-context.xml:
myservices/src/main/resources/service-context.xml:
<context:component-scan base-package="com.myproject.myservices"/>
myclient/src/main/resources/client-context.xml:
myclient/src/main/resources/client-context.xml:
<context:component-scan base-package="com.myproject.myclient"/>
回答by Jeevan Patil
Try this :
尝试这个 :
<context:component-scan base-package="com.myproject"/>
回答by dataverse
Please check the following things:
请检查以下事项:
- Is your serviceproject present "only" as jar or is the whole project available via workspace resolution. This can be checked via maven-context-menue (Disable/Enable Workspaceresolution)
- The layout of the webapp under src/main/webapp is okay. BUT do i get you right that all classes are copied there? If so, you should make sure everything is under target directory. So please check whether a maven call "clean package" generates a webappstructure under the target folder and all required libs (e.g. myservice) exist under target\$your-webarchivename\WEB-INF\lib
- Check that packaging in myservice pom.xml is set to jar (you probably have this, right?)
- 您的 serviceproject 是“仅”作为 jar 存在,还是整个项目可通过工作区解析获得。这可以通过 maven-context-menue (Disable/Enable Workspaceresolution) 检查
- src/main/webapp下webapp的布局是可以的。但是我对所有课程都复制在那里的说法正确吗?如果是这样,您应该确保所有内容都在目标目录下。所以请检查maven调用“clean package”是否在target文件夹下生成了webappstructure,target\$your-webarchivename\WEB-INF\lib下是否存在所有需要的libs(例如myservice)
- 检查 myservice pom.xml 中的包装是否设置为 jar(您可能有这个,对吗?)
回答by Henry Leu
it's obvious that your service jar is not included in your client project's build path. this is the only root cause.
很明显,您的服务 jar 未包含在您的客户端项目的构建路径中。这是唯一的根本原因。
Make sure 3 points:
确保3点:
- you have run mvn clean installunder your service project which has correct pom.xml.
- you have run mvn eclipse:eclipseunder your client project. this will pull out all your dependency project.
- check your client eclipse project's build path dialog. is there your service jar in the list? Make sure this
- 您已经在具有正确 pom.xml 的服务项目下运行了mvn clean install。
- 您已经在您的客户端项目下运行了mvn eclipse:eclipse。这将拉出您所有的依赖项目。
- 检查您的客户端 eclipse 项目的构建路径对话框。列表中有您的服务 jar 吗?确保这
回答by abishkar bhattarai
You better once look this tutorial http://fusesource.com/docs/framework/2.2/deploy_guide/CXFServletDeploySpring.html
你最好看看这个教程 http://fusesource.com/docs/framework/2.2/deploy_guide/CXFServletDeploySpring.html