Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring + Struts [1 web app / 2 ApplicationContext]

 
Michael Chan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

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
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that it is normal to have both, one for Spring MVC, and another for Struts, both being WebApplicationContext type.
The ContextLoaderPlugIn will create a new WebApplicationContext whose parent is Spring's WebApplicationContext. So calling Struts' WebApplicationContext.getParent() should be the same has calling ActionSupport.getWebApplicationContext().
 
Sabarish Sasidharan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are providing the same application context file to both the loaders (plugin and contextloaderlistener). And thats why its being loaded twice.

A better and more correct approach would be to split the applicationContext.xml into 2 files say, applicationContext-web.xml and applicationContext-main.xml. All the delegating proxy definitions can go into the applicationContext-struts.xml file and all other business logic, facade beans etc can go into the applicationContext-main.xml. Provide the applicationContext-struts.xml to the plugin and the other xml to the ContextLoaderListener. And yes beans defined in applicationContext-struts.xml can ref to the beans defined in applicationContext-main.xml.
 
kri shan
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i mention multiple struts-config.xml file in the Spring's applicationContext.xml file ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please not hijack other ranchers' thread ? You've already asked in your own thread somewhere. Please do not duplicate posts.
 
kri shan
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can i mention applicationContext-struts.xml file thru <plug-in > only in struts-config.xml (or) in both the places(struts-config.xml & web.xml(thru ContextListener)) ?
 
Rashid Darvesh
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI all,
i think this thread is very much close to my problem
i declard the plug in my struts config file like this
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property= "contextConfigLocation" value="/WEB-INF/applicationContext-hibernate.xml"/>
</plug-in>

and my action class extends Action Support. but when i call my action class i get Action not found error.
Any help where i am going wrong
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic