<< Best Linux distribution for java | Home | Configuring a kernel for a laptop >>

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:
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
Object myDao = context.getBean("daoBeanName");

It's that easy... really.
Tags : , ,
Categories : Tips


Re: Accessing a Spring bean from a servlet

You do not have to use a servlet, you can use you framework action to return the result. We included the servlet as an example but in the release there is an abstract struts action an the same can be done with Spring.

Re: Accessing a Spring bean from a servlet

That was helpful, thanks for sharing!

Re: Accessing a Spring bean from a servlet

thanks, just what i needed.

Re: Accessing a Spring bean from a servlet

Great post exactly what I needed; ironically you are doing exactly what I am working on doing.

Re: Accessing a Spring bean from a servlet

Accediendo a un bean Spring desde un servlet.
Una buena solucion dificil de encontrar en la web.
Great!!, thank you

Re: Accessing a Spring bean from a servlet

This is exactly what's missing in Spring Tutorial, "Developing a Spring Framework MVC application step-by-ste".

Cool stuff.

Jiryih Tsaur

Re: Accessing a Spring bean from a servlet

justo lo que necesitaba, thank you

Re: Accessing a Spring bean from a servlet

That was really helpful Thanks

Re: Accessing a Spring bean from a servlet

Thanks!  Saved my day.

Re: Accessing a Spring bean from a servlet

Great tip!
As the others, just what I needed :D
Thanks!

Re: Accessing a Spring bean from a servlet

howto put the value back on the spring bean?

Re: Accessing a Spring bean from a servlet

Is there a way to make my servlet a bean? I can't use MVC because in my servlet I need to send data back piece by piece. Thx

Re: Accessing a Spring bean from a servlet

Works great, thanks!

Re: Accessing a Spring bean from a servlet

Thanks!

Re: Accessing a Spring bean from a servlet

Thanks a lot .......it helped :)

Re: Accessing a Spring bean from a servlet

If I'm not mistaken new instance will be created when you say context.getBean("daoBeanName"); This means, this is stateless (prototype). Please correct me is I'm wrong. If this is true how do we create a singleton instance?

Re: Accessing a Spring bean from a servlet

Re: singleton bean. See http://static.springsource.org/spring/docs/1.2.9/reference/beans.html#beans-factory-modes

Re: Accessing a Spring bean from a servlet

this information is very useful to apply it in my project. keep posting such info.

Re: Accessing a Spring bean from a servlet

Great example!

The only suggestion I have is moving the code from doGet() to init() 


Add a comment Send a TrackBack