Announcement Announcement Module
No announcement yet.
Share application context between portlets in single war Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Share application context between portlets in single war

    Hi there,
    spent much time here search for the answer but could not find. Would really appreciate if someone will suggest a solution. My challenge is the following - with Spring 3.0.5 and annotated controllers I'm trying to implement more than one portlet in single war file. There's a substantial application context which should be used by both the portlets. And of course I want to share it to avoid double load.
    So code-wise, my global shared application context:
    ...bunch of various beans, including AnnotationMethodHandlerAdapter,
    DefaultAnnotationHandlerMapping and InternalResourceViewResolver
    I want my individual portlet context to look like this
    	<context:component-scan base-package="com.mytest.controller.portlet1controller" />
    The following options don't work:
    • Adding <import "main-context"> to my individual portlet context file - it loads main application context twice (in case of two portlets). The same effect is achieved when I specify two context files in portlet definition in portlet.xml
    • Specifying ContextLoaderListener and contextConfigLocation in web.xml and not mentioning it in portlet.xml file - in this case each individual portlet controller cannot autowire beans that are defined in global application context
    • Not adding anything at all assuming it should work like this - Spring cannot find handler for portlet render request.

    I have seen many similar questions over internet but they are either unanswered or answered in a way that doesn't work.
    My opinion is that this pattern is quite common - please any suggestions welcome.

  • #2
    As nobody replies, I will answer by myself - it may be useful for other people. After some deep diving into Spring source code it appeared that actually the application context loaded from web.xml is really parent context for every portlet. But....
    The problem with annotated controllers is that they are loaded only once when DefaultAnnotationHandlerMapping is initialized. So when you put this bean into global application context, no portlet controller will ever by found - as they should be loaded in each individual portlet context. It means that you either don't define beans like DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter at all (then Spring will automatically instantiate default beans in each portlet context), or define it per portlet context - e.g. in case you want to define interceptors, etc.

    I think such a problem deserves to be filed as JIRA issue for Spring Web - as handling multiple portlets in one WAR with annotated controller turns out to be not really convenient.