提交 0543036e 编写于 作者: S Sam Brannen

FooConfig, Foo, Bar, and BarFactory are now public static classes in order to...

FooConfig, Foo, Bar, and BarFactory are now public static classes in order to avoid a bug with JDT/Spring IDE where the classes cannot be found in the XML application context.
上级 2ff2f020
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="foo" class="org.springframework.context.annotation.Foo">
<constructor-arg ref="barFactory"/>
<bean id="foo" class="org.springframework.context.annotation.Spr6602Tests$Foo">
<constructor-arg ref="barFactory" />
</bean>
<bean id="barFactory" class="org.springframework.context.annotation.BarFactory"/>
<bean id="barFactory" class="org.springframework.context.annotation.Spr6602Tests$BarFactory" />
</beans>
......@@ -36,58 +36,67 @@ public class Spr6602Tests {
public void testXmlBehavior() throws Exception {
doAssertions(new ClassPathXmlApplicationContext("Spr6602Tests-context.xml", Spr6602Tests.class));
}
@Test
public void testConfigurationClassBehavior() throws Exception {
doAssertions(new AnnotationConfigApplicationContext(FooConfig.class));
}
private void doAssertions(ApplicationContext ctx) throws Exception {
Foo foo = ctx.getBean(Foo.class);
Bar bar1 = ctx.getBean(Bar.class);
Bar bar2 = ctx.getBean(Bar.class);
assertThat(bar1, is(bar2));
assertThat(bar1, is(foo.bar));
BarFactory barFactory1 = ctx.getBean(BarFactory.class);
BarFactory barFactory2 = ctx.getBean(BarFactory.class);
assertThat(barFactory1, is(barFactory2));
Bar bar3 = barFactory1.getObject();
Bar bar4 = barFactory1.getObject();
assertThat(bar3, is(not(bar4)));
}
}
@Configuration
public static class FooConfig {
@Bean
public Foo foo() throws Exception {
return new Foo(barFactory().getObject());
}
@Configuration
class FooConfig {
public @Bean Foo foo() throws Exception {
return new Foo(barFactory().getObject());
}
public @Bean BarFactory barFactory() {
return new BarFactory();
@Bean
public BarFactory barFactory() {
return new BarFactory();
}
}
}
class Foo { final Bar bar; public Foo(Bar bar) { this.bar = bar; } }
class Bar { }
public static class Foo {
final Bar bar;
class BarFactory implements FactoryBean<Bar> {
public Bar getObject() throws Exception {
return new Bar();
public Foo(Bar bar) {
this.bar = bar;
}
}
public Class<? extends Bar> getObjectType() {
return Bar.class;
public static class Bar {
}
public boolean isSingleton() {
return true;
public static class BarFactory implements FactoryBean<Bar> {
public Bar getObject() throws Exception {
return new Bar();
}
public Class<? extends Bar> getObjectType() {
return Bar.class;
}
public boolean isSingleton() {
return true;
}
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册