Accessing a Spring bean from a servlet
I'm using Ajaxtags to implement an ajax-enabled form with dynamic dropdown fields. In order to access my DAO object (which is a Spring managed bean) to get the dropdown options, I had to have access to the Application context from a regular Servlet.
The solution is very simple, but since I had some troubles finding it, I'm adding it here for future reference, and for anyone who's looking for the same. Basically, you use the WebApplicationContextUtils object from the spring framework to get a reference to your bean. Inside the doGet() method I have:
It's that easy... really.
The solution is very simple, but since I had some troubles finding it, I'm adding it here for future reference, and for anyone who's looking for the same. Basically, you use the WebApplicationContextUtils object from the spring framework to get a reference to your bean. Inside the doGet() method I have:
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
Object myDao = context.getBean("daoBeanName");
It's that easy... really.
Categories : Tips