Spring get current ApplicationContext

LynAs picture LynAs · Feb 17, 2014 · Viewed 310.9k times · Source

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

Answer

gipinani picture gipinani · Feb 17, 2014

Simply inject it..

@Autowired
private ApplicationContext appContext;

or implement this interface: ApplicationContextAware