是否所有Spring Framework Java Configuration注入示例都有问题?

Mik*_*cki 1 java spring dependency-injection spring-java-config

我想知道它是否只是我或者是Spring的Java配置的大多数例子有缺陷吗?比如这里:

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最弹簧工具套件

注意他们如何注入豆子?他们直接使用方法,例如: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 ...
    }
}
Run Code Online (Sandbox Code Playgroud)

这让我想到 - 如果两次使用它会不会创建两个对象?有效地绕过Spring的单身保证?他们为什么不注入豆类呢?因为依赖框架被设计为首先工作.

我们来做快速测试吧.以这两个类为例--TestParent和TestedChild.家长接受一个孩子:new TestParent(new TestedChild()).我们将在一分钟内制作单身豆.

public class TestedChild { }

public class TestParent {

    private TestedChild child;

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

    public TestedChild getChild() { return child; }

}
Run Code Online (Sandbox Code Playgroud)

我想看到的是,如果我们实际上可以获得在上下文初始化期间创建的两个不同的TestedChild实例.我们现在配置我们的单例bean:

@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();
    }

}
Run Code Online (Sandbox Code Playgroud)

我正在创建两个不同的TestParent对象,它们应该在创建时注入相同的TestedChild.

我们来测试一下:

@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);

    }

}
Run Code Online (Sandbox Code Playgroud)

我希望豆子是相同的,但这是我在Spring 3.2.6上得到的:

junit.framework.AssertionFailedError: expected same:<pl.aie.TestParent@59e5a42c> was not:<pl.aie.TestParent@737d72cf>
Run Code Online (Sandbox Code Playgroud)

回到问题.为什么示例使用直接方法调用Spring配置类?如果它们被称为那样,为什么它们用@Bean注释注释?这是一种不好的做法还是我的逻辑/代码在某处有缺陷?

Sot*_*lis 5

在您的测试中,您正在比较两个TestParentbean,而不是单个TestedChildbean.

此外,Spring代理您的@Configuration类,以便在调用其中一个带@Bean注释的方法时,它会缓存结果并始终在将来的调用中返回相同的对象.

看这里: