Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring and Guice together, or just Spring

Tags:

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?

like image 645
csalazar Avatar asked Jan 10 '14 23:01

csalazar


People also ask

Does Guice work with Spring?

Guice manages its dependencies in a special class called a module. A Guice module has to extend the AbstractModule class and override its configure() method. Guice uses binding as the equivalent to wiring in Spring. Simply put, bindings allow us to define how dependencies are going to be injected into a class.

Which is better Guice or Spring?

Spring allows you to omit the @Autowired annotation when there's only one constructor. Guice allows binding to a Provider, as well as injecting a Provider of your class, even when your class has no Provider binding.

What is the relationship between Spring and spring boot?

Spring Boot is an extension of Spring, which eliminates the boilerplate configurations required for setting up a Spring application. Featuring default codes and annotation based configuration, Spring Boot enables a faster and more efficient development ecosystem.

Which dependency injection is better in Spring?

Setter-Based Dependency Injection We can combine constructor-based and setter-based types of injection for the same bean. The Spring documentation recommends using constructor-based injection for mandatory dependencies, and setter-based injection for optional ones.


2 Answers

I think Spring alone is good enough for enterprise application.

Spring doesn't 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.

like image 130
MariuszS Avatar answered Oct 01 '22 12:10

MariuszS


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. It 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

like image 31
Jakub Kubrynski Avatar answered Oct 01 '22 12:10

Jakub Kubrynski