I'm starting a new Java web app from scratch.
I don't have much experience on Spring Framework, but I know I'd like to use some of its features, such as Transaccions Management.
On the other hand, I really like Guice for dependency injection.
I know that Guice and Spring can work together: http://www.jroller.com/mindcrime/entry/an_example_of_integrating_guice
But before starting designing my application, I wanted to know if someone had experienced issues by following that approach.
Also, what I really like from Guice is that you don't need an XML configuration file, but just java Modules, which shorter and are easier to read. Is there any alternative to XML configuration files on Spring, similar to Guice?
I think Spring alone is good enough for enterprise application.
Spring dosent need XML too!!! Modern Spring Apps uses JavaConfig and minimal configuration. Take look at Spring Boot Guides. Whole Spring apps can not use any XML at all.
Guice is nice, but very limited. With Spring is possible to write web application or REST application with transactions and persistance very easy and fast. With Guice this is more complicated.
If you're just starting then I'll recommend you using https://github.com/spring-projects/spring-boot
It has great autoconfiguration feature and saves writing boilerplate code. I even can release you from using application server due to embedded Tomcat. For example implementing simple MVC controller (which can be used as REST endpoints) looks like that:
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
Now you can execute java -jar your_package.jar
and thats all. You will also get transaction management, database integration, etc. More examples can be found in mentioned link, especially in https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples directory
Spring has had Java-based annotation config for two major releases now. You don't need to write a single line of XML (not even a web.xml!)
I've worked with Guice and Spring. Guice is sufficient for smaller projects that need DI, but if you're going to be using Spring for MVC or transactional support you might as well just use its DI as well. Guice also doesn't have good profile support the way Spring does -- you have to do your own manual switching of modules if you want to have separate beans for local development, test environments, and production.
Google Guice almost has 1:1 mapping for all the Spring Dependency Injection concepts but It has a few more cool things:
1. It's all in Java. Google Guice contains all the configurations in Java code, so we don't have to deal with any xml configurations or anything.
2. Better error messages. Guice has an interceptor that cleans up and rethrows nicer stack traces. Spring just spits out everything.
3. Just-in-time binding (or Implicit binding). This means if you have a zero-arg constructor, there's no additional binding needed, just inject it! Same if you're trying to inject a constructor with multiple args that are either zero-arg constructors, or already injected, there is no need for additional configuration. Again, just inject it!
4. Eager/Lazy injection When you inject with Spring, it says I will inject nothing or all the things. There is an option in Guice to inject lazily, which means only create the subsection of the dependency graph that I need. This means a few things - you don't need those weird special Spring testing files, Guice injects way faster in testing, and you can run integration tests in Eclipse with minimal to no setup!
5. Binding by types. This is different from Spring, which binds by names. In Spring, if you accidentally bind two instances to the same name, Spring will fail silently, and will confuse you even more by taking the binding which came last (yech). Spring offers a "bind by type" option, but don't let it fool you - its underlying implementation is still a String.
Source: https://github.com/google/guice
I would also suggest you take a look at HK2 for a light-weight J2SE DI engine. It is similar to Guice and CDI and has many extensibility features that you might find interesting. It also works with Guice and Spring via bi-directional bridges. You can start learning about HK2 here: http://hk2.java.net/. Information on the Guice bridge is here: https://hk2.java.net/2.2.0-b27/guice-bridge.html. Information on the Spring bridge is here: https://hk2.java.net/2.2.0-b27/spring-bridge.html.
The decision of which DI provider to use is based a lot on the special features needed by your application.