Spring + Struts [1 web app / 2 ApplicationContext]

Discussions

Web tier: servlets, JSP, Web frameworks: Spring + Struts [1 web app / 2 ApplicationContext]

  1. Hi, I'm working on a Spring + Struts web application. I am using the Delegating Action method, so I have the plug-in defined in struts-config.xml Since I need to use the Spring-managed beans in my InitServlet, so I have the following in web.xml. contextConfigLocation /WEB-INF/applicationContext.xml org.springframework.web.context.ContextLoaderListener Init com.na.eshop.web.InitServlet 3 This works but I find that there are two Spring context for my app. Log ========================================== - Root WebApplicationContext: initialization started - Loading Spring root WebApplicationContext - Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml] - Bean factory for application context [Root WebApplicationContext]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [dataSource,transactionManager,transactionTemplate,...]; root of BeanFactory hierarchy - 21 beans defined in application context [Root WebApplicationContext] ........ - Root WebApplicationContext: initialization completed in 531 ms - Loading validation rules file from '/WEB-INF/validator-rules.xml' - Loading validation rules file from '/WEB-INF/validation.xml' - ContextLoaderPlugIn for Struts ActionServlet 'action, module '': initialization started - Initializing WebApplicationContext for Struts ActionServlet 'action', module '' - Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml] - Bean factory for application context [WebApplicationContext for namespace 'action-servlet']: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [dataSource,transactionManager,transactionTemplate,...]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [dataSource,transactionManager,transactionTemplate,...]; root of BeanFactory hierarchy - 21 beans defined in application context [WebApplicationContext for namespace 'action-servlet'] ......... ================================================== So my question is is it normal to have 2 contexts for a single web app? If no, how can I reduce to use a single context? Thanks for your help. Michael
  2. No, you should defenitily not have more than one appCtx in your application. You must decide where to initate the context (probably in the initServlet), and then provide *that* instance to struts. One possibility is to make the ContextLoader retrieve the already existing apCtx and provide it to struts. (Do that by subclassing the ContextLoaderPlugIn, make it ApplcationContextAware and declare it in the applicationContext.xml). /Niklas
  3. Hi Niklas, thanks for your hint. Here is what I have. Seems working without problem. package com.yourdomain; import org.springframework.beans.BeansException; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.web.struts.ContextLoaderPlugIn; public class MySpringPlugIn extends ContextLoaderPlugIn { protected WebApplicationContext initWebApplicationContext() throws BeansException, IllegalStateException { WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); String attrName = getServletContextAttributeName(); getServletContext().setAttribute(attrName, wac); return wac; } }
  4. adding a parent-child relation[ Go to top ]

    Hi, There's a simpler way to solve this problem. Spring application context can have a parent-child relation ship between multiple applicationContext's. As you can find in the Spring's context loader plugin src, it loads 'action-servlet.xml' (by default) file as a child of the applicationContext loaded by Spring's ContextLoader(Listener or Servlet). Divide the applicaionContext.xml file into two pieces of file and load "struts-related" beans from ContextLoaderPlugin, and load other beans from ContextLoader(Listener or Servlet). Beware beans loaded from ContextLoader(Listener or Servlet) can be used in your initServlet, but not the ones from ContextLoaderPlugin. Have a nice day!