About Joonas Javanainen

Joonas is a passionate software developer from Finland. He has worked on a multitude of problem domains, and focuses professionally on functional programming and JVM-based technologies.

Spring vs Guice: The one critical difference that matters

Spring objects are recognized based on their names
It doesn’t matter whether you use XML or Java config, a Spring scope is roughly like a Map<String, Object> structure. This means that you cannot have two objects with the same name. Why is this a bad thing? If you have a large application with lots of @Configuration classes or XML files, it’s very easy to accidentally use the same name twice.
The worst part of this is that using the same with several objects, they silently override each other until only one actually remains in the ApplicationContext. These objects can also be of different types and the declaration order is what really determines which object wins. The problem here is that if you want to make reusable modules based on Spring, you will basically be forced to use a prefix in the name or something else to ensure you won’t have a name clash.
Guice objects are recognized based on their classes
A Guice scope is basically like a Map<Class<?>, Object> structure. This means that you cannot have two objects of the same type without using extra metadata (e.g. qualifiers). This design choice has different pros and cons, but overall I think it’s the saner one. If you create reusable modules, you mostly have to make sure that you don’t export any objects of common types (e.g. String). With type-based scopes you can always create a wrapped class for common types, while with name-based scopes you would always have to use unique names based on lucky guesses. Guice also has PrivateModules so you can use Guice for all injection, but only export some of the objects in the scope.  
Example code
Here’s a naive example of a Spring application that breaks runtime because of silent bean overriding. 

Main.java

This class instantiates the application context, registers the configuration classes and tries to get a MyBean from the context.

package springbreak;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
  public static void main(String[] args) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

    ctx.register(GoodConfig.class);
    ctx.register(EvilConfig.class);

    ctx.refresh();
    ctx.start();

    System.out.println(ctx.getBean(MyBean.class).getValue());

    ctx.stop();
  }
}

MyBean.java

This is just an example type of bean that we expect to get from the application context.

package springbreak;

public interface MyBean {
  String getValue();
}

GoodConfig.java

This is a configuration class that exports a MyBean

package springbreak;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GoodConfig {

  private static class MyBeanImpl implements MyBean {
    public String getValue() {
      return "I'm a bean";
    }
  }

  @Bean
  public MyBean myBean() {
    return new MyBeanImpl();
  }

}

EvilConfig.java

This configuration class exports a String with the name myBean. It’s not a very realistic example but shows the basic idea.

package springbreak;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EvilConfig {

  @Bean
  public String myBean() {
    return "I'm a string!";
  }

}
Analyzing the example
Can you guess what happens when you run the example? Here’s the basic idea:
  1. GoodConfig exports a MyBeanImpl with the name “myBean”
  2. EvilConfig exports a String with the name “myBean” replacing the one from GoodConfig even though the types won’t match
  3. Main gets a NoSuchBeanDefinitionException “No unique bean of type [springbreak.MyBean] is defined”
    So, basically a MyBeanImpl is replaced by a String and there won’t be a bean that implements MyBean. The worst part is that if you reverse the @Configuration class registration order, the code will work because then a String will be replaced by a MyBeanImpl. Now, imagine you have 20 nicely encapsulated modules with potentially conflicting names…I’ve banged my head against the wall several times trying to debug problems in a situation like that.
    Spring (as of version 3.0.6) offers no possibility to alter the naming of @Configuration class exported beans. If you want to create safely reusable modules, you will have to use some kind of fully qualified names in the methods that export beans (e.g goodConfigMyBean, evilConfigMyBean).
    I like Spring (especially non-DI-container parts), but in new projects I will refuse to use a library that is so fundamentally broken. And yes, using the same name twice is a developer error, but any library that is prone to such errors can be considered worse than an alternative that attempts to minimize them.

    Reference: Spring vs Guice: The one critical difference that matters from our JCG partner Joonas Javanainen at the Jawsy Solutions technical blog blog.

    Related Whitepaper:

    Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

    Get ready to program in a whole new way!

    Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

    Get it Now!  

    3 Responses to "Spring vs Guice: The one critical difference that matters"

    1. Ray DeCampo says:

      Interesting take, I have to say while I was reading my immediate reaction was that the Guice way was inferior. I have existing Spring projects that re-use the same class with different parameters – sounds like that would be difficult with Guice (or perhaps I misunderstand). As to the supposed scoping nightmare, Java already has to deal with it and does so with packages; there’s no reason one couldn’t do the same in the Spring context. So your concern seems overblown.

      But like I said that is my first reaction and I haven’t had the same experience as you have with Spring name conflicts.

    2. Nicolas says:

      “Spring (as of version 3.0.6) offers no possibility to alter the naming of @Configuration class exported beans.”

      Even with version 3.0.6, you can alter the name with the “name” attribute of bean annotation:

      @Bean(name={“b1″”}) // bean available as ‘b1′, but not ‘myBean’
      public MyBean myBean() {
      // instantiate and configure MyBean obj
      return obj;
      }

    3. Kurt says:

      I wonder why Spring doesn’t have an option to blow up (by default) when it encounters duplicate declarations. Failing fast at runtime would be preferable to this situation.

    Leave a Reply


    four + = 6



    Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
    All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
    Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
    Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

    Sign up for our Newsletter

    20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

    As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

    • Fresh trends
    • Cases and examples
    • Research and insights
    • Two complimentary e-books