LynAs LynAs - 2 months ago 25
Java Question

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

Answer

Simply inject it..

@Autowired
private ApplicationContext appContext;

or implement this interface: ApplicationContextAware