Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

About Auto Loading the Spring'sAppliction Context in an EJB3 module

  Asked By: Richard    Date: Dec 11    Category: Java    Views: 2848

I have a problem about auto loading the Application Context of Spring before calling any EJB service. I use Spring 2.5.4 and EJB3 and I want that application will be loaded as soon as the EJB module is deployed under the server (like WAR modules) Please Help me. I added some of my code here


public interface TestService {



* @return a Test String


String test();



@javax.ejb.Stateless(name = "TestService")




public class TestServiceImpl implements TestService {


* Logger definition


protected Log log = LogFactory.getLog(getClass());


public String test() {

return "Hello World";




<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "www.springframework.org/dtd/spring-beans.dtd">


<bean name="applicationContext-main" class="org.springframework.context.support.ClassPathXmlApplicationContext">









<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "www.springframework.org/dtd/spring-beans.dtd">






1 Answer Found

Answer #1    Answered By: Landra Schmidt     Answered On: Dec 11

You can use ContextSingletonBeanFactoryLocator of spring  framework for loading  your context  one time on first access to your EJB or on your application  startup .Default path for loading context for this class  is classpath*:beanRefContext.xml. This class Load your context as a singleton per context location and you can get that any time you need by calling  the getInstance methods.

Use this code  as initialization code of your application on startup or any static constructor for loading your context :

BeanFactoryLocator beanFactoryLocator = ContextSingletonBeanFactoryLocator.getInstance(); // load from classpath*:beanRefContext.xml


String youRefContextLocation="classpath*:anyBeanRefContextName.xml";

BeanFactoryLocator beanFactoryLocator = ContextSingletonBeanFactoryLocator.getInstance(youRefContextLocation);

Use this code as initialization code of your EJB on creation:

BeanFactoryLocator beanFactoryLocator = ContextSingletonBeanFactoryLocator.getInstance();//Return previously loaded singleton


You can save beanFactoryLocator as a singletone on startup of application and remove upside line of code and following code will

use something like this YourSpringUtils.getBeanFactoryLocator(). useBeanFactory(beanFactoryLocatorKey);

Definitely you know that you can use more encapsulation for obtaining beanFactoryReference or beanFactory by moving more code to YourSpringUtils


String beanFactoryLocatorKey = "applicationContext-main" ;

String yourBeanName = "yourBeanNameInApplicationContext";

BeanFactoryReference beanFactoryReference = beanFactoryLocator. useBeanFactory(beanFactoryLocatorKey);

BeanFactory beanFactory = beanFactoryReference.getFactory();

Object object = beanFactory.getBean(yourBeanName);


Cast object to your interface  and use it as a local variable or set it on a class member attribute for future use(you can use the casted object as a delegate for your EJB, it means your EJB acts as a wrapper for the casted object or any other case of use)


/*After obtaining all of your beans from the beanFactory you must release beanFactoryReference .*/