Hello,
I want to use single Spring instance in EJB container and Web Tier. My options are:
1. Use 'AbstractStatelessSessionBean' as base class and in the EJB:
beanRefFactory.xml:Code:public void setSessionContext(SessionContext sessionContext) { super.setSessionContext(sessionContext); setBeanFactoryLocatorKey("spring.global"); setBeanFactoryLocator(SingletonBeanFactoryLocator.getInstance()); }
Code:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="spring.global" class="org.springframework.context.support.ClassPathXmlApplicationContext"> <constructor-arg> <list> <value>META-INF/util.xml</value> <value>META-INF/services.xml</value> </list> </constructor-arg> </bean> </beans>
URL: http://forum.springframework.org/arc...p/t-17772.html
2. Create a Singleton utility class which returns applicationContext which is stored in EJB:
In EJB:
SpringUtil class:Code:public void ejbCreate() { this.applicationContext = SpringUtil.getInstance().getContext(); }
URL:http://dev2dev.bea.com/blog/pmalani/...ic_and_sp.htmlCode:package com.app.util; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringUtil { protected ApplicationContext mContext; protected static SpringUtil sInstance; protected SpringUtil() { mContext = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml" } ); } public synchronized static SpringUtil getInstance() { if (sInstance == null) { sInstance = new SpringUtil(); } return sInstance; } public ApplicationContext getContext() { return mContext; } }
3. Create a Singleton utility class which uses SingletonBeanFactoryLocator.
Do NOT Use 'AbstractStatelessSessionBean' as base class for EJB.
It uses same beanRefFactory.xml as 1st approach.
In EJB:
SpringFactoryUtil class:Code:public void ejbCreate() { this.factory = SpringFactoryUtil.getInstance().getBeanFactory(); }
Code:package com.app.util; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.access.BeanFactoryLocator; import org.springframework.beans.factory.access.BeanFactoryReference; import org.springframework.beans.factory.access.SingletonBeanFactoryLocator; import org.springframework.context.access.ContextSingletonBeanFactoryLocator; public class SpringFactoryUtil { protected BeanFactory beanFactory; protected static SpringFactoryUtil sInstance; public synchronized static SpringFactoryUtil getInstance() { if (sInstance == null) { sInstance = new SpringFactoryUtil(); } return sInstance; } private SpringFactoryUtil() { createBeanFactory(); } private void createBeanFactory() { //Looks for beanRefFactory.xml on classpath BeanFactoryLocator locator = SingletonBeanFactoryLocator.getInstance(); //"classpath*:beanRefContext.xml"); BeanFactoryReference ref = locator.useBeanFactory("com.acctmgmtapp.spring.global"); beanFactory = ref.getFactory(); } public BeanFactory getBeanFactory() { return beanFactory; } }
Please advise:
1. Which is the best approach for large applications. I would like to mentain single Spring container instance across EJB & web tier.
2. In this case, how to access Util spring beans from web (JSF) tier?
3. In case 2 and 3, I have to 'cleanup' using WeakReferenceMonitor ?
Thank You.


Reply With Quote