Я создаю модульное веб-приложение с пружиной, используя наложения maven для объединения различных модулей в основное веб-приложение. Оба приложения являются стандартной структурой maven-archetype-webapp. Для тестирования я создал проект под названием «портал», который будет основной войной, и проект «привет», который будет наложен на главную войну. Основные файлы:Контекстно-сканирование Spring MVC с несколькими сервлетами диспетчера с накладками maven
портал (основной) применение 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>
Aditionnally, у меня есть два контроллера тестирования. Один на модуле:
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
Почему вы говорите, что контроллеры не выбраны во время сканирования компонентов? Вы пытались ударить по URL-адресам для ваших контроллеров. Ваша конфигурация выглядит нормально –
Мне не хватает в журналах этих строк, в которых говорится: «Путь/привет/.... сопоставлен с сервлетом x.y.x.» которые появлялись и работали перед переносом проектора для решения с несколькими диспетчерскими сервлетами. – tggm
Чтобы быть более точным, я ожидаю этих строк: [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (пул потоков ServerService - 6) Отображается «{[/ portal /], methods = [GET]} "на публичный org.springframework.web.servlet.ModelAndView pt.fhc.fmc.web.controllers.HomeController.home2 (java.util.Locale) – tggm