Skip to content

Commit 8b5d355

Browse files
committed
Avoid stack overflow in case of chained factory-bean references to FactoryBean class
Issue: SPR-14551
1 parent 6157fad commit 8b5d355

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ public Object configureBean(Object existingBean, String beanName) throws BeansEx
325325
}
326326

327327
@Override
328-
public Object resolveDependency(DependencyDescriptor descriptor, String beanName) throws BeansException {
329-
return resolveDependency(descriptor, beanName, null, null);
328+
public Object resolveDependency(DependencyDescriptor descriptor, String requestingBeanName) throws BeansException {
329+
return resolveDependency(descriptor, requestingBeanName, null, null);
330330
}
331331

332332

@@ -802,10 +802,14 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
802802
if (objectType.value != null) {
803803
return objectType.value;
804804
}
805+
else {
806+
// No type found for shortcut FactoryBean instance:
807+
// fall back to full creation of the FactoryBean instance.
808+
return super.getTypeForFactoryBean(beanName, mbd);
809+
}
805810
}
806811

807-
// No type found - fall back to full creation of the FactoryBean instance.
808-
return super.getTypeForFactoryBean(beanName, mbd);
812+
return null;
809813
}
810814

811815
/**
@@ -824,7 +828,7 @@ protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd,
824828
SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
825829
exposedObject = ibp.getEarlyBeanReference(exposedObject, beanName);
826830
if (exposedObject == null) {
827-
return exposedObject;
831+
return null;
828832
}
829833
}
830834
}

spring-orm/src/test/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryIntegrationTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -122,7 +122,9 @@ public void tearDown() throws Exception {
122122

123123
@AfterClass
124124
public static void closeContext() {
125-
applicationContext.close();
125+
if (applicationContext != null) {
126+
applicationContext.close();
127+
}
126128
applicationContext = null;
127129
}
128130

spring-orm/src/test/resources/org/springframework/orm/jpa/hibernate/hibernate-manager.xml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
35

4-
<beans>
5-
6-
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
6+
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" primary="true">
77
<property name="persistenceXmlLocation" value="org/springframework/orm/jpa/domain/persistence-context.xml"/>
88
<property name="dataSource" ref="dataSource"/>
99
<property name="jpaVendorAdapter">
@@ -23,4 +23,10 @@
2323
</property>
2424
</bean>
2525

26+
<bean id="sessionFactory" factory-bean="entityManagerFactory" factory-method="getSessionFactory"/>
27+
28+
<bean id="hibernateStatistics" factory-bean="sessionFactory" factory-method="getStatistics"/>
29+
30+
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
31+
2632
</beans>

0 commit comments

Comments
 (0)