Spring Framework applicationContext.xml

G

gtengli

I'm very new to Spring Framework and just trying to understand and get
hold of it.
My manager just told me that the application's applicationContext.xml
file is too big and it needs to brake down into modules.
Can an application have multiple applicationContext.xml?. If yes, how
do I do? what direction to go?. Any help is appreciated.

Thanks in advance
 
I

iksrazal

You can't do multiple applicationContext.xml files, but luckily that
isn't really the solution. So in our case - using hibernate and struts
along with spring, we have:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web
Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp">

<filter>
<filter-name>hibernate</filter-name>

<filter-class>org.springframework.orm.hibernate.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>mySessionFactory</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>hibernate</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>hibernate</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>

<listener>

<listener-class>com.siemens.swa.session.SessionListener</listener-class>

</listener>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>definitions-config</param-name>
<param-value>/WEB-INF/tiles-defs.xml</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml
</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>validate</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<!-- Inicializando o BeanFactory -->
<servlet>
<servlet-name>ObjectFactoryServlet</servlet-name>
<servlet-class>com.siemens.swa.util.support.ObjectFactoryServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>protected/index.html</welcome-file>
<welcome-file>protected/index.htm</welcome-file>
<welcome-file>protected/index.jsp</welcome-file>
</welcome-file-list>

<session-config>
<session-timeout>10</session-timeout>
</session-config>

<error-page>
<error-code>500</error-code>
<location>/protected/common/openError.jsp</location>
</error-page>

<taglib>
<taglib-uri>/WEB-INF/tld/webmenu.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/webmenu.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts-bean-el.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-bean-el.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts-html-el.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-html-el.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts-logic-el.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-logic-el.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts-nested.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts-template.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-template.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/c.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/c.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/swa.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/swa.tld</taglib-location>
</taglib>
<taglib>

<taglib-uri>http://jakarta.apache.org/taglibs/datagrid-1.0</taglib-uri>

<taglib-location>/WEB-INF/tld/taglibs-datagrid.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/functions</taglib-uri>
<taglib-location>/WEB-INF/tld/fn.tld</taglib-location>
</taglib>


<!-- Arquivo de mensagens p/ internacionalizacao-->
<context-param>

<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>ApplicationResources</param-value>
</context-param>
<context-param>
<param-name>application</param-name>
<param-value>ApplicationResources</param-value>
</context-param>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml
/WEB-INF/actionContext.xml /WEB-INF/serviceContext.xml
/WEB-INF/daoContext.xml</param-value>
</context-param>
</web-app>


Notice the end of the xml - one for a dao, service etc. Note that there
are other motives in the example I give - they have to do with advance
Hibernate topics like lazy loading.

Anyways, so then in our case we have the ObjectFactoryServlet
referenced above which does:

public void init() throws ServletException {
try {
log.info("Carregando BeanFactory no ObjectFactory.");
super.init();
WebApplicationContext wac = WebApplicationContextUtils.
getRequiredWebApplicationContext(getServletContext());
ObjectFactory.setFactory(wac);
} catch (Exception e) {
log.fatal("Não foi possível carregar o BeanFactory no "
+ "ObjectFactory.", e);
}
}

Gotta run, but you can see some of the config files here:

http://www.braziloutsource.com/random/logs

HTH,
iksrazal
http://www.braziloutsource.com/
 
P

PerfectDayToChaseTornados

| I'm very new to Spring Framework and just trying to understand and get
| hold of it.
| My manager just told me that the application's applicationContext.xml
| file is too big and it needs to brake down into modules.
| Can an application have multiple applicationContext.xml?. If yes, how
| do I do? what direction to go?. Any help is appreciated.
|
| Thanks in advance
|
You can break it down into as many 'context' files as you like they just
need to be registered with the main context. If you are running a
WebApplicationContext for example you do it this way in the web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml,
/WEB-INF/spring-schedule.xml,
/WEB-INF/spring-email.xml
</param-value>
</context-param>
There are several other ways to do this, check out the Spring documentation
for details. If you still have problems I would suggest the Spring forums
they contain a huge amount of Spring info. http://forum.springframework.org/

--
-P
"Programs that are hard to read are hard to modify.
Programs that have duplicated logic are hard to modify.
Programs with complex conditional logic are hard to modify"

( Kent Beck)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top