1

I'm building a modular web application with spring using maven overlays to combine different modules on a main web application. Both applications are standard maven-archetype-webapp structure. For testing I created a project called "portal" which would be the main war, and a "hello" project which will be overlayed onto the main war. The major files are:

Portal (main) application web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>fmc</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/application-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>main</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>


    <servlet-mapping>
        <servlet-name>main</servlet-name>
        <url-pattern>/portal</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/module-hello</url-pattern>
    </servlet-mapping>
</web-app>

application-context

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:application.properties</value>
        </property>
    </bean>
    <mvc:annotation-driven />
    <tx:annotation-driven transaction-manager="txManager" />
    <context:component-scan base-package="pt.fhc.fmc.services" />
    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages">
        </property>
        <property name="defaultEncoding" value="UTF-8">
        </property>
    </bean>
    <bean id="localeChangeInterceptor"
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang">
        </property>
    </bean>
    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="pt">
        </property>
    </bean>
    <bean id="handlerMapping"
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="order" value="1" />
    </bean>
</beans>

main-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <mvc:annotation-driven />
    <context:component-scan base-package="pt.fhc.fmc.web" />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

hello-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="pt.fhc.fmc.modules.hello" ></context:component-scan>

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

Aditionnally, I have two test controllers. One on the module:

package pt.fhc.fmc.modules.hello;
// Standard spring imports.

@Controller
public class HelloController {

    @Autowired
    TestService testService;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public ModelAndView home(Locale locale) {

        ModelAndView model = new ModelAndView("helloview");
        return model;
    }
}

And the other on the main project (the portal):

package pt.fhc.fmc.web.controllers;

@Controller
public class HomeController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView  home(Locale locale) {

        ModelAndView model = new ModelAndView("home");
        return model;
    }
}

The main pom.xml (on the portal)

    <dependencies>
        <dependency>
            <groupId>pt.fhc.fmc</groupId>
            <artifactId>portal.services</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!-- Modules -->
        <dependency>
            <groupId>pt.fhc.fms.modules</groupId>
            <artifactId>module-hello</artifactId>
            <type>war</type>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>

                <configuration>
                    <overlays>
                        <overlay>
                            <groupId>pt.fhc.fms.modules</groupId>
                            <artifactId>module-hello</artifactId>
                        </overlay>
                    </overlays>
                </configuration>

            </plugin>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Problem is, no controllers are found during context scan. what might be missing?

16:00:42,908 INFO  [io.undertow.servlet] (ServerService Thread Pool -- 100) Initializing Spring root WebApplicationContext
16:00:42,908 INFO  [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 100) Root WebApplicationContext: initialization started
16:00:42,981 INFO  [org.springframework.web.context.support.XmlWebApplicationContext] (ServerService Thread Pool -- 100) Refreshing Root WebApplicationContext: startup date [Fri Mar 11 16:00:42 GMT 2016]; root of context hierarchy
16:00:43,016 INFO  [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] (ServerService Thread Pool -- 100) Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/application-context.xml]
16:00:43,207 INFO  [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer] (ServerService Thread Pool -- 100) Loading properties file from class path resource [application.properties]
16:00:43,211 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (ServerService Thread Pool -- 100) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
16:00:43,396 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] (ServerService Thread Pool -- 100) Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Fri Mar 11 16:00:42 GMT 2016]; root of context hierarchy
16:00:43,435 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] (ServerService Thread Pool -- 100) Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Fri Mar 11 16:00:42 GMT 2016]; root of context hierarchy
16:00:43,513 INFO  [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 100) Root WebApplicationContext: initialization completed in 604 ms
16:00:43,517 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 100) Initializing Mojarra 2.2.12-jbossorg-2 20150729-1131 for context '/fmc'
16:00:43,964 INFO  [io.undertow.servlet] (ServerService Thread Pool -- 100) Initializing Spring FrameworkServlet 'main'
16:00:43,964 INFO  [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 100) FrameworkServlet 'main': initialization started
16:00:43,967 INFO  [org.springframework.web.context.support.XmlWebApplicationContext] (ServerService Thread Pool -- 100) Refreshing WebApplicationContext for namespace 'main-servlet': startup date [Fri Mar 11 16:00:43 GMT 2016]; parent: Root WebApplicationContext
16:00:43,967 INFO  [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] (ServerService Thread Pool -- 100) Loading XML bean definitions from ServletContext resource [/WEB-INF/main-servlet.xml]
16:00:44,001 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (ServerService Thread Pool -- 100) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
16:00:44,032 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] (ServerService Thread Pool -- 100) Looking for @ControllerAdvice: WebApplicationContext for namespace 'main-servlet': startup date [Fri Mar 11 16:00:43 GMT 2016]; parent: Root WebApplicationContext
16:00:44,046 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] (ServerService Thread Pool -- 100) Looking for @ControllerAdvice: WebApplicationContext for namespace 'main-servlet': startup date [Fri Mar 11 16:00:43 GMT 2016]; parent: Root WebApplicationContext
16:00:44,099 INFO  [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 100) FrameworkServlet 'main': initialization completed in 135 ms
16:00:44,100 INFO  [io.undertow.servlet] (ServerService Thread Pool -- 100) Initializing Spring FrameworkServlet 'hello'
16:00:44,100 INFO  [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 100) FrameworkServlet 'hello': initialization started
16:00:44,101 INFO  [org.springframework.web.context.support.XmlWebApplicationContext] (ServerService Thread Pool -- 100) Refreshing WebApplicationContext for namespace 'hello-servlet': startup date [Fri Mar 11 16:00:44 GMT 2016]; parent: Root WebApplicationContext
16:00:44,103 INFO  [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] (ServerService Thread Pool -- 100) Loading XML bean definitions from ServletContext resource [/WEB-INF/hello-servlet.xml]
16:00:44,131 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (ServerService Thread Pool -- 100) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
16:00:44,160 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] (ServerService Thread Pool -- 100) Looking for @ControllerAdvice: WebApplicationContext for namespace 'hello-servlet': startup date [Fri Mar 11 16:00:44 GMT 2016]; parent: Root WebApplicationContext
16:00:44,173 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] (ServerService Thread Pool -- 100) Looking for @ControllerAdvice: WebApplicationContext for namespace 'hello-servlet': startup date [Fri Mar 11 16:00:44 GMT 2016]; parent: Root WebApplicationContext
16:00:44,205 INFO  [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 100) FrameworkServlet 'hello': initialization completed in 105 ms
16:00:44,205 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 100) WFLYUT0021: Registered web context: /fmc
16:00:44,411 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) WFLYSRV0016: Replaced deployment "portal.web.war" with deployment "portal.web.war"

I'm using Widfly 9.0.2 and Spring 4.2

4
  • Why do you say that the controllers are not picked during component scanning ? Did you try to hit the urls for your controllers. Your configuration looks fine Mar 11, 2016 at 16:21
  • I'm missing in the logs those lines saying "Path /hello/.... mapped to servlet x.y.x." which were appearing and working before migrating the projecto for a multiple-dispatcher servlet solution.
    – tggm
    Mar 11, 2016 at 16:43
  • To be more precise, I'm expecting these lines: [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 6) Mapped "{[/portal/],methods=[GET]}" onto public org.springframework.web.servlet.ModelAndView pt.fhc.fmc.web.controllers.HomeController.home2(java.util.Locale)
    – tggm
    Mar 11, 2016 at 16:49
  • To troubleshoot if its failing to scan due to the packaging structure register your beans manually in the config file and see if it works. <bean class="pt.fhc.fmc.web.controllers.HomeController"/> Mar 11, 2016 at 16:53

1 Answer 1

0

Eventually this problem was solved with a reinstall of WildFly. The configuration above works for multiple dispatcher servlets with maven combining multiple web projects into a single deployable war.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.