DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. An Opinionless Comparison of Spring and Guice

An Opinionless Comparison of Spring and Guice

One developer maps out the features of both Spring and Guide from a dependency injection angle to see which one comes out on top.

Nikhil Wanpal user avatar by
Nikhil Wanpal
·
Oct. 04, 16 · Opinion
Like (28)
Save
Tweet
Share
37.92K Views

Join the DZone community and get the full member experience.

Join For Free

Recently, I had to delve into the play framework for a particular microservice at work. Now it is not exactly new, nor is Guice, nor DI, but coming from Spring world it was still kind of a shift in approach. There is a lot of documentation comparing Spring with Guice, stating which is better, why, and how.

In general, these articles discuss specific points where these two frameworks differ in their approaches and which approach seems better to the author. I am not sure these articles really help someone trying to take a dip in the other framework. We know the differing opinions, as they are stated by the authors of the respective frameworks in their own documentation as well.

Another person (the article’s author) reiterating it with an incomplete comparison of these frameworks does not sound helpful. What would work much better is a direct mapping of features, without the author’s opinion (didn’t this sound like an opinion?). That should help someone getting into Spring from Guice or vice versa.

Now let me warn you, since these are different frameworks for the same purpose, DI (Dependency Injection), they exist for their differences. Hence, there cannot be one-to-one mapping of features/differences in these frameworks. What we can get instead is mapping of similar features. If nothing else, the comparison below should help someone find the right documentation for what they are trying to do, instead of wondering what to look for.

To another point, we are here discussing Spring and Guice only on their dependency injection approaches and not as web frameworks, AOP, JPA abilities, their ecosystem or any other features they provide. That is for another time maybe, but not today.

SpringGuice

Application level @Configuration

Extend AbstractModule, comes closest to that. It defines a part of your application; multiple modules can depend on each other in an application. (Unless your service is too small)

@ComponentScan

There is no classpath scanning in Guice. (keep reading...)

@Component, @Service etc.

@Singleton with “bind() with/without .to()” in Module

@Scope(“”); singleton (DEFAULT), prototype, request, session, global-session

Default is unscoped, similar to prototype in Spring, @Singleton, @SessionScoped, @RequestScoped, custom; eager/lazy differ for production and development.

@Autowired, @Inject

@Inject (from javax or from guice package)

@Qualifier("name")

@Qualifier / @Named, Names.named, annotatedWith and @BindingAnnotation

@Bean

@Provides or implement Provider<T>        

@Bean with @Autowired field in it

Explicit constructor binding: .toConstructor(A.class.getConstructor())

@Value

@Named with Names.bindProperties() in your module

Injecting static fields can be achieved with @Autowired on non-static setter method.

For static fields, use .requestStaticInjection() in your Module

ApplicationContext (BeanFactory to be precise)

Injector

@Autowired with context.getBean(Clazz, Object...)

@AssistedInject. Allows for using params, with injected beans to instantiate objects.

@Lookup

Provider<T> with FactoryProvider; FactoryModuleBuilder

@PostConstruct, @PreDestroy

No Support for lifecycle events (extensions)


Let’s also see a few more points, which would not fit well in a tabular form:

  • One can add more capabilities to Guice with plugins, and there are a few actively maintained ones like Governator from Netflix. Spring DI can be extended using BeanPostProcessor or BeanFactoryPostProcessor in your application, but I was unable to find a plugin for extending Spring’s core DI abilities.

  • Unlike Spring, wiring in Guice (called binding) is plain Java, and so Guice has compile-time verification of any wiring we do. Spring depends on metadata through annotations, which are not checked during compilation. It does not have this feature, and exceptions are at runtime.

  • Classpath scanning can be achieved in Guice by extending it. (Some plugins provide this, but Governator, for one, has deprecated it.)

  • Lack of classpath scanning in Guice, most likely, considerably reduces the application startup time in comparison to Spring.

  • In Guice, an Interface can declare the default implementation class (is it odd, Spring people?) via the @ImplementedBy annotation, which can be overridden by .bind() if found in a module. Similarly, the interface can declare the configuration class, which generates the instance: @ProvidedBy.

  • I know I said we are not going to discuss any other abilities, but this one is a little interesting; Guice has built-in support for AOP, but in Spring, we need an additional dependency.

  • Not a difference, but a point to note: Both frameworks have similar injection types, Constructor, method, and field.

I have tried to be as opinionless as possible when writing the above piece; although there are a few things that I find important to note.

  • Guice is very much a non-magical (in the words of Guice authors) dependency injection framework, you can literally see DI happen, with the code that you write and can read.

  • Thankfully, Guice has no beans. NO BEANS! How many beans do we have to remember and disambiguate before it is too much? Javabeans, Enterprise Javabeans, Spring Beans, Coffee Beans, Mr. Bean, and I might still have missed a few others!

  • Guice still feels like Java. It believes in extending classes. Spring nowadays seems to believe only in annotations, so much so that a few folks I asked around can’t even remember what ‘extends’ keyword stands for!

So which one is better? Now, that was not the question we were hoping to answer!

Spring Framework Comparison (grammar) Framework Dependency injection application Relational database Play Framework Classpath (Java) Documentation Annotation

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Application Architecture Design Principles
  • Reliability Is Slowing You Down
  • gRPC on the Client Side
  • Testing Level Dynamics: Achieving Confidence From Testing

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: