Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring context:component-scan fails to find components in another Eclipse project dependency referenced in POM

I am working on a GWT web application split across two Eclipse Projects (myclient & myservice).

The myclient project references the myservice project via a dependency in the POM.

<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.

The myclient project has a Spring descriptor application-context.xml with the following

<context:component-scan base-package="com.myproject.myclient, com.myproject.myservices"/>

and the 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.

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.

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.

like image 888
Josh Avatar asked Feb 25 '13 00:02

Josh


People also ask

What is@ ComponentScan annotation in Spring boot?

One of the most important annotations in spring is @ComponentScan which is used along with the @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.

How component scan works in Spring what is component scan in Spring?

In Spring configuration xml file, we can define a package for tag component-scan, which tells Spring framework to search all classes within this specified package, to look for those classes which are annotated with @Named or @Component.

What is context component scan in Spring?

Using component scan is one method of asking Spring to detect Spring managed components. Spring needs the information to locate and register all the Spring components with the application context when the application starts. Spring can auto scan, detect, and instantiate components from pre-defined project packages.


1 Answers

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.

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.

But make sure you the latest version of m2e-wtp installed. This way your configuration in your pom.xml and deployment assembly will get correctly configured.

like image 191
gamerson Avatar answered Sep 28 '22 06:09

gamerson