Back Forum Reply New

Make BeanFactory accessible globally

Hi,

in my web.xml I'm loading some contexts:Code:
lt;context-paramgt; lt;param-namegt;contextConfigLocationlt;/param-namegt; lt;param-valuegt;   /WEB-INF/SpringDispatcher-servlet.xml   /WEB-INF/ApplicationContext-acegi-security.xml lt;/param-valuegt;
lt;/context-paramgt;
Then I created a bean implementing InitializingBean interface to put the BeanFactory defined by the xml files above into a singleton class. Thus I want to access the BeanFactory by using that singleton from anywhere in my application.

How do I access the BeanFactory in my InitializingBean class?

Can't use just use ApplicationContextAware or BeanFactoryAware?
docs/...onality-events
docs/...anfactoryaware

docs/...textAware.html
docs/...toryAware.html


Originally Posted by karldmooreCan't use just use ApplicationContextAware or BeanFactoryAware?
docs/...onality-events
docs/...anfactoryaware

docs/...textAware.html
docs/...toryAware.htmlOk, I created a class like this:Code:
public class GlobalConfiguration implements ApplicationContextAware, InitializingBean
{
private ApplicationContext ctx;

public void setApplicationContext(ApplicationContext ctx) throws BeansException
{
this.ctx = ctx;
}

public void afterPropertiesSet() throws Exception
{
ConfigurationManager.getConfiguration().put(quot;beanFactoryquot;, ctx.getParentBeanFactory());}
}
It seems as if I get a NullPointerException in my quot;afterPropertiesSetquot;-method. I want to put that Beanfactory into my ConfigurationManager-singleton.

This is the bean definition in my servlet xmlHP Code:
nbsp;lt;!--nbsp;generalnbsp;configurationsnbsp;atnbsp;thenbsp;startnbsp;--gt;lt;beannbsp;id="globalConfig"nbsp;class="de.tfhberlin.eclipsophone.server.webapp.config.GlobalConfiguration"gt;lt;/beangt;
I would presume the afterPropertiesSet fires before the setApplicationContext.  Do you need to implement InitializingBean anyway, can't you just do all the work when the applicationContext is set?

I've seen lots of people who do something like this to hold onto the ApplicationContext.Code:
public class ApplicationContextHolder implements ApplicationContextAware
{   private static BeanFactory factory;   private static ApplicationContextHolder context;
   public static ApplicationContextHolder getInstance ()   {       if ( context == null )       context = new ApplicationContextHolder ();       return context;   }
   public void setApplicationContext ( ApplicationContext applicationContext )throws BeansException   {   factory = applicationContext;   }
   public Object getService ( String serviceName )   {       return factory.getBean ( serviceName );   }
}Forgot to add you can also have a look at SingletonBeanFactoryLocator.
docs/...ryLocator.html


Originally Posted by karldmooreForgot to add you can also have a look at SingletonBeanFactoryLocator.
docs/...ryLocator.html

I almost solved it on a slightly different way. The afterPropertiesSet-method is called after having set the beanFactory  I'm putting the BeanFactory into my singleton class. This works fine! Then I want to load a file with key/value pairs, but it can't find it.
Before this attempt I used the quot;getServletContext().getRealPath(quot;WEB-INF/classes/global.propertiesquot;));quot; method to load this file.
Now I don't have the ServletContext anymore.

How do I load that file anyway?
Code:
public class GlobalConfiguration implements BeanFactoryAware, InitializingBean
{
/*** Logger for this class*/
private static final Logger logger = Logger.getLogger(ContextListener.class);

private BeanFactory bf;

public void setBeanFactory(BeanFactory bf) throws BeansException
{
this.bf = bf;
}

public void afterPropertiesSet() throws Exception
{
ConfigurationManager.getConfiguration().put(quot;beanFactoryquot;, bf);
   // Load global properties file   Properties globalProperties = new Properties();       FileInputStream fis = null;       try       { // this does not work anymore       //fis = new FileInputStream(evt.getServletContext().getRealPath(quot;WEB-INF/classes/global.propertiesquot;));// what instead?       fis = new FileInputStream(......);                     globalProperties = new Properties();globalProperties.load(fis);       }       catch (FileNotFoundException ex)       {
logger.error(quot;contextInitialized(ServletContextEvent) - exception ignoredquot;, ex); //$NON-NLS-1$       }       catch (IOException ex)       {
logger.error(quot;contextInitialized(ServletContextEvent) - exception ignoredquot;, ex); //$NON-NLS-1$       }       ConfigurationManager.getConfiguration().put(quot;globalPropertiesquot;, globalProperties);
}
}It seems I need the ServletContext in this class to have acces to the path of the application.

But I don't know how to access the web applications context from within this class.

Have you had a look at WebApplicationContextUtils?
docs/...textUtils.html
¥
Back Forum Reply New