Spring MVC上下文扫描,带有多个调度程序servlet和maven叠加层

时间:2016-03-11 16:08:23

标签: spring maven spring-mvc servlets

我正在构建一个带有弹簧的模块化Web应用程序,使用maven覆盖来组合主Web应用程序上的不同模块。这两个应用程序都是标准的maven-archetype-webapp结构。为了进行测试,我创建了一个名为“portal”的项目,它将成为主要的战争,以及一个“hello”项目,它将被重叠到主战争上。主要文件是:

门户网站(主要)应用 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>

应用上下文

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

主要-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>

你好-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>

另外,我有两个测试控制器。一个在模块上:

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;
    }
}

另一个在主项目(门户网站)上:

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;
    }
}

主要的pom.xml(在门户网站上)

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

问题是,在上下文扫描期间找不到控制器。可能缺少什么?

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"

我正在使用Widfly 9.0.2和Spring 4.2

1 个答案:

答案 0 :(得分:0)

最终通过重新安装WildFly解决了这个问题。上面的配置适用于多个调度程序servlet,maven将多个Web项目组合成一个可部署的战争。