首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在主ServletContainer和DispatcherServlet之间共享相同的Spring上下文

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

Stack Overflow用户
提问于 2017-10-25 12:24:52
回答 1查看 551关注 0票数 0

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

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

主web.xml:

代码语言:javascript
复制
<?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片段. The:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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>

是否有一种方法可以在主应用程序和所有dispatcher servlet之间共享相同的上下文和bean?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-25 13:37:53

解决方案:

因为混合

代码语言:javascript
复制
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      my.config.class.MyConfig
    </param-value>
  </context-param>

代码语言:javascript
复制
<init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:/WEB-INF/admin-servlet.xml</param-value>
</init-param>

Spring同时创建了一个AnnotationConfigWebApplicationContext和一个XmlWebApplicationContext,显然无法合并。通过将我的admin-servlet.xml转换为等效的WebMvcConfigurerAdapter实现,只创建了一个AnnotationConfigWebApplicationContext

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46932387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档