Go to file
Chris Beams 7d9d255daf 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

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4681 50f2f4bb-b051-0410-bef5-90022cba6387
2011-07-06 09:15:37 +00:00
build-spring-framework DataBinder etc 2011-07-03 22:39:08 +00:00
org.springframework.aop Deprecate/move CGLIB methods AopUtils=>ClassUtils 2011-07-06 09:15:27 +00:00
org.springframework.asm Merge 3.1.0 development branch into trunk 2010-10-25 19:48:20 +00:00
org.springframework.aspects shortened build properties "org.junit.version" to "junit.version" and "org.testng.version" to "testng.version"; reverted SLF4J version back to 1.5.3 (for Hibernate 3.3.1 compatibility) 2011-06-09 13:01:09 +00:00
org.springframework.beans Determine FactoryBean object type via generics 2011-07-06 09:15:37 +00:00
org.springframework.context Determine FactoryBean object type via generics 2011-07-06 09:15:37 +00:00
org.springframework.context.support JavaMailSenderImpl detects and respects "mail.transport.protocol" property in existing Session (SPR-8501) 2011-07-04 22:31:03 +00:00
org.springframework.core Introduce GenericTypeResolver#resolveReturnTypeArgument 2011-07-06 09:15:32 +00:00
org.springframework.expression fixed TypeDescriptor rendering (SPR-8508) 2011-07-05 22:06:14 +00:00
org.springframework.instrument Include license.txt and notice.txt in module JARs 2011-02-09 06:56:40 +00:00
org.springframework.instrument.tomcat Include license.txt and notice.txt in module JARs 2011-02-09 06:56:40 +00:00
org.springframework.integration-tests shortened build properties "org.junit.version" to "junit.version" and "org.testng.version" to "testng.version"; reverted SLF4J version back to 1.5.3 (for Hibernate 3.3.1 compatibility) 2011-06-09 12:50:55 +00:00
org.springframework.jdbc avoid Logger.getGlobal() - it's Java 7 only... 2011-06-09 23:15:27 +00:00
org.springframework.jms revised JMS CachedConnectionFactory to avoid unnecessary rollback calls on Session return (SPR-8437); fixed JMS CachedConnectionFactory to fully synchronize its Session list (SPR-8436) 2011-06-13 22:32:42 +00:00
org.springframework.orm support for Hibernate 4.0 as a JPA provider 2011-06-14 22:45:29 +00:00
org.springframework.oxm Update pom and .classpath to reflect Castor upgrade 2011-06-16 04:36:53 +00:00
org.springframework.spring-library Merge 3.1.0 development branch into trunk 2010-10-25 19:48:20 +00:00
org.springframework.spring-parent Updating JUnit and TestNG versions for IDEs and Maven. 2011-03-28 19:59:09 +00:00
org.springframework.test fixed getHeaderNames signature 2011-06-28 14:08:47 +00:00
org.springframework.transaction fixed package declaration 2011-07-04 22:04:31 +00:00
org.springframework.web ContextLoader and FrameworkServlet support "contextId" parameter for custom serialization id 2011-07-03 22:36:42 +00:00
org.springframework.web.portlet fixed getHeaderNames signature 2011-06-28 14:08:47 +00:00
org.springframework.web.servlet ContextLoader and FrameworkServlet support "contextId" parameter for custom serialization id 2011-07-03 22:36:42 +00:00
org.springframework.web.struts shortened build properties "org.junit.version" to "junit.version" and "org.testng.version" to "testng.version"; reverted SLF4J version back to 1.5.3 (for Hibernate 3.3.1 compatibility) 2011-06-09 13:10:09 +00:00
spring-framework-reference SPR-7858 2011-07-06 06:15:27 +00:00
src/test/java/org/springframework/core/env Merge 3.1.0 development branch into trunk 2010-10-25 19:48:20 +00:00
.gitignore Add ignorable log file to .gitignore 2011-06-05 21:01:37 +00:00
build.properties Update javadoc memory and doc publication settings 2011-06-09 02:54:52 +00:00
build.versions shortened build properties "org.junit.version" to "junit.version" and "org.testng.version" to "testng.version"; reverted SLF4J version back to 1.5.3 (for Hibernate 3.3.1 compatibility) 2011-06-09 09:58:15 +00:00
ci-build.properties adding properties file to simplify ant use in Bamboo CI build 2008-12-17 15:47:14 +00:00
eclipse-code-formatter.xml h2 embedded db support; updated formatting conventions not to auto-format javadoc; added hsqldb and h2 to jdbc maven pom as optional deps 2009-05-09 22:27:05 +00:00
spring-framework.ipr shortened build properties "org.junit.version" to "junit.version" and "org.testng.version" to "testng.version"; reverted SLF4J version back to 1.5.3 (for Hibernate 3.3.1 compatibility) 2011-06-09 13:26:46 +00:00
spring-framework.psf Renamed org.springframework.instrument.classloading module to org.springframework.instrument.tomcat 2009-09-25 12:51:58 +00:00