GWT with spring dependency injection - spring

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;
}
}

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.

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.

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.

I wonder if Guice (the Google DI framework) is supported by GWT?
This might be an alternative.

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 .

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.

Related

Integrate JSF2 and Spring3 (with AOP)

I am using Spring and JSF2 a lot and was wondering what the best way to integrate them is? Now i understand there are basically two ways to do this, but i have some problems with both:
a) Use normal #ManagedBean, and inject Spring-Services into that beans using #ManagedProperty: The problem is that i can't use Spring-AOP inside a #ManagedBean obviously, because it is not Spring-managed. I usually use an arround-aspect on every method annotated with my custom annotation #DatabaseOperation. Another example would be #Secured from Spring-Security and so on. I use a lot of AOP in my project and not beeing able to use them on "the top level" is really limiting for me.
b) Use SpringBeanFacesELResolver to make everything managed by Spring. On the pro side is that AOP works like a charm, but the cons are big too:
No ViewScope. I am not sure if i can trust custom view scope implementations like this https://github.com/michail-nikolaev/primefaces-spring-scopes on productive systems?
Serialization is not possible. It's already pretty complicated, but once i use AOP i can't get it to work because org.springframework.aop.aspectj.AspectJPointcutAdvisor is not Serializable
So my question is: How do you overcome this issues? How do YOU integrate JSF2 and Spring3.x? I have been using possibility b) mostly, but on my next project i need session replication..
Any further suggestions?
The main integration pain point of the two frameworks is that JSF is usually used in a stateful way. to make the integration the most seamless, you would have to let Spring handle the statefulness and page navigations aspects himself instead of JSF, as well as bean creation and scoping.
The second important integration point is at the level of the view expression language. We want to be able to access spring beans while building the view, which means the managed bean layer is no longer needed.
The best integration available for the two frameworks is provided by introducing Spring webflow, see here for further details. With SWF, JSF no longer manages beans itself, this is done by Spring. JSF does not manage page navigation anymore, this handled in the SWF flow definition.
Also the statefulness is handled by SWF. The flow definition XML file replaces large parts of the faces-config.xml for view navigation, transition actions, bean definition, etc.
Using the SWF JSF integration means that your faces-config.xml is mostly empty. Expression language accessing directly spring beans can be used while building the view, and a view scope is available.
If you want to keep a page isolated from the rest of the application, youcan create a flow with a single view state and self-transitions. An advantage of SWF is that it prevents duplicate form submissions via a POST-REDIRECT-GET mechanism that works transparently out of the box.

Advantage of Spring

Spring is a popular framework, however I have difficulties to see in which situation the framework would actually help.
Currently I'm using the following:
* Tomcat
* Jersey
* Jackson
* Hibernate
Together this results in a Webservice, created by annotations, automatic JSON (un)marshalling and a comfortable Object/Relational Mapping.
So honestly at the moment I'm not missing anything, but I might just not know what great thing I'm missing... Could you help me out with this?
Thank you
Spring is a big framework providing a lot of functionality. It's hard to talk about advantages without knowing what functionality are you trying to use in the project.
Most probably you talk about Spring as an IoC container. It is very important part of Spring, but there is also AOP, transaction management, JDBC abstraction layer, authentication and authorization, testing and some more.
In a nutshell, Spring offers you uniform way to control dependencies between your objects. This is called inversion of control or dependency injection. Using it you can create pluggable, testable code that is easy to maintain.
In addition it gives you gazillion utility classes that just make life easier. For example, Hibernate is much easier to maintain via Spring facilities. It kind of brings together many different technologies under the same roof.

GWT/GAE Spring IoC powered

