Stefan Gloutnikov
Stefan Gloutnikov

Reputation: 448

Where to start with adding Spring to GWT back-end?

I have a GWT application in which I would like to add Spring, and mainly use Spring with JdbcTemplate to handle my database connection and DAO model. I am fairly experienced working with GWT but am just starting to learn Spring.

What is not clear to me is where and how I would initialize spring in my GWT application? I need to initialize an application context somewhere I assume. Is it possible to do this with an RPC method invoked from onModuleLoad() in the front-end, but will the objects still be available later on if I do that?

Basically I am looking for a basic overview of how and where to initialize Spring in the back-end so that I can start developing and experimenting with Springs components.

Thank You!

Upvotes: 2

Views: 838

Answers (1)

Tahir Akhtar
Tahir Akhtar

Reputation: 11625

Several years ago, I did a Spring-GWT integration with roughly the following steps:

  1. Configure a Spring ContextLoaderListener in web.xml to start up Spring's root WebApplicationContext.
  2. Create a base RemoteServiceServlet class that have methods for acquiring spring ApplicationContext from ServletContext. This can be done using something like following:WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

  3. Make all the RPC servlets extends this base class.

  4. Service methods now can acquire spring's ApplicationContext easily. You can have your service and data layer beans configured in the spring context and made available to your RPC servlet this way.

The only problem with this approach was that the RPC servlet are not them-self not created by Spring so you cannot have their dependencies injected.

Now you can employ third-party frameworks for spring-gwt integration. For example GWT Platform's Dispatch module lets you configure your action handler's as spring beans. Such solutions basically employ a single RPC servlet provided by the framework. This framework RPC servlet is responsible for instantiating your server-side handlers and invoking them. If you are willing to learn these additional frameworks, they might be better long-term solutions than the one I outlined above.

Upvotes: 1

Related Questions