Determine FactoryBean object type via generics
For the particular use case detailed in SPR-8514, with this change we
now attempt to determine the object type of a FactoryBean through its
generic type parameter if possible.
For (a contrived) example:
@Configuration
public MyConfig {
@Bean
public FactoryBean<String> fb() {
return new StringFactoryBean("foo");
}
}
The implementation will now look at the <String> generic parameter
instead of attempting to instantiate the FactoryBean in order to call
its #getObjectType() method.
This is important in order to avoid the autowiring lifecycle issues
detailed in SPR-8514. For example, prior to this change, the following
code would fail:
@Configuration
public MyConfig {
@Autowired Foo foo;
@Bean
public FactoryBean<String> fb() {
Assert.notNull(foo);
return new StringFactoryBean("foo");
}
}
The reason for this failure is that in order to perform autowiring,
the container must first determine the object type of all configured
FactoryBeans. Clearly a chicken-and-egg issue, now fixed by this
change.
And lest this be thought of as an obscure bug, keep in mind the use case
of our own JPA support: in order to configure and return a
LocalContainerEntityManagerFactoryBean from a @Bean method, one will
need access to a DataSource, etc -- resources that are likely to
be @Autowired across @Configuration classes for modularity purposes.
Note that while the examples above feature methods with return
types dealing directly with the FactoryBean interface, of course
the implementation deals with subclasses/subinterfaces of FactoryBean
equally as well. See ConfigurationWithFactoryBeanAndAutowiringTests
for complete examples.
There is at least a slight risk here, in that the signature of a
FactoryBean-returing @Bean method may advertise a generic type for the
FactoryBean less specific than the actual object returned (or than
advertised by #getObjectType for that matter). This could mean that an
autowiring target may be missed, that we end up with a kind of
autowiring 'false negative' where FactoryBeans are concerned. This is
probably a less common scenario than the need to work with an autowired
field within a FactoryBean-returning @Bean method, and also has a clear
workaround of making the generic return type more specific.
Issue: SPR-8514
This commit is contained in:
@@ -0,0 +1,218 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.annotation;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Tests cornering bug SPR-8514.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @since 3.1
|
||||
*/
|
||||
public class ConfigurationWithFactoryBeanAndAutowiringTests {
|
||||
|
||||
@Test
|
||||
public void withConcreteFactoryBeanImplementationAsReturnType() {
|
||||
AnnotationConfigApplicationContext ctx =
|
||||
new AnnotationConfigApplicationContext();
|
||||
ctx.register(AppConfig.class);
|
||||
ctx.register(ConcreteFactoryBeanImplementationConfig.class);
|
||||
ctx.refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withParameterizedFactoryBeanImplementationAsReturnType() {
|
||||
AnnotationConfigApplicationContext ctx =
|
||||
new AnnotationConfigApplicationContext();
|
||||
ctx.register(AppConfig.class);
|
||||
ctx.register(ParameterizedFactoryBeanImplementationConfig.class);
|
||||
ctx.refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withParameterizedFactoryBeanInterfaceAsReturnType() {
|
||||
AnnotationConfigApplicationContext ctx =
|
||||
new AnnotationConfigApplicationContext();
|
||||
ctx.register(AppConfig.class);
|
||||
ctx.register(ParameterizedFactoryBeanInterfaceConfig.class);
|
||||
ctx.refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withNonPublicParameterizedFactoryBeanInterfaceAsReturnType() {
|
||||
AnnotationConfigApplicationContext ctx =
|
||||
new AnnotationConfigApplicationContext();
|
||||
ctx.register(AppConfig.class);
|
||||
ctx.register(NonPublicParameterizedFactoryBeanInterfaceConfig.class);
|
||||
ctx.refresh();
|
||||
}
|
||||
|
||||
@Test(expected=BeanCreationException.class)
|
||||
public void withRawFactoryBeanInterfaceAsReturnType() {
|
||||
AnnotationConfigApplicationContext ctx =
|
||||
new AnnotationConfigApplicationContext();
|
||||
ctx.register(AppConfig.class);
|
||||
ctx.register(RawFactoryBeanInterfaceConfig.class);
|
||||
ctx.refresh();
|
||||
}
|
||||
|
||||
@Test(expected=BeanCreationException.class)
|
||||
public void withWildcardParameterizedFactoryBeanInterfaceAsReturnType() {
|
||||
AnnotationConfigApplicationContext ctx =
|
||||
new AnnotationConfigApplicationContext();
|
||||
ctx.register(AppConfig.class);
|
||||
ctx.register(WildcardParameterizedFactoryBeanInterfaceConfig.class);
|
||||
ctx.refresh();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class DummyBean {
|
||||
}
|
||||
|
||||
|
||||
class MyFactoryBean implements FactoryBean<String> {
|
||||
public String getObject() throws Exception {
|
||||
return "foo";
|
||||
}
|
||||
public Class<String> getObjectType() {
|
||||
return String.class;
|
||||
}
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class MyParameterizedFactoryBean<T> implements FactoryBean<T> {
|
||||
|
||||
private final T obj;
|
||||
|
||||
public MyParameterizedFactoryBean(T obj) {
|
||||
this.obj = obj;
|
||||
}
|
||||
|
||||
public T getObject() throws Exception {
|
||||
return obj;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Class<T> getObjectType() {
|
||||
return (Class<T>)obj.getClass();
|
||||
}
|
||||
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
class AppConfig {
|
||||
@Bean
|
||||
public DummyBean dummyBean() {
|
||||
return new DummyBean();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
class ConcreteFactoryBeanImplementationConfig {
|
||||
@Autowired
|
||||
private DummyBean dummyBean;
|
||||
|
||||
@Bean
|
||||
public MyFactoryBean factoryBean() {
|
||||
Assert.notNull(dummyBean, "DummyBean was not injected.");
|
||||
return new MyFactoryBean();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
class ParameterizedFactoryBeanImplementationConfig {
|
||||
@Autowired
|
||||
private DummyBean dummyBean;
|
||||
|
||||
@Bean
|
||||
public MyParameterizedFactoryBean<String> factoryBean() {
|
||||
Assert.notNull(dummyBean, "DummyBean was not injected.");
|
||||
return new MyParameterizedFactoryBean<String>("whatev");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
class ParameterizedFactoryBeanInterfaceConfig {
|
||||
@Autowired
|
||||
private DummyBean dummyBean;
|
||||
|
||||
@Bean
|
||||
public FactoryBean<String> factoryBean() {
|
||||
Assert.notNull(dummyBean, "DummyBean was not injected.");
|
||||
return new MyFactoryBean();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
class NonPublicParameterizedFactoryBeanInterfaceConfig {
|
||||
@Autowired
|
||||
private DummyBean dummyBean;
|
||||
|
||||
@Bean
|
||||
FactoryBean<String> factoryBean() {
|
||||
Assert.notNull(dummyBean, "DummyBean was not injected.");
|
||||
return new MyFactoryBean();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
class RawFactoryBeanInterfaceConfig {
|
||||
@Autowired
|
||||
private DummyBean dummyBean;
|
||||
|
||||
@Bean
|
||||
@SuppressWarnings("rawtypes")
|
||||
public FactoryBean factoryBean() {
|
||||
Assert.notNull(dummyBean, "DummyBean was not injected.");
|
||||
return new MyFactoryBean();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
class WildcardParameterizedFactoryBeanInterfaceConfig {
|
||||
@Autowired
|
||||
private DummyBean dummyBean;
|
||||
|
||||
@Bean
|
||||
public FactoryBean<?> factoryBean() {
|
||||
Assert.notNull(dummyBean, "DummyBean was not injected.");
|
||||
return new MyFactoryBean();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user