Skip to content

Commit 0288878

Browse files
committed
Consistent handling of early FactoryBean instantiation failures
Closes gh-22409
1 parent 46a39db commit 0288878

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,18 @@ private FactoryBean<?> getSingletonFactoryBeanForTypeCheck(String beanName, Root
992992
instance = bw.getWrappedInstance();
993993
}
994994
}
995+
catch (UnsatisfiedDependencyException ex) {
996+
// Don't swallow, probably misconfiguration...
997+
throw ex;
998+
}
999+
catch (BeanCreationException ex) {
1000+
// Instantiation failure, maybe too early...
1001+
if (logger.isDebugEnabled()) {
1002+
logger.debug("Bean creation exception on singleton FactoryBean type check: " + ex);
1003+
}
1004+
onSuppressedException(ex);
1005+
return null;
1006+
}
9951007
finally {
9961008
// Finished partial creation of this bean.
9971009
afterSingletonCreation(beanName);
@@ -1019,7 +1031,7 @@ private FactoryBean<?> getNonSingletonFactoryBeanForTypeCheck(String beanName, R
10191031
return null;
10201032
}
10211033

1022-
Object instance = null;
1034+
Object instance;
10231035
try {
10241036
// Mark this bean as currently in creation, even if just partially.
10251037
beforePrototypeCreation(beanName);
@@ -1030,8 +1042,12 @@ private FactoryBean<?> getNonSingletonFactoryBeanForTypeCheck(String beanName, R
10301042
instance = bw.getWrappedInstance();
10311043
}
10321044
}
1045+
catch (UnsatisfiedDependencyException ex) {
1046+
// Don't swallow, probably misconfiguration...
1047+
throw ex;
1048+
}
10331049
catch (BeanCreationException ex) {
1034-
// Can only happen when getting a FactoryBean.
1050+
// Instantiation failure, maybe too early...
10351051
if (logger.isDebugEnabled()) {
10361052
logger.debug("Bean creation exception on non-singleton FactoryBean type check: " + ex);
10371053
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context.annotation;
18+
19+
import org.junit.Test;
20+
21+
import org.springframework.beans.factory.BeanFactoryUtils;
22+
import org.springframework.beans.factory.FactoryBean;
23+
import org.springframework.context.ApplicationContext;
24+
25+
/**
26+
* @author Andy Wilkinson
27+
*/
28+
public class AggressiveFactoryBeanInstantiationTests {
29+
30+
@Test
31+
public void directlyRegisteredFactoryBean() {
32+
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
33+
context.register(SimpleFactoryBean.class);
34+
context.addBeanFactoryPostProcessor((factory) -> {
35+
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, String.class);
36+
});
37+
context.refresh();
38+
}
39+
}
40+
41+
@Test
42+
public void beanMethodFactoryBean() {
43+
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
44+
context.register(BeanMethodConfiguration.class);
45+
context.addBeanFactoryPostProcessor((factory) -> {
46+
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, String.class);
47+
});
48+
context.refresh();
49+
}
50+
}
51+
52+
53+
@Configuration
54+
static class BeanMethodConfiguration {
55+
56+
@Bean
57+
public SimpleFactoryBean simpleFactoryBean(ApplicationContext applicationContext) {
58+
return new SimpleFactoryBean(applicationContext);
59+
}
60+
}
61+
62+
63+
static class SimpleFactoryBean implements FactoryBean<Object> {
64+
65+
public SimpleFactoryBean(ApplicationContext applicationContext) {
66+
}
67+
68+
@Override
69+
public Object getObject() {
70+
return new Object();
71+
}
72+
73+
@Override
74+
public Class<?> getObjectType() {
75+
return Object.class;
76+
}
77+
}
78+
79+
}

0 commit comments

Comments
 (0)