Question

I'm wondering if it's only me or are most examples of Spring's Java Configuration flawed? Like here for example:

http://spring.io/blog/2008/03/27/spring-java-configuration-what-s-new-in-m3 http://spring.io/blog/2013/07/18/javaconfig-support-in-the-spring-tool-suite

Notice how they inject beans? They use methods directly, like: new OrderRepository(dataSource()) here:

@Configuration
public class ApplicationConfig {

    public @Bean OrderRepository orderRepository() {
        return new OrderRepository(dataSource());
    }

    public @Bean DataSource dataSource() {
        // instantiate and return an new DataSource ...
    }
}

This got me thinking - wouldn't it create two objects if used twice? Effectively bypassing singleton guarantee of Spring? Why aren't they injecting beans instead? As dependency framework was designed to work in the first place.

Let's do a quick test. Take those two classes for example - TestParent and TestedChild. Parent accepts a child: new TestParent(new TestedChild()). We will make them singleton beans in a minute.

public class TestedChild { }

public class TestParent {

    private TestedChild child;

    public TestParent(TestedChild child) {
        this.child = child;
    }

    public TestedChild getChild() { return child; }

}

What I want to see is if we can in fact get two different instances of TestedChild created during context initialization. Let's configure our singleton beans now:

@Configuration
public class TestInjectionConfig {

    @Bean(name = "injected")
    public TestParent injected(TestedChild bean) {
        return new TestParent(bean);
    }

    @Bean(name = "direct")
    public TestParent direct() {
        return new TestParent(testedBean());
    }

    @Bean
    public TestedChild testedBean() {
        return new TestedChild();
    }

}

I'm creating two different TestParent objects which should have the same TestedChild injected at the time of their creation.

Let's test them:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestInjectionConfig.class)
public class InjectionApplicationTest {

    @Inject @Named("injected")
    TestParent injected;

    @Inject @Named("direct")
    TestParent direct;

    @Test
    public void instancesShouldBeTheSame() {

        Assert.assertSame(injected, direct);

    }

}

I would expect the beans to be the same but this is what I'm getting on Spring 3.2.6:

junit.framework.AssertionFailedError: expected same:<pl.aie.TestParent@59e5a42c> was not:<pl.aie.TestParent@737d72cf>

Back to the question. Why are the examples using direct method calls on Spring configuration classes? And if they are meant to be called like that, why are they annotated with a @Bean annotation? Is it a bad practice or is my logic/code flawed somewhere?

Was it helpful?

Solution

In your test, you are comparing the two TestParent beans, not the single TestedChild bean.

Also, Spring proxies your @Configuration class so that when you call one of the @Bean annotated methods, it caches the result and always returns the same object on future calls.

See here:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top