@Bean-returned FactoryBean proxy delegates to actual target instance now

Issue: SPR-12915
This commit is contained in:
Juergen Hoeller 2015-04-15 22:49:53 +02:00
parent 96e6406b4b
commit 1da98b0542
2 changed files with 194 additions and 140 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -289,7 +289,7 @@ class ConfigurationClassEnhancer {
} }
else { else {
// It is a candidate FactoryBean - go ahead with enhancement // It is a candidate FactoryBean - go ahead with enhancement
return enhanceFactoryBean(factoryBean.getClass(), beanFactory, beanName); return enhanceFactoryBean(factoryBean, beanFactory, beanName);
} }
} }
@ -365,11 +365,11 @@ class ConfigurationClassEnhancer {
* instance directly. If a FactoryBean instance is fetched through the container via &-dereferencing, * instance directly. If a FactoryBean instance is fetched through the container via &-dereferencing,
* it will not be proxied. This too is aligned with the way XML configuration works. * it will not be proxied. This too is aligned with the way XML configuration works.
*/ */
private Object enhanceFactoryBean(Class<?> fbClass, final ConfigurableBeanFactory beanFactory, private Object enhanceFactoryBean(final Object factoryBean, final ConfigurableBeanFactory beanFactory,
final String beanName) throws InstantiationException, IllegalAccessException { final String beanName) throws InstantiationException, IllegalAccessException {
Enhancer enhancer = new Enhancer(); Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(fbClass); enhancer.setSuperclass(factoryBean.getClass());
enhancer.setUseFactory(false); enhancer.setUseFactory(false);
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE); enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
enhancer.setCallback(new MethodInterceptor() { enhancer.setCallback(new MethodInterceptor() {
@ -378,7 +378,7 @@ class ConfigurationClassEnhancer {
if (method.getName().equals("getObject") && args.length == 0) { if (method.getName().equals("getObject") && args.length == 0) {
return beanFactory.getBean(beanName); return beanFactory.getBean(beanName);
} }
return proxy.invokeSuper(obj, args); return proxy.invoke(factoryBean, args);
} }
}); });
return enhancer.create(); return enhancer.create();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,13 +19,17 @@ package org.springframework.context.annotation;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import static org.junit.Assert.*;
/** /**
* Tests cornering bug SPR-8514. * Tests cornering bug SPR-8514.
* *
* @author Chris Beams * @author Chris Beams
* @author Juergen Hoeller
* @since 3.1 * @since 3.1
*/ */
public class ConfigurationWithFactoryBeanAndAutowiringTests { public class ConfigurationWithFactoryBeanAndAutowiringTests {
@ -77,138 +81,188 @@ public class ConfigurationWithFactoryBeanAndAutowiringTests {
ctx.register(WildcardParameterizedFactoryBeanInterfaceConfig.class); ctx.register(WildcardParameterizedFactoryBeanInterfaceConfig.class);
ctx.refresh(); ctx.refresh();
} }
}
@Test
public void withFactoryBeanCallingBean() {
class DummyBean { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
} ctx.register(AppConfig.class);
ctx.register(FactoryBeanCallingConfig.class);
ctx.refresh();
class MyFactoryBean implements FactoryBean<String> { assertEquals("true", ctx.getBean("myString"));
@Override }
public String getObject() throws Exception {
return "foo";
} static class DummyBean {
@Override }
public Class<String> getObjectType() {
return String.class;
} static class MyFactoryBean implements FactoryBean<String>, InitializingBean {
@Override
public boolean isSingleton() { private boolean initialized = false;
return true;
} @Override
} public void afterPropertiesSet() throws Exception {
this.initialized = true;
}
class MyParameterizedFactoryBean<T> implements FactoryBean<T> {
@Override
private final T obj; public String getObject() throws Exception {
return "foo";
public MyParameterizedFactoryBean(T obj) { }
this.obj = obj;
} @Override
public Class<String> getObjectType() {
@Override return String.class;
public T getObject() throws Exception { }
return obj;
} @Override
public boolean isSingleton() {
@Override return true;
@SuppressWarnings("unchecked") }
public Class<T> getObjectType() {
return (Class<T>)obj.getClass(); public String getString() {
} return Boolean.toString(this.initialized);
}
@Override }
public boolean isSingleton() {
return true;
} static class MyParameterizedFactoryBean<T> implements FactoryBean<T> {
}
private final T obj;
@Configuration public MyParameterizedFactoryBean(T obj) {
class AppConfig { this.obj = obj;
@Bean }
public DummyBean dummyBean() {
return new DummyBean(); @Override
} public T getObject() throws Exception {
} return obj;
}
@Configuration @Override
class ConcreteFactoryBeanImplementationConfig { @SuppressWarnings("unchecked")
@Autowired public Class<T> getObjectType() {
private DummyBean dummyBean; return (Class<T>)obj.getClass();
}
@Bean
public MyFactoryBean factoryBean() { @Override
Assert.notNull(dummyBean, "DummyBean was not injected."); public boolean isSingleton() {
return new MyFactoryBean(); return true;
} }
} }
@Configuration @Configuration
class ParameterizedFactoryBeanImplementationConfig { static class AppConfig {
@Autowired
private DummyBean dummyBean; @Bean
public DummyBean dummyBean() {
@Bean return new DummyBean();
public MyParameterizedFactoryBean<String> factoryBean() { }
Assert.notNull(dummyBean, "DummyBean was not injected."); }
return new MyParameterizedFactoryBean<String>("whatev");
}
} @Configuration
static class ConcreteFactoryBeanImplementationConfig {
@Configuration @Autowired
class ParameterizedFactoryBeanInterfaceConfig { private DummyBean dummyBean;
@Autowired
private DummyBean dummyBean; @Bean
public MyFactoryBean factoryBean() {
@Bean Assert.notNull(dummyBean, "DummyBean was not injected.");
public FactoryBean<String> factoryBean() { return new MyFactoryBean();
Assert.notNull(dummyBean, "DummyBean was not injected."); }
return new MyFactoryBean(); }
}
}
@Configuration
static class ParameterizedFactoryBeanImplementationConfig {
@Configuration
class NonPublicParameterizedFactoryBeanInterfaceConfig { @Autowired
@Autowired private DummyBean dummyBean;
private DummyBean dummyBean;
@Bean
@Bean public MyParameterizedFactoryBean<String> factoryBean() {
FactoryBean<String> factoryBean() { Assert.notNull(dummyBean, "DummyBean was not injected.");
Assert.notNull(dummyBean, "DummyBean was not injected."); return new MyParameterizedFactoryBean<String>("whatev");
return new MyFactoryBean(); }
} }
}
@Configuration
@Configuration static class ParameterizedFactoryBeanInterfaceConfig {
class RawFactoryBeanInterfaceConfig {
@Autowired @Autowired
private DummyBean dummyBean; private DummyBean dummyBean;
@Bean @Bean
@SuppressWarnings("rawtypes") public FactoryBean<String> factoryBean() {
public FactoryBean factoryBean() { Assert.notNull(dummyBean, "DummyBean was not injected.");
Assert.notNull(dummyBean, "DummyBean was not injected."); return new MyFactoryBean();
return new MyFactoryBean(); }
} }
}
@Configuration
@Configuration static class NonPublicParameterizedFactoryBeanInterfaceConfig {
class WildcardParameterizedFactoryBeanInterfaceConfig {
@Autowired @Autowired
private DummyBean dummyBean; private DummyBean dummyBean;
@Bean @Bean
public FactoryBean<?> factoryBean() { FactoryBean<String> factoryBean() {
Assert.notNull(dummyBean, "DummyBean was not injected."); Assert.notNull(dummyBean, "DummyBean was not injected.");
return new MyFactoryBean(); return new MyFactoryBean();
} }
}
@Configuration
static class RawFactoryBeanInterfaceConfig {
@Autowired
private DummyBean dummyBean;
@Bean
@SuppressWarnings("rawtypes")
public FactoryBean factoryBean() {
Assert.notNull(dummyBean, "DummyBean was not injected.");
return new MyFactoryBean();
}
}
@Configuration
static class WildcardParameterizedFactoryBeanInterfaceConfig {
@Autowired
private DummyBean dummyBean;
@Bean
public FactoryBean<?> factoryBean() {
Assert.notNull(dummyBean, "DummyBean was not injected.");
return new MyFactoryBean();
}
}
@Configuration
static class FactoryBeanCallingConfig {
@Autowired
private DummyBean dummyBean;
@Bean
public MyFactoryBean factoryBean() {
Assert.notNull(dummyBean, "DummyBean was not injected.");
return new MyFactoryBean();
}
@Bean
public String myString() {
return factoryBean().getString();
}
}
} }