Spring MVC:共享内容 [英] Spring MVC: Sharing context within ear

查看:115
本文介绍了Spring MVC:共享内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个耳包,其中包含一个带有常见对象的jar和两个war webapps,我想使用常见的jar。我已经将配置设置为通过ContextLoaderListener和webapp上下文分别为DispatcherServlet使用应用程序范围的上下文。

I have an ear package that contains one jar with common objects and two war webapps that I'd like to use the common jar. I've setup the configuration to use application wide context via ContextLoaderListener and webapp contexts separately for DispatcherServlet.

我的演示应用程序的设置大致如下

The setup of my demo app is roughly the following


  • common.jar 包含 applicationContext.xml beanRefContext。 xml ,它们应该是应用程序(耳朵)广泛的上下文。文件如下所示。 共享命名空间是共享bean所在的位置。

  • common.jar contains applicationContext.xml and beanRefContext.xml, which are supposed to be application (ear) wide context. The files are like below. shared namespace is where the shared bean is located.

applicationContext

<beans>
    <!-- namespace etc declarations omitted -->
    <context:annotation-config />
    <context:component-scan base-package="study.spring.multicontext.shared" />
</beans>

beanRefContext.xml

<beans>
    <!-- namespace etc declarations omitted -->
<bean id="sharedContext" class="org.springframework.context.support.ClassPathXmlApplicationContext">
    <constructor-arg>
        <list>
            <value>classpath*:applicationContext.xml</value>
        </list>
    </constructor-arg>
</bean>
</beans>




  • webapp1 webapp2 将Spring MVC应用程序打包为单独的战争,其中包含 web.xml 文件,如下所示

    • webapp1 and webapp2 are Spring MVC applications packaged as separate wars with web.xml file like below

      <web-app>
      
      <context-param>
        <param-name>parentContextKey</param-name>
        <param-value>sharedContext</param-value>
      </context-param>
      <context-param>
        <param-name>locatorFactorySelector</param-name>
        <param-value>classpath:beanRefContext.xml</param-value>
      </context-param>
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
      </context-param>
      
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
      <servlet>
          <servlet-name>dos</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      
          <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dos-servlet.xml</param-value>
          </init-param>
      
          <load-on-startup>1</load-on-startup>
      </servlet>
      
      
      <servlet-mapping>
          <servlet-name>dos</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>
      

      xx-servlet.xml 类似于webapp特定的上下文。 web 命名空间是控制器所在的位置。

      and xx-servlet.xml like for webapp specific context. web namespace is where the controllers are located.

      <beans>
          <!-- namespace etc declarations omitted -->
      
          <context:component-scan base-package="study.spring.multicontext.web"/>
          <mvc:annotation-driven />
      
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      
            <property name="suffix" value=".jsp"/>
          </bean>
      
      </beans>
      




      • 共享bean在Controller中以正常方式@Autowired班级

        • The shared bean is @Autowired in normal fashion in Controller classes

          @Autowired
          MySharedBean mySharedBean
          


        • 耳包含有战争和jar,结构就像

        • ear package contains both wars and jar, and structure is like

          ear
           |
           |--common.jar
           |   |--META-INF
           |   |--applicationContext.xml
           |   |--beanRefContext.xml
           |
           |--webapp1.war
           |   |--WEB-INF
           |       |--xx-servlet.xml
           |       |--web.xml
           |
           |--webapp2.war
           |   |--WEB-INF
           |       |--xx-servlet.xml
           |       |--web.xml
          


        • 问题是bean仍然会有两个实例。每个控制器/ webapp一个,因为每个战争中只有一个控制器。我试图改变配置,但无论我做什么,我要么得到零实例,要么得到两个实例。

          The problem is that there will still be two instances of the bean. One for each controller/webapp, since there's only one Controller in each of the wars. I have tried to twiddle with the configuration, but no matter what I do, I either get zero instances or two instances.

          我用内存中的Eclipse MAT检查了引用dump,实际上有4个实例,但我猜这两个是Spring内部使用的。无论如何,从那里可以清楚地看到每个控制器都有它自己的实例。

          I checked the references with Eclipse MAT from a memory dump, and there are actually 4 instances, but I guess the two are for Spring internal use. Anyway, from there it's clearly visible that each controller has it's own instance.

          我读过很多博客文章,讨论论坛等,他们说这应该是就这么简单。有人建议JNDI,但正如我所理解的那样,如果没有它,这应该是可能的。

          I've read numerous of blog posts, discussion forums, etc where they say that this should be as simple as this. Some suggest JNDI, but as I've understood, this should be possible without it.

          并且不可能将战争结合起来并将jar捆绑在里面。因为它可能适用于这个演示应用程序,我正在使用的真实案例不允许这样做。

          And it's not possible to combine the wars and bundle the jar inside. As it might work for this demo app, the real life case I'm working with does not allow this.

          对此问题的任何帮助都非常感谢。提前致谢。

          Any help on this matter is highly appreciated. Thanks in advance.

          SpringSource示例,它执行相同但具有不同配置的相同功能。有点过时,正在寻找基于Spring 3.X的解决方案,如在赏金说明中所述。

          SpringSource example from 2007 for Spring 2.X that does the same but with different configuration. A bit outdated and looking for a Spring 3.X based solution, as dscribed in the bounty description.

          推荐答案

          我解决了它。

          问题出现在班级加载中,因为我怀疑是对@ Akshay的回答。

          The problem was in class loading as I suspected in comments to @Akshay's answer.

          Maven包括春天每个war包中的libs,因此它们被多次加载。要解决此问题,需要生成紧张战争

          Maven included spring libs inside each war package, so they were loaded multiple times. To fix this, one needs to generate skinny wars.

          我假设Akshay关于他从web.xml中的context-params删除 contextConfigLocation 的答案的说明是也是关键角色。

          I assume Akshay's note on his answer to remove the contextConfigLocation from context-params in web.xml was in key role as well.

          这篇关于Spring MVC:共享内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