text: Loading an Spring ApplicationContext in a J2EEContainer
  Thread: Loading an Spring ApplicationContext in a J2EEContainer
  Oct 11th, 2005, 06:47 PM
  julio_helden
  Posts 6	Loading an Spring ApplicationContext in a J2EEContainer
  Hi folks, now i have the following scenario:
  Basically i have an ear that contains a sar (Jboss service archive) with the hibernateServiceMbean. This ear basically calls a Mbean that creates the Hibernate SessionFactory and makes this SessionFactory available for all applications that i have (it loads all hibernate mappings build the session factory and makes this session factory available for all applications horizontally). After deploy this ear i can deploy my war´s and all web applications can access the session factory. It works fine but the problem is the sar is JBoss specific. If i want to deploy this MBean in Weblogic the approach is other. What is my idea, use the spring applicationContext to do this. I create en applicationContext.xml that looks like this:
  <entry key="curso:service=HibernateService">
  <ref local="HibernateService" />
  <bean id="HibernateService" lazy-init="false" class="net.sf.hibernate.jmx.HibernateService" init-method="start">
  <property name="mapResources">
  br/com/netservicos/core/bean/bb/BbEstqInstaladoraBean.hbm.xml,
  br/com/netservicos/core/bean/cp/CpAtividadeBean.hbm.xml,
  <property name="datasource" value="java:/BCV_SOC" />
  <property name="dialect"
  value="org.hibernate.dialect.Oracle9Dialect" />
  <property name="jndiName" value="java:/HibernateFactory_SOC" />
  class="org.springframework.transaction.jta.JtaTran sactionManager" />
  Using spring to start my Mbeans, i can start my MBeans in a single way in all j2ee containers. I understand that this mbean need to be deployed before my web application because it servers to all web applications that i developed. The problem is that i cant figure out a way to load this application context in the J2EE container without use the ContextLoaderListener web application based. is there some way to load an spring context in a j2ee container instead of use a web application ? If i use a web application to load the spring application context the web applcation classpath (WEB-INF/classes) must have the hibernate hbm.xml files and so on. I think that it´s not a good idea.
  Reply With Quote Oct 11th, 2005, 09:56 PM
  Posts 1,142	Check out http://forum.springframework.org/showthread.php?t=18383
  This shows how to create a parent context for your web app.
  Last edited by robyn; May 14th, 2006 at 06:18 PM.
  Reply With Quote Oct 13th, 2005, 08:34 AM
  Posts 6	This shows how to create a parent context for your web app
  Hi wpoitras, my question is how to load an Application Context without a web application. I dont want link my entity classes (hibernate) with a web application. I would like to load my hibernate classes with a standalone alone application context and bind my session factory in JNDI. In this way all my web applcations can access the session factory.
  Reply With Quote Oct 13th, 2005, 09:14 AM
  Posts 1,142	I understand the problem you are trying to solve. A parent context would be a single context accessible from multiple web applications. Its loaded from a classpath. And your web application wouldn't really need to know that the hibernate objects weren't actually initialized inside the web applications. You would access them same way as the web objects (or could be referred to in your web application context). That would save you from having to look up the application context in the JNDI tree.
  If you wanted to add your application context to the JNDI tree you'd need to write some sort of custom start up class (depending on your application server).
  Reply With Quote Oct 13th, 2005, 11:40 AM
  Posts 61	You could always create a SAR or something similar and export your objects as MBeans (JMX). Spring supports JMX exporting and importing and MBeans support starting and stoping where you can load context.
  Reply With Quote Oct 13th, 2005, 09:44 PM
  Posts 736	There's nothing really that wrong with using a web-app as a mechanism to load something in a J2EE container. There doesn't have to be any web code in it of course. In that usage scenario, it's just a bootstrap mechanism, and a well-established one...
  Reply With Quote Oct 14th, 2005, 10:52 AM
  Posts 6	Loading an Application Context in a J2EE Container !
  Hi folks, i have some corcerns about the subject.
  With this post in the forum i could realize some strategies to load the application context.
  Wpoitras saids that i can load a parent context that would be accessible from multiple web applications. I like this approach because this context is loaded from a classpath and your web application wouldn't really need to know that the hibernate objects weren't actually initialized inside the web applications. I could bind my application context to the JNDI tree too.
  Dvoitenko gives me the idea to load the context through MBean Start method, it is interesting but i cant realize how i cant inject my MBean without load my applcation context first.
  Colin saids that i can deploy a war (There doesn't have to be any web code in it of course) that calls the ContextLoaderListener and works only like a bootstrap mechanism. By the way my hbm files needs to be in the war´s class path.
  After all answers i need to know what is the best approach to load an application context that builds my hibernate session factory using spring.