Spring get current ApplicationContext
Asked Answered
T

11

130

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

Tauten answered 17/2, 2014 at 11:14 Comment(1)
@Autowired MyClass myClass should do the job!Rondi
A
189

Simply inject it..

@Autowired
private ApplicationContext appContext;

or implement this interface: ApplicationContextAware

Agitation answered 17/2, 2014 at 11:17 Comment(5)
Maybe this can work: #11683358Agitation
The following ApplicationContextProvider.java answer looks to be the most reliable solution for this.Wadleigh
It's returning NULL everytime. To mention here that i am doing this inside a normal class that is neither a "@RestController" nor a "@Component"Johnsson
According to Spring documentation, it's best to avoid @Autowired due to some issues. Here is the link spring.io/understanding/application-context. The best option is to go with implementing the ApplicationContextAware interface.Computation
I am not sure how does this answers the question. You cannot auto wire the application context in the object not managed by Spring.Cordate
J
112

I think this link demonstrates the best way to get application context anywhere, even in the non-bean class. I find it very useful. Hope its the same for you. The below is the abstract code of it

Create a new class ApplicationContextProvider.java

package com.java2novice.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ApplicationContextProvider implements ApplicationContextAware{

    private static ApplicationContext context;

    public static ApplicationContext getApplicationContext() {
        return context;
    }

    @Override
    public void setApplicationContext(ApplicationContext ac)
            throws BeansException {
        context = ac;
    }
}

Add an entry in application-context.xml

<bean id="applicationContextProvider"
                        class="com.java2novice.spring.ApplicationContextProvider"/>

In annotations case (instead of application-context.xml)

@Component
public class ApplicationContextProvider implements ApplicationContextAware{
...
}

Get the context like this

TestBean tb = ApplicationContextProvider.getApplicationContext().getBean("testBean", TestBean.class);

Cheers!!

Judaist answered 9/2, 2015 at 11:4 Comment(4)
I coded similar to Vivek. But I avoid creating new ApplicationContextProvider() everytime I need to call the getBean() from the context. What I did was to have static ApplicationContextProvider.getApplicationContext() method. Then, when it is time to need the current app context, I invoke: ApplicationContextProvider appContext = ApplicationContextProvider.getApplicationContext()Hulk
Yes Panini Luncher, that would still be good. As per your suggestion i will change it that way. :)Judaist
Add @Component on ApplicationContextProvider can avoid configuration in aplication-context.xmlBacchic
Note: getter and setter of context should be synchronized. You will avoid a lot of headache specially for unit/integration-tests. In my case similar ApplicationContextProvider held old context(from previous integration test) that caused a lot of tricky bugs.Hackberry
S
54

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());

or

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

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

new ClassPathXmlApplicationContext("..."); // are you sure?

...as 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.

Shod answered 17/2, 2015 at 20:53 Comment(6)
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this) - did the job for me in the Liferay portlet action filter's init() method.Basilio
Awesome, the processInjectionBasedOnCurrentContext did all the job I needed. Many thanks @JaroslavPeriapt
ApplicationContextAware DOES work for me, when it is annotated with @Component as in Vivek's solution (I am initializing Spring context manually via extending AbstractContextLoaderInitializer / createRootApplicationContext)Stramonium
Be warned... SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); doesn't happen immediately, so you can't use it as the first line in your constructor for example.Pled
This works perfectly in a classical Java webapp (not spring-mvc managed).Skald
Where should SpringBeanAutowiringSupport be called in my class? SledgeHammer says "not first line in constructor"... if not then where?Opera
L
34

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);
Leeth answered 19/9, 2016 at 13:59 Comment(1)
It doesn't work to me. My class is out of Spring context. I've tried to use your code but it give me a null as response. I'm talking about ContextLoader.getCurrentWebApplicationContext()Seclude
W
10

Add this to your code

@Autowired
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.
@Autowired
private MyClass myClass;

This will simply inject myClass into your application

Winter answered 17/2, 2014 at 11:35 Comment(0)
B
8

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

@Component("applicationContextProvider")
public class ApplicationContextProvider implements ApplicationContextAware {

    private static class AplicationContextHolder{

        private static final InnerContextResource CONTEXT_PROV = new InnerContextResource();

        private AplicationContextHolder() {
            super();
        }
    }

    private static final class InnerContextResource {

        private ApplicationContext context;

        private InnerContextResource(){
            super();
        }

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

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

    @Override
    public void setApplicationContext(ApplicationContext ac) {
        AplicationContextHolder.CONTEXT_PROV.setContext(ac);
    }
}

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

Beverlybevers answered 4/11, 2015 at 17:30 Comment(2)
There is org.springframework.core.io.ContextResource interface. I would suggest to choose different name for inner class ContextResource just to avoid mess.Henrieta
@AlexanderRadchenko Ok, I changed it to InnerContextResourceBeverlybevers
Z
1

Even after adding @Autowire if your class is not a RestController or Configuration Class, the applicationContext object was coming as null. Tried Creating new class with below and it is working fine:

@Component
public class SpringContext implements ApplicationContextAware{

   private static ApplicationContext applicationContext;

   @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws 
     BeansException {
    this.applicationContext=applicationContext;
   }
 }

you can then implement a getter method in the same class as per your need like getting the Implemented class reference by:

    applicationContext.getBean(String serviceName,Interface.Class)
Zlatoust answered 7/7, 2020 at 14:32 Comment(0)
R
0

Step 1 :Inject following code in class

@Autowired
private ApplicationContext _applicationContext;

Step 2 : Write Getter & Setter

Step 3: define autowire="byType" in xml file in which bean is defined

Rodin answered 25/12, 2014 at 4:15 Comment(0)
C
0

Another way is to inject applicationContext through servlet.

This is an example of how to inject dependencies when using Spring web services.

<servlet>
        <servlet-name>my-soap-ws</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
        <init-param>
            <param-name>transformWsdlLocations</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:my-applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>5</load-on-startup>

</servlet>

Alternate way is to add application Context in your web.xml as shown below

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/classes/my-another-applicationContext.xml
        classpath:my-second-context.xml
    </param-value>
</context-param>

Basically you are trying to tell servlet that it should look for beans defined in these context files.

Cheryle answered 7/8, 2015 at 14:6 Comment(0)
A
0

Even better than having it with @Autowired is to let it be injected via constructor. Find some arguments pro constructor injection here

@Component
public class MyClass{
  private final ApplicationContext applicationContext;

  public MyClass(ApplicationContext applicationContext){
    this.applicationContext = applicationContext;
  }

  //here will be your methods using the applicationcontext
}
Awlwort answered 10/12, 2020 at 10:52 Comment(0)
B
0

If your main() method has SpringApplication.run() method then you should remember ,it by default, returns the ApplicationContext object. See this. Meaning,you can do this:

ApplicationContext context=SpringApplication.run(YourMainMethodClassName.class,args);
MyClass obj=(MyClass)context.getBean(MyClass.class);
Balloon answered 7/2, 2023 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.