Spring get current ApplicationContext


I am using Spring MVC for my web application. My beans are written in "spring-servlet.xml" file

Now I have a class MyClass and i want to access this class using spring bean

In the spring-servlet.xml i have written following

<bean id="myClass" class="com.lynas.MyClass" />

Now i need to access this using ApplicationContext

ApplicationContext context = ??

So that I can do

MyClass myClass = (MyClass) context.getBean("myClass");

How to do this??

2/17/2014 11:14:49 AM

Accepted Answer

Simply inject it..

private ApplicationContext appContext;

or implement this interface: ApplicationContextAware

10/9/2014 6:56:09 PM

In case you need to access the context from within a HttpServlet which itself is not instantiated by Spring (and therefore neither @Autowire nor ApplicationContextAware will work)...

WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());



As for some of the other replies, think twice before you do this:

new ClassPathXmlApplicationContext("..."); // are you sure? this does not give you the current context, rather it creates another instance of it for you. Which means 1) significant chunk of memory and 2) beans are not shared among these two application contexts.


If you're implementing a class that's not instantiated by Spring, like a JsonDeserializer you can use:

WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
MyClass myBean = context.getBean(MyClass.class);

Add this to your code

private ApplicationContext _applicationContext;

//Add below line in your calling method
MyClass class = (MyClass) _applicationContext.getBean("myClass");

// Or you can simply use this, put the below code in your controller data member declaration part.
private MyClass myClass;

This will simply inject myClass into your application


based on Vivek's answer, but I think the following would be better:

public class ApplicationContextProvider implements ApplicationContextAware {

    private static class AplicationContextHolder{

        private static final InnerContextResource CONTEXT_PROV = new InnerContextResource();

        private AplicationContextHolder() {

    private static final class InnerContextResource {

        private ApplicationContext context;

        private InnerContextResource(){

        private void setContext(ApplicationContext context){
            this.context = context;

    public static ApplicationContext getApplicationContext() {
        return AplicationContextHolder.CONTEXT_PROV.context;

    public void setApplicationContext(ApplicationContext ac) {

Writing from an instance method to a static field is a bad practice and dangerous if multiple instances are being manipulated.


There are many way to get application context in Spring application. Those are given bellow:

  1. Via ApplicationContextAware:

    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    public class AppContextProvider implements ApplicationContextAware {
    private ApplicationContext applicationContext;
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;

Here setApplicationContext(ApplicationContext applicationContext) method you will get the applicationContext

  1. Via Autowired:

    private ApplicationContext applicationContext;

Here @Autowired keyword will provide the applicationContext.

For more info visit this thread

Thanks :)