tags:

views:

2142

answers:

6

I googled but couldn't find any answer.

I am planning to use GWT. I want to know if I can use spring in my GWT code to use the dependency injection framework? I am not talking about GWT gui interaction with backend spring app.

The reason I am asking is the GWT code gets compiled to JavaScript and this is what gets executed in browser. If I am using spring code in that, then would it work or for that matter any other library like log4j, etc.?

Or the GUI code have to be pure GWT API only?

For example,

public class MyTable {
   private Button myButton;
   @Autowired
   public MyTable(Button aMyButton) {
      myButton = aMyButton;
   }
}
+3  A: 

No, you won't be able to do that. The DI logic applies at runtime on the server side, and the GWT code is entirely client-side.

skaffman
+1  A: 

I wonder if Guice (the Google DI framework) is supported by GWT?

This might be an alternative.

Fortyrunner
+8  A: 

Guice is supported on GWT using GIN. For Spring-like DI with GWT, check out GWT Toolbox or Rocket GWT.

I believe GIN is a more natural choice for GWT. Not because it's also made by Google, but because using XML for GWT configuration makes absolutely no sense. Everything gets statically compiled into JavaScript so there is no need for externalized configuration. Keep your refactoring tools happy; go for GIN.

To answer your other question, you will not find many SE frameworks that work on GWT. First and foremost, it has no support for reflection or bytecode manipulation (everything is JavaScript), which immediately rules out a lot of frameworks. Log4j, on the other hand, doesn't make sense because there is no file system accessible on the client side, but there are libraries available that do things differently.

The Spring libraries for GWT mentioned above are basically a rewrite of the Spring for GWT. They do not share any code with Spring simply because they can't. Those frameworks work by generating code ("factories") that wire up your components as if you were doing DI manually.

This is also how GIN works, it generates Java factories for your classes, and GWT compiles it into optimized JavaScript (meaning little performance overhead). GIN does use Guice behind the scenes though, to validate configuration at compile time and to inspect modules.

RobbieV
+3  A: 

I thought it would be simpler to just create a Spring Controller that invoked the doPost method of the GWT RemoteServlet. A sample is provided here. I know this is a little round about. But this shields you from changes to the GWT implementation if any.. Hope it helps.

The question is about client-side DI, not on the RemoteServlet side.
Robert Munteanu
@Robert Agreed; however, it is exactly what I needed for MY question, so I'm glad kiran posted!
tpierzina
A: 

Spring ME is capable of helping you out here. Although I partly agree with some of the previous responses, it's nice to have the same programming (and plumbing) paradigm across your client and server code.

Wilfred Springer
A: 

You can implement a Servlet Service in the server side that retrive objects from a Spring ApplicationContext, rendering to JSon Objects (I did it with http://json-lib.sourceforge.net/apidocs/net/sf/json/JSONSerializer.html) by example. Then you can have a Singleton Facade Service that make the request from the GWT-client side to our Servlet Service.

In this way you can get a runtime depency injection in the GWT-client side .

Chech0x