Skip to content

Commit 1a929f2

Browse files
jhoellerunknown
authored andcommitted
EntityManagerFactoryUtils finds default EntityManagerFactory in parent contexts as well
Also introduces consistent use of getBean(Class) for similar use cases across the framework, accepting a locally unique target bean even if further matching beans would be available in parent contexts (in contrast to BeanFactoryUtils.beanOfType's behavior). Issue: SPR-10160
1 parent 8e75eee commit 1a929f2

File tree

4 files changed

+15
-19
lines changed

4 files changed

+15
-19
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/config/ServiceLocatorFactoryBean.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 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.
@@ -27,7 +27,6 @@
2727
import org.springframework.beans.FatalBeanException;
2828
import org.springframework.beans.factory.BeanFactory;
2929
import org.springframework.beans.factory.BeanFactoryAware;
30-
import org.springframework.beans.factory.BeanFactoryUtils;
3130
import org.springframework.beans.factory.FactoryBean;
3231
import org.springframework.beans.factory.InitializingBean;
3332
import org.springframework.beans.factory.ListableBeanFactory;
@@ -364,12 +363,12 @@ private Object invokeServiceLocatorMethod(Method method, Object[] args) throws E
364363
try {
365364
String beanName = tryGetBeanName(args);
366365
if (StringUtils.hasLength(beanName)) {
367-
// Service locator for a specific bean name.
366+
// Service locator for a specific bean name
368367
return beanFactory.getBean(beanName, serviceLocatorMethodReturnType);
369368
}
370369
else {
371-
// Service locator for a bean type.
372-
return BeanFactoryUtils.beanOfTypeIncludingAncestors(beanFactory, serviceLocatorMethodReturnType);
370+
// Service locator for a bean type
371+
return beanFactory.getBean(serviceLocatorMethodReturnType);
373372
}
374373
}
375374
catch (BeansException ex) {

spring-orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryUtils.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 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.
@@ -76,10 +76,12 @@ public abstract class EntityManagerFactoryUtils {
7676
* Find an EntityManagerFactory with the given name in the given
7777
* Spring application context (represented as ListableBeanFactory).
7878
* <p>The specified unit name will be matched against the configured
79-
* peristence unit, provided that a discovered EntityManagerFactory
79+
* persistence unit, provided that a discovered EntityManagerFactory
8080
* implements the {@link EntityManagerFactoryInfo} interface. If not,
8181
* the persistence unit name will be matched against the Spring bean name,
8282
* assuming that the EntityManagerFactory bean names follow that convention.
83+
* <p>If no unit name has been given, this method will search for a default
84+
* EntityManagerFactory through {@link ListableBeanFactory#getBean(Class)}.
8385
* @param beanFactory the ListableBeanFactory to search
8486
* @param unitName the name of the persistence unit (may be {@code null} or empty,
8587
* in which case a single bean of type EntityManagerFactory will be searched for)
@@ -108,7 +110,8 @@ public static EntityManagerFactory findEntityManagerFactory(
108110
return beanFactory.getBean(unitName, EntityManagerFactory.class);
109111
}
110112
else {
111-
return BeanFactoryUtils.beanOfType(beanFactory, EntityManagerFactory.class);
113+
// Find unique EntityManagerFactory bean in the context, falling back to parent contexts.
114+
return beanFactory.getBean(EntityManagerFactory.class);
112115
}
113116
}
114117

spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionAspectSupport.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 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.
@@ -24,9 +24,7 @@
2424

2525
import org.springframework.beans.factory.BeanFactory;
2626
import org.springframework.beans.factory.BeanFactoryAware;
27-
import org.springframework.beans.factory.BeanFactoryUtils;
2827
import org.springframework.beans.factory.InitializingBean;
29-
import org.springframework.beans.factory.ListableBeanFactory;
3028
import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils;
3129
import org.springframework.core.NamedThreadLocal;
3230
import org.springframework.transaction.NoTransactionException;
@@ -247,12 +245,8 @@ protected PlatformTransactionManager determineTransactionManager(TransactionAttr
247245
else if (this.transactionManagerBeanName != null) {
248246
return this.beanFactory.getBean(this.transactionManagerBeanName, PlatformTransactionManager.class);
249247
}
250-
else if (this.beanFactory instanceof ListableBeanFactory) {
251-
return BeanFactoryUtils.beanOfTypeIncludingAncestors(((ListableBeanFactory) this.beanFactory), PlatformTransactionManager.class);
252-
}
253248
else {
254-
throw new IllegalStateException(
255-
"Cannot retrieve PlatformTransactionManager beans from non-listable BeanFactory: " + this.beanFactory);
249+
return this.beanFactory.getBean(PlatformTransactionManager.class);
256250
}
257251
}
258252

spring-tx/src/main/java/org/springframework/transaction/interceptor/TransactionProxyFactoryBean.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 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.
@@ -176,8 +176,8 @@ public void setPointcut(Pointcut pointcut) {
176176
/**
177177
* This callback is optional: If running in a BeanFactory and no transaction
178178
* manager has been set explicitly, a single matching bean of type
179-
* PlatformTransactionManager will be fetched from the BeanFactory.
180-
* @see org.springframework.beans.factory.BeanFactoryUtils#beanOfTypeIncludingAncestors
179+
* {@link PlatformTransactionManager} will be fetched from the BeanFactory.
180+
* @see org.springframework.beans.factory.BeanFactory#getBean(Class)
181181
* @see org.springframework.transaction.PlatformTransactionManager
182182
*/
183183
public void setBeanFactory(BeanFactory beanFactory) {

0 commit comments

Comments
 (0)