views:

9375

answers:

6

Is there a way to statically/globally request a copy of the ApplicationContext in a Spring application?

Assuming the main class starts up and initializes the application context, does it need to pass that down throw the call stack to any classes that need it, or is there a way for a class to ask for the previously created context? (Which I assume has to be a singleton?)

Thanks!

+5  A: 

Here's a nice way (not mine, the original reference is here: http://sujitpal.blogspot.com/2007/03/accessing-spring-beans-from-legacy-code.html

I've used this approach and it works fine. Basically it's a simple bean that holds a (static) reference to the application context. By referencing it in the spring config it's initialized.

Take a look at the original ref, it's very clear.

Steve B.
Very good blog entry there!
Chris
+1  A: 

Take a look at ContextSingletonBeanFactoryLocator. It provides static accessors to get hold of Spring's contexts, assuming they have been registered in certain ways.

It's not pretty, and more complex than perhaps you'd like, but it works.

skaffman
+11  A: 

If the object that needs access to the container is a bean in the container, just implement the BeanFactoryAware or ApplicationContextAware interfaces.

If an object outside the container needs access to the container, I've used a standard GoF singleton pattern for the spring container. That way, you only have one singleton in your application, the rest are all singleton beans in the container.

Don Kirkby
There is also a better interface for ApplicationContexts - ApplicationContextAware. BeanFactoryAware should work but you'd have to cast it to an application context if you need app context functionality.
MetroidFan2002
Thanks for the tip, I've updated the answer.
Don Kirkby
+4  A: 

Before you implement any of the other suggestions, ask yourself these questions...

  • Why am I trying to get the ApplicationContext?
  • Am I effectively using the ApplicationContext as a service locator?
  • Can I avoid accessing the ApplicationContext at all?

The answers to these questions are easier in certain types of applications (Web apps, for example) than they are in others, but are worth asking anyway.

Accessing the ApplicationContext does kind of violate the whole dependency injection principle, but sometimes you've not got much choice.

belugabob
A good example is JSP tags; their creation is goverened by the servlet container, so they have no choice but to obtain the context statically. Spring provides base Tag classes, and they use BeanFactoryLocators to get the contexts they need.
skaffman
+1  A: 

I believe you could use SingletonBeanFactoryLocator. The beanRefFactory.xml file would hold the actual applicationContext, It would go something like this:

<bean id="mainContext" class="org.springframework.context.support.ClassPathXmlApplicationContext">
     <constructor-arg>
        <list>
            <value>../applicationContext.xml</value>
        </list>
     </constructor-arg>
 </bean>

And the code to get a bean from the applicationcontext from whereever would be something like this:

BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance();
BeanFactoryReference bf = bfl.useBeanFactory("mainContext");
SomeService someService = (SomeService) bf.getFactory().getBean("someService");

The Spring team discourage the use of this class and yadayada, but it has suited me well where I have used it.

stian
+3  A: 

If you use a web-app there is also another way to access the application context without using singletons by using a servletfilter and a ThreadLocal. In the filter you can access the application context using WebApplicationContextUtils and store either the application context or the needed beans in the TheadLocal.

Caution: if you forget to unset the ThreadLocal you will get nasty problems when trying to undeploy the application! Thus, you should set it and immediately start a try that unsets the ThreadLocal in the finally-part.

Of course, this still uses a singleton: the ThreadLocal. But the actual beans do not need to be anymore. The can even be request-scoped, and this solution also works if you have multiple WARs in an Application with the libaries in the EAR. Still, you might consider this use of ThreadLocal as bad as the use of plain singletons. ;-)

Perhaps Spring already provides a similar solution? I did not find one, but I don't know for sure.

hstoerr