I'm trying to put together Google App Engine and Google Web Toolkit for one of my projects.
I think I'm going to use Objectify for data persistence, too.
The guys of Springsource says that integrate those technology with Spring is possible.
Do you know where can I find some sort of tutorial about that?
Spring + GAE == slow start up for every instance.
You will face performance problems.
In my project I had to get get rid of Spring once we had everything implemented :(
I wont use Spring + gae anymore
There is no any extra stuff required, no special configurations, tricks, etc. If you know both Spring and GAE - just use it, it's pretty standard.
I've few project based on Spring+GAE+Objectify+Java/Groovy - everything working fine together.
update:
Spring is good only for server-side part. As you want to use IoC on client side (in GWT part), you can use Google GIN instead. It's Google Guice framework (IoC from Google) designed for using with GWT.
See http://code.google.com/p/google-gin/

GWT Spring Integration - How to do AOP logging?

I have a GWT application where its RPC services are handled by a GWTHandler bean so that it can integrate with Spring smoothly. The application works. No problem with that.
My issue is I can't do any AOP logging with Spring. I like to log user activities from the GWT interface using AOP. (I could of course do it the old way of calling an RPC service for every action that a user does and log those action, but that is not the AOP way). I have to do it in AOP because that's the client's requirements.
I tried using the normal Spring AOP with a generic pointcut pattern "execution(* .(..))". It's able to capture all methods except for the GWT services. So in other words, it's useless. I could of course log the backend Spring DAO's using AOP but how do I know which RPC service it came from? These DAO's are used by numerous classes and methods (not exclusive to GWT).
I tried exploring GWT-ENT package. It looks good. However, it works on the client's side and your classes must implement Aspectable. This means requiring changes on all client classes on my GWT application. Furthermore, you can't use private methods since to handle AOP with GWT-ENT, you need to create your classes via GWT.create instead of the new(). Having private methods throws an error. I set-up a simple application and really private methods don't work.
I tried searching the GWT-SL package (where my GWTHandler came from). They mentioned about something about AOP, but the info is very scarce. Google didn't give me any solutions or examples.
I've tried everything I could think of and searched Google with all my efforts but I can't find a solution to my problem.
All I want to do is log methods from my GWT services via AOP. Let's say a client goes to the Report tab. Then he click on Delete button record. I want to log that activity via AOP.
I'm using GWT (with SmartGWT) and Spring/Hibernate stack.
Spring AOP will only advise public methods of beans in your Spring context, so GWT infrastructure is out unless you specifically instantiate it through the Spring container.
You could use compile-time weaving with AspectJ to wire your AOP into everything, but it might get a bit messy. It's also uncertain that it would work, unless you are compiling the GWT classes in question.
I'm gonna answer my question.
Instead of performing AOP logging on the GWT server implementations (which theoretically should work but in practice it's not), I decided to do AOP logging on the DAO layer. Just make sure you log the DAO not the Hibernate Session

What are the advantages/disadvantages of Seam over Spring?

What are the advantages/disadvantages of Seam over Spring? Why would I use Seam in lieu of Spring?
Is there anything that can be done in Seam that can't be done in Spring? Anything in Spring that can't be done in Seam?
What about stateful/stateless architecture? I am a Spring user, so I am biased, naturally.
Why Spring?
Cleaner code
Streamlined application configuration
Nice integration with popular open source products
First class AOP support
Enterprise-scale security: Acegi
Highly flexible MVC
Abstracted data access (JDBC is OK)
Enterprise Java without EJB
Testing is easy
Why Seam?
Merge Java EE 5 standards (EJB 3.0, JPA, JSF, Annotation) seamlessly
Stateful by design
Bijection
Integrated Ajax (ICEfaces and Ajax4JSF)
Business process integration (jBPM)
Business rules integration (Drools)
Workspace management
Deliver complete stack (from JBoss & RedHat)
Seam Text and EL enhancements
Probably will be a standard (JSR-299: Web Beans)
From Framework Deathmatch: Spring vs Seam. Thomas Wiradikusuma (Spring). Joshua Jackson (Seam). Java User Group Indonesia. JaMU 07.03. March 17, 2007 power point presentation here
although seam does have many advantages over spring, there is a magic word that really is worth paying attention to and this is PERFORMANCE!!! if you are not worried about performance issues I would go with seam. From the other hand if you want your application to be as fast as possible and your hardware is limited I would use spring. I am not saying that you can not develop fast applications with seam, but in order to do this you really need to know what you are doing. I have used both of them (i am not a guru in any of them) and what I found out is that although spring needs more effort to build what you want, at the end the result is more flexible and is performing better. I do not think that there is something that can be done in one framework that it can not be done in the other, saying that, remember that I am not an expert to any of those.
Seam will give you a pretty, ah, seamless, integration between the components that make up the seam stack. All very nice as long you keep within that stack, and within the seam model and foing things. It all starts to look a little less convincing as soon as you start doing something unusual, though.
If it's not too much of a generalisation, Seam is very "microsofty" in that regard. This isn't a bad thing, it's just a stylistic thing. Spring is more open-ended and takes more effort to get going, but it's ultimately more flexible, and a lot more open.
You can use Spring and Seam together - Spring for backend components, Seam for enhancement of web layer (JSF/GWT/Wicket) and other stuff. Seam offers a lot of Spring functionality (i.e. IoC container, transaction managment) - in your project you can decide - witch implementation to use.
More details on integrating Seam with Spring - "Seam in Action - free bonus chapter"
Let's compare the two.
What is common?
Both are open source, follow MVC architecture and has a servlet based front controller.
Advantages of Spring MVC
Extension of Struts.
View can be developed using JSP and HTML. You can also plugin other's like PHP or velocity.
Has large number of controllers predefined.
Integrated out of the box with Spring framework.
Advantages of Seam
Extension of JSF
View can be developed using JSF component library. There are large number of vendors to choose from.
Integrates JPA entities with Web layer
Annotation based validation
Integrates with EJB 3.0
Out the box jBPM support which provides process flow definitions.
Integrates with Drools where you can define web layer business rules.
Good community support.
Conclusion
Since Seam is built on JSF, it has large number of UI Component libraries to pick from. It reuses Java EE stack better. It has lot of interesting modules integrated beforehand.
Spring MVC is built on top of Struts and Spring, so it will reuse Spring framework stack far better than others. But the view is built using JSP, so we have to rely on JSP tag library vendors to build rich components.
Seam framework would be a better choice as Spring framework is anyway extensible enough to be leveraged by Seam.

Resources