Getting Spring Application Context

spring get application context in main method
spring boot configuration get application context
spring get environment from application context
get application context in non spring class
spring get current web application context
get application context from request
spring application context load-on startup
how application context works internally in spring

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 through 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?)

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.

Static way to get 'Context' in Android?, To get a reference to the ApplicationContext in a Spring application, it can easily be achieved by implementing the ApplicationContextAware interface. Spring will automatically detect this interface and inject a reference to the ApplicationContext: view rawMyBeanImpl. java hosted by GitHub. ApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code.

You can implement ApplicationContextAware or just use @Autowired:

public class SpringBean {
  @Autowired
  private ApplicationContext appContext;
}

SpringBean will have ApplicationContext injected, within which this bean is instantiated. For example if you have web application with a pretty standard contexts hierarchy:

main application context <- (child) MVC context

and SpringBean is declared within main context, it will have main context injected; otherwise, if it's declared within MVC context, it will have MVC context injected.

Spring ApplicationContext, Getting ApplicationContext. To access the application context, we can autowire the ApplicationContext interface or implement the  Spring ApplicationContext Container. The Application Context is Spring's advanced container. Similar to BeanFactory, it can load bean definitions, wire beans together, and dispense beans upon request. Additionally, it adds more enterprise-specific functionality such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners.

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.

This page gives an example to get spring application context object with in non spring managed classes as well. It is not possible to have all classes as spring  Questions: 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 through the call stack to any classes that need it, or is there a way for a class to ask

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.

This article shows you three different ways how to get to the Spring Framework Application Context in your code. Summary (This is a repost of  In the case of spring web application we have a utility class provided by spring framework called WebApplicationContextUtils. We can use this utility to get application context but we need to provide the respective servletcontext as parameter.

Here is how to get the ApplicationContext in a spring bean. It can be used to look up other spring beans or search out beans based on their  It is not possible to have all classes as spring managed classes, in such classes you need to get spring application context object. This can be achieved by using ApplicationContextAware interface. Here are the steps to achieve application context object: Create a new class and implement ApplicationContextAware method and its unimplemented method as shown below:

How to access ApplicationContext inside a java bean? To get access to ApplicationContext we should implement ApplicationContextAware interface in the  So your actually writing code in your business objects that Spring can do for you. This type of code is of no real value to your business objects. Spring provides you with the ability to keep this kind of plumbing code out of your business objects letting the developers focus on the business logic.

Spring ApplicationContext Container - The Application Context is Spring's step is used to get the required bean using getBean() method of the created context. In the Application, we create a bean, call its method and set up the Spring Boot application. The CommandLineRunner interface indicates that a bean should run when it is contained within a SpringApplication .

Semiconductor. This article shows you three different ways how to get to the Spring Framework Application Context in your code. Inheritance from a parent context. Definitions in a descendant context will always take priority. This means, for example, that a single parent context can be used by an entire web application, while each servlet has its own child context that is independent of that of any other servlet. ApplicationContext vs BeanFactory:

Comments
  • 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.
  • @Don Kirkby Using the singleton pattern means instanciating your container class from a static method within your container class... once you "manually" instanciate an object it is not managed by Spring anymore : how did you tackle this problem ?
  • My memory is a little vague after nine years, @Antonin, but I don't think the singleton was managed within the Spring container. I think the singleton's only job was to load the container from an XML file and hold it in a static member variable. I didn't return an instance of its own class, it returned an instance of the Spring container.
  • Thanks Don Kirkby, a Spring singleton owning a static reference to itself, thus usable by non Spring objects maybe.
  • That might work, @Antonin, if you told the Spring container to use the singleton's instance() method as a factory. However, I think I just let all the code outside the container access the container first. Then that code could request objects from the container.
  • This helped a bunch. I've got some odd issues with an older app with Spring 2.0 and your answer was the only way I could sanely get things to work with a single ApplicationContext, with a single Spring IoC container.
  • Readers..Don't forget to declare this SpringBean in your springconfig.xml as a bean.
  • What if this is already a Bean, and I use Application.getApplicationContext()(Singleton pattern) , which returns a instance of new XXXXApplicationContext(XXXX), why it is not work? Why Do I have to autowired it?
  • You can use @Inject too
  • That approach can fail if you call getBean from code that runs during a Unit test because the Spring context will not be set up before you ask for it. Its a race condition I just slammed into today after 2 years of successfully using this approach.
  • I'm running into the same thing.. not from a unit test but from a database trigger.. any suggestions ?
  • Excellent response. Thank you.
  • 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.