我想知道它是否只是我或者是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-the-spring-tool-suite
注意他们如何注入豆子?他们直接使用方法,例如:new OrderRepository(dataSource())
:
@Configuration
public class ApplicationConfig {
public @Bean OrderRepository orderRepository() {
return new OrderRepository(dataSource());
}
public @Bean DataSource dataSource() {
// instantiate and return an new DataSource ...
}
}
这让我思考 - 如果两次使用它会不会创建两个对象?有效地绕过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; }
}
我想看到的是,如果我们实际上可以获得在上下文初始化期间创建的两个不同的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();
}
}
我正在创建两个不同的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);
}
}
我希望这些bean是相同的,但这是我在Spring 3.2.6上得到的:
junit.framework.AssertionFailedError: expected same:<pl.aie.TestParent@59e5a42c> was not:<pl.aie.TestParent@737d72cf>
回到问题。为什么示例使用直接方法调用Spring配置类?如果它们被称为那样,为什么它们用@Bean注释注释?这是一种不好的做法还是我的逻辑/代码在某处有缺陷?
答案 0 :(得分:5)
在测试中,您正在比较两个TestParent
bean,而不是单个TestedChild
bean。
此外,Spring代理您的@Configuration
类,以便在调用其中一个@Bean
带注释的方法时,它会缓存结果并始终在将来的调用中返回相同的对象。
见这里: