[java] Are all Spring Framework Java Configuration injection examples buggy?

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?

This question is related to java spring dependency-injection spring-java-config

The answer is


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:


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to spring

Are all Spring Framework Java Configuration injection examples buggy? Two Page Login with Spring Security 3.2.x Access blocked by CORS policy: Response to preflight request doesn't pass access control check Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified Spring Data JPA findOne() change to Optional how to use this? After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName The type WebMvcConfigurerAdapter is deprecated No converter found capable of converting from type to type

Examples related to dependency-injection

Are all Spring Framework Java Configuration injection examples buggy? Passing data into "router-outlet" child components ASP.NET Core Dependency Injection error: Unable to resolve service for type while attempting to activate Error when trying to inject a service into an angular component "EXCEPTION: Can't resolve all parameters for component", why? org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demoRestController' How to inject window into a service? How to get bean using application context in spring boot Resolving instances with ASP.NET Core DI from within ConfigureServices How to inject a Map using the @Value Spring Annotation? WELD-001408: Unsatisfied dependencies for type Customer with qualifiers @Default

Examples related to spring-java-config

Are all Spring Framework Java Configuration injection examples buggy? disabling spring security in spring boot app How To Inject AuthenticationManager using Java Configuration in a Custom Filter