Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Richard Torres   on Dec 11 In Java Category.

  
Question Answered By: Landra Schmidt   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

Or

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 .*/
beanFactoryReference.release();

Share: 

 
 


Tagged: