aspose file tools*
The moose likes Other Application Frameworks and the fly likes Spring + Struts [1 web app / 2 ApplicationContext] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Spring + Struts [1 web app / 2 ApplicationContext]" Watch "Spring + Struts [1 web app / 2 ApplicationContext]" New topic
Author

Spring + Struts [1 web app / 2 ApplicationContext]

Michael Chan
Greenhorn

Joined: Aug 08, 2006
Posts: 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.

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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().


[My Blog]
All roads lead to JavaRanch
Sabarish Sasidharan
Ranch Hand

Joined: Aug 29, 2002
Posts: 73
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.


Sab<br /> <br />Perfection does not come from belief or faith. Talk does not count for anything. Parrots can do that. Perfection comes through selfless work.<br />Swami Vivekananda
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1373
How can i mention multiple struts-config.xml file in the Spring's applicationContext.xml file ?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Apr 08, 2004
Posts: 1373
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

Joined: Feb 13, 2004
Posts: 189
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring + Struts [1 web app / 2 ApplicationContext]