Resolved SPR-6602, relating to FactoryBean behavior in @Configuration classes. See issue and code comments for full details.

This commit is contained in:
Chris Beams
2010-01-18 08:54:45 +00:00
parent 1cd0a9750d
commit 4c05eaeb16
4 changed files with 165 additions and 3 deletions

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<bean id="barFactory" class="org.springframework.context.annotation.BarFactory"/>
</beans>

View File

@@ -0,0 +1,80 @@
package org.springframework.context.annotation;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Tests to verify that FactoryBean semantics are the same in Configuration
* classes as in XML.
*
* @author Chris Beams
*/
public class Spr6602Tests {
@Test
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
class FooConfig {
public @Bean Foo foo() throws Exception {
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() {
return Bar.class;
}
public boolean isSingleton() {
return true;
}
}