在主ServletContainer和DispatcherServlet之间共享相同的Spring上下文

时间:2017-10-25 12:24:52

标签: java spring spring-mvc servlets

在我的Spring 4应用程序中,我为主应用程序定义了一个servlet容器,为管理仪表板定义了一个用于Spring MVC应用程序的专用servlet。

我的问题是Spring上下文和所有bean /服务都被创建了两次,因此两个上下文中引用的服务都不一样,阻止了来自管理仪表板的对象的任何运行时操作(例如触发刷新一些上下文中的服务。)

主web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_3_1.xsd">
  <display-name>REST API</display-name>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>

  <context-param>
    <param-name>contextClass</param-name>
    <param-value>
      org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
  </context-param>

  <context-param>
    <param-name>log4jRefreshInterval</param-name>
    <param-value>60000</param-value>
  </context-param>

  <context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>local</param-value>
  </context-param>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      <!-- my config classes -->
    </param-value>
  </context-param>

  <servlet>
    <servlet-name>MyWebService</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>javax.ws.rs.Application</param-name>
      <param-value><!-- my.config.class --></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyWebService</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

我的管理应用程序jar中内联的web-fragment.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-fragment>
  <servlet>
    <servlet-name>admin</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:/WEB-INF/admin-servlet.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>admin</servlet-name>
    <url-pattern>/admin/*</url-pattern>
  </servlet-mapping>
</web-fragment>

admin-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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/cache 
        http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">

  <context:component-scan base-package="my.package" />

  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/templates/"/>
    <property name="suffix" value=".jsp"/>
  </bean>

  <mvc:annotation-driven />
  <mvc:resources mapping="/admin/**" location="/resources/, classpath:/META-INF/resources/" />
  <mvc:default-servlet-handler />

  <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="warnLogCategory" value="apperror" />
    <property name="exceptionMappings">
      <props>
        <prop key="java.lang.Exception">error</prop>
      </props>
    </property>
  </bean>
</beans>

有没有办法在主应用程序和所有调度程序servlet之间共享相同的上下文和bean?

1 个答案:

答案 0 :(得分:0)

解决方案:

因为混合

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      my.config.class.MyConfig
    </param-value>
  </context-param>

<init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:/WEB-INF/admin-servlet.xml</param-value>
</init-param>

Spring正在创建一个显然无法合并的AnnotationConfigWebApplicationContextXmlWebApplicationContext。通过将我的admin-servlet.xml转换为等效的WebMvcConfigurerAdapter实施,只会创建1个AnnotationConfigWebApplicationContext