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.
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3117 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
parent
3478dec5f0
commit
677a3d2615
|
|
@ -1,12 +1,11 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
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">
|
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">
|
<bean id="foo" class="org.springframework.context.annotation.Spr6602Tests$Foo">
|
||||||
<constructor-arg ref="barFactory"/>
|
<constructor-arg ref="barFactory" />
|
||||||
</bean>
|
</bean>
|
||||||
|
|
||||||
<bean id="barFactory" class="org.springframework.context.annotation.BarFactory"/>
|
<bean id="barFactory" class="org.springframework.context.annotation.Spr6602Tests$BarFactory" />
|
||||||
|
|
||||||
</beans>
|
</beans>
|
||||||
|
|
|
||||||
|
|
@ -36,58 +36,67 @@ public class Spr6602Tests {
|
||||||
public void testXmlBehavior() throws Exception {
|
public void testXmlBehavior() throws Exception {
|
||||||
doAssertions(new ClassPathXmlApplicationContext("Spr6602Tests-context.xml", Spr6602Tests.class));
|
doAssertions(new ClassPathXmlApplicationContext("Spr6602Tests-context.xml", Spr6602Tests.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testConfigurationClassBehavior() throws Exception {
|
public void testConfigurationClassBehavior() throws Exception {
|
||||||
doAssertions(new AnnotationConfigApplicationContext(FooConfig.class));
|
doAssertions(new AnnotationConfigApplicationContext(FooConfig.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void doAssertions(ApplicationContext ctx) throws Exception {
|
private void doAssertions(ApplicationContext ctx) throws Exception {
|
||||||
Foo foo = ctx.getBean(Foo.class);
|
Foo foo = ctx.getBean(Foo.class);
|
||||||
|
|
||||||
Bar bar1 = ctx.getBean(Bar.class);
|
Bar bar1 = ctx.getBean(Bar.class);
|
||||||
Bar bar2 = ctx.getBean(Bar.class);
|
Bar bar2 = ctx.getBean(Bar.class);
|
||||||
assertThat(bar1, is(bar2));
|
assertThat(bar1, is(bar2));
|
||||||
assertThat(bar1, is(foo.bar));
|
assertThat(bar1, is(foo.bar));
|
||||||
|
|
||||||
BarFactory barFactory1 = ctx.getBean(BarFactory.class);
|
BarFactory barFactory1 = ctx.getBean(BarFactory.class);
|
||||||
BarFactory barFactory2 = ctx.getBean(BarFactory.class);
|
BarFactory barFactory2 = ctx.getBean(BarFactory.class);
|
||||||
assertThat(barFactory1, is(barFactory2));
|
assertThat(barFactory1, is(barFactory2));
|
||||||
|
|
||||||
Bar bar3 = barFactory1.getObject();
|
Bar bar3 = barFactory1.getObject();
|
||||||
Bar bar4 = barFactory1.getObject();
|
Bar bar4 = barFactory1.getObject();
|
||||||
assertThat(bar3, is(not(bar4)));
|
assertThat(bar3, is(not(bar4)));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public static class FooConfig {
|
||||||
|
@Bean
|
||||||
|
public Foo foo() throws Exception {
|
||||||
|
return new Foo(barFactory().getObject());
|
||||||
|
}
|
||||||
|
|
||||||
@Configuration
|
@Bean
|
||||||
class FooConfig {
|
public BarFactory barFactory() {
|
||||||
public @Bean Foo foo() throws Exception {
|
return new BarFactory();
|
||||||
return new Foo(barFactory().getObject());
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public @Bean BarFactory barFactory() {
|
|
||||||
return new BarFactory();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Foo { final Bar bar; public Foo(Bar bar) { this.bar = bar; } }
|
|
||||||
class Bar { }
|
|
||||||
|
|
||||||
class BarFactory implements FactoryBean<Bar> {
|
|
||||||
|
|
||||||
public Bar getObject() throws Exception {
|
|
||||||
return new Bar();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Class<? extends Bar> getObjectType() {
|
public static class Foo {
|
||||||
return Bar.class;
|
final Bar bar;
|
||||||
}
|
|
||||||
|
public Foo(Bar bar) {
|
||||||
public boolean isSingleton() {
|
this.bar = bar;
|
||||||
return true;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
public static class Bar {
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue