Skip to content

Commit 6157fad

Browse files
committed
PersistenceAnnotationBeanPostProcessor obtains default EntityManagerFactory via getBean(Class) algorithm
Issue: SPR-7549
1 parent eeeab27 commit 6157fad

File tree

7 files changed

+205
-92
lines changed

7 files changed

+205
-92
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/BeanFactory.java

Lines changed: 3 additions & 3 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.
@@ -153,12 +153,12 @@ public interface BeanFactory {
153153

154154
/**
155155
* Return the bean instance that uniquely matches the given object type, if any.
156-
* @param requiredType type the bean must match; can be an interface or superclass.
157-
* {@code null} is disallowed.
158156
* <p>This method goes into {@link ListableBeanFactory} by-type lookup territory
159157
* but may also be translated into a conventional by-name lookup based on the name
160158
* of the given type. For more extensive retrieval operations across sets of beans,
161159
* use {@link ListableBeanFactory} and/or {@link BeanFactoryUtils}.
160+
* @param requiredType type the bean must match; can be an interface or superclass.
161+
* {@code null} is disallowed.
162162
* @return an instance of the single bean matching the required type
163163
* @throws NoSuchBeanDefinitionException if no bean of the given type was found
164164
* @throws NoUniqueBeanDefinitionException if more than one bean of the given type was found

spring-beans/src/main/java/org/springframework/beans/factory/NamedBean.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2006 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.
@@ -17,10 +17,10 @@
1717
package org.springframework.beans.factory;
1818

1919
/**
20-
* Counterpart of BeanNameAware. Returns the bean name of an object.
20+
* Counterpart of {@link BeanNameAware}. Returns the bean name of an object.
2121
*
22-
* <p>This interface can be introduced to avoid a brittle dependence
23-
* on bean name in objects used with Spring IoC and Spring AOP.
22+
* <p>This interface can be introduced to avoid a brittle dependence on
23+
* bean name in objects used with Spring IoC and Spring AOP.
2424
*
2525
* @author Rod Johnson
2626
* @since 2.0
@@ -29,7 +29,7 @@
2929
public interface NamedBean {
3030

3131
/**
32-
* Return the name of this bean in a Spring bean factory.
32+
* Return the name of this bean in a Spring bean factory, if known.
3333
*/
3434
String getBeanName();
3535

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

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.springframework.beans.BeansException;
2222
import org.springframework.beans.TypeConverter;
2323
import org.springframework.beans.factory.BeanFactory;
24+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
25+
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
2426

2527
/**
2628
* Extension of the {@link org.springframework.beans.factory.BeanFactory}
@@ -154,15 +156,6 @@ public interface AutowireCapableBeanFactory extends BeanFactory {
154156
*/
155157
Object configureBean(Object existingBean, String beanName) throws BeansException;
156158

157-
/**
158-
* Resolve the specified dependency against the beans defined in this factory.
159-
* @param descriptor the descriptor for the dependency
160-
* @param beanName the name of the bean which declares the present dependency
161-
* @return the resolved object, or {@code null} if none found
162-
* @throws BeansException if dependency resolution failed
163-
*/
164-
Object resolveDependency(DependencyDescriptor descriptor, String beanName) throws BeansException;
165-
166159

167160
//-------------------------------------------------------------------------
168161
// Specialized methods for fine-grained control over the bean lifecycle
@@ -312,18 +305,55 @@ Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String be
312305
*/
313306
void destroyBean(Object existingBean);
314307

308+
309+
//-------------------------------------------------------------------------
310+
// Delegate methods for resolving injection points
311+
//-------------------------------------------------------------------------
312+
313+
/**
314+
* Resolve the bean instance that uniquely matches the given object type, if any,
315+
* including its bean name.
316+
* <p>This is effectively a variant of {@link #getBean(Class)} which preserves the
317+
* bean name of the matching instance.
318+
* @param requiredType type the bean must match; can be an interface or superclass.
319+
* {@code null} is disallowed.
320+
* @return the bean name plus bean instance
321+
* @throws NoSuchBeanDefinitionException if no bean of the given type was found
322+
* @throws NoUniqueBeanDefinitionException if more than one bean of the given type was found
323+
* @throws BeansException if the bean could not be created
324+
* @since 4.3.3
325+
* @see #getBean(Class)
326+
*/
327+
<T> NamedBeanHolder<T> resolveNamedBean(Class<T> requiredType) throws BeansException;
328+
329+
/**
330+
* Resolve the specified dependency against the beans defined in this factory.
331+
* @param descriptor the descriptor for the dependency
332+
* @param requestingBeanName the name of the bean which declares the present dependency
333+
* @return the resolved object, or {@code null} if none found
334+
* @throws NoSuchBeanDefinitionException if no matching bean was found
335+
* @throws NoUniqueBeanDefinitionException if more than one matching bean was found
336+
* @throws BeansException if dependency resolution failed for any other reason
337+
* @see #resolveDependency(DependencyDescriptor, String, Set, TypeConverter)
338+
* @since 2.5
339+
*/
340+
Object resolveDependency(DependencyDescriptor descriptor, String requestingBeanName) throws BeansException;
341+
315342
/**
316343
* Resolve the specified dependency against the beans defined in this factory.
317344
* @param descriptor the descriptor for the dependency
318-
* @param beanName the name of the bean which declares the present dependency
319-
* @param autowiredBeanNames a Set that all names of autowired beans (used for
320-
* resolving the present dependency) are supposed to be added to
321-
* @param typeConverter the TypeConverter to use for populating arrays and
322-
* collections
345+
* @param requestingBeanName the name of the bean which declares the present dependency
346+
* @param autowiredBeanNames a Set that all names of autowired beans (used for resolving
347+
* the present dependency) are supposed to be added to
348+
* @param typeConverter the TypeConverter to use for populating arrays and collections
323349
* @return the resolved object, or {@code null} if none found
324-
* @throws BeansException if dependency resolution failed
350+
* @throws NoSuchBeanDefinitionException if no matching bean was found
351+
* @throws NoUniqueBeanDefinitionException if more than one matching bean was found
352+
* @throws BeansException if dependency resolution failed for any other reason
353+
* @see DependencyDescriptor
354+
* @since 2.5
325355
*/
326-
Object resolveDependency(DependencyDescriptor descriptor, String beanName,
356+
Object resolveDependency(DependencyDescriptor descriptor, String requestingBeanName,
327357
Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException;
328358

329359
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2002-2016 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.beans.factory.config;
18+
19+
import org.springframework.beans.factory.NamedBean;
20+
21+
/**
22+
* A simple holder for a given bean name plus bean instance.
23+
*
24+
* @author Juergen Hoeller
25+
* @since 4.3.3
26+
* @see AutowireCapableBeanFactory#resolveNamedBean(Class)
27+
*/
28+
public class NamedBeanHolder<T> implements NamedBean {
29+
30+
private final String beanName;
31+
32+
private final T beanInstance;
33+
34+
35+
/**
36+
* Create a new holder for the given bean name plus instance.
37+
*/
38+
public NamedBeanHolder(String beanName, T beanInstance) {
39+
this.beanName = beanName;
40+
this.beanInstance = beanInstance;
41+
}
42+
43+
44+
@Override
45+
public String getBeanName() {
46+
return this.beanName;
47+
}
48+
49+
/**
50+
* Return the corresponding bean instance.
51+
*/
52+
public T getBeanInstance() {
53+
return this.beanInstance;
54+
}
55+
56+
}

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

Lines changed: 77 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.Collection;
3333
import java.util.Collections;
3434
import java.util.Comparator;
35-
import java.util.HashMap;
3635
import java.util.IdentityHashMap;
3736
import java.util.Iterator;
3837
import java.util.LinkedHashMap;
@@ -62,11 +61,13 @@
6261
import org.springframework.beans.factory.ObjectProvider;
6362
import org.springframework.beans.factory.SmartFactoryBean;
6463
import org.springframework.beans.factory.SmartInitializingSingleton;
64+
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
6565
import org.springframework.beans.factory.config.BeanDefinition;
6666
import org.springframework.beans.factory.config.BeanDefinitionHolder;
6767
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
6868
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
6969
import org.springframework.beans.factory.config.DependencyDescriptor;
70+
import org.springframework.beans.factory.config.NamedBeanHolder;
7071
import org.springframework.core.OrderComparator;
7172
import org.springframework.core.ResolvableType;
7273
import org.springframework.core.annotation.AnnotationUtils;
@@ -326,43 +327,15 @@ public <T> T getBean(Class<T> requiredType) throws BeansException {
326327

327328
@Override
328329
public <T> T getBean(Class<T> requiredType, Object... args) throws BeansException {
329-
Assert.notNull(requiredType, "Required type must not be null");
330-
String[] beanNames = getBeanNamesForType(requiredType);
331-
if (beanNames.length > 1) {
332-
ArrayList<String> autowireCandidates = new ArrayList<>();
333-
for (String beanName : beanNames) {
334-
if (!containsBeanDefinition(beanName) || getBeanDefinition(beanName).isAutowireCandidate()) {
335-
autowireCandidates.add(beanName);
336-
}
337-
}
338-
if (autowireCandidates.size() > 0) {
339-
beanNames = autowireCandidates.toArray(new String[autowireCandidates.size()]);
340-
}
341-
}
342-
if (beanNames.length == 1) {
343-
return getBean(beanNames[0], requiredType, args);
344-
}
345-
else if (beanNames.length > 1) {
346-
Map<String, Object> candidates = new HashMap<>();
347-
for (String beanName : beanNames) {
348-
candidates.put(beanName, getBean(beanName, requiredType, args));
349-
}
350-
String primaryCandidate = determinePrimaryCandidate(candidates, requiredType);
351-
if (primaryCandidate != null) {
352-
return getBean(primaryCandidate, requiredType, args);
353-
}
354-
String priorityCandidate = determineHighestPriorityCandidate(candidates, requiredType);
355-
if (priorityCandidate != null) {
356-
return getBean(priorityCandidate, requiredType, args);
357-
}
358-
throw new NoUniqueBeanDefinitionException(requiredType, candidates.keySet());
359-
}
360-
else if (getParentBeanFactory() != null) {
361-
return getParentBeanFactory().getBean(requiredType, args);
330+
NamedBeanHolder<T> namedBean = resolveNamedBean(requiredType, args);
331+
if (namedBean != null) {
332+
return namedBean.getBeanInstance();
362333
}
363-
else {
364-
throw new NoSuchBeanDefinitionException(requiredType);
334+
BeanFactory parent = getParentBeanFactory();
335+
if (parent != null) {
336+
return parent.getBean(requiredType, args);
365337
}
338+
throw new NoSuchBeanDefinitionException(requiredType);
366339
}
367340

368341

@@ -642,13 +615,15 @@ protected boolean isAutowireCandidate(String beanName, DependencyDescriptor desc
642615
else if (containsSingleton(beanName)) {
643616
return isAutowireCandidate(beanName, new RootBeanDefinition(getType(beanName)), descriptor, resolver);
644617
}
645-
else if (getParentBeanFactory() instanceof DefaultListableBeanFactory) {
618+
619+
BeanFactory parent = getParentBeanFactory();
620+
if (parent instanceof DefaultListableBeanFactory) {
646621
// No bean definition found in this factory -> delegate to parent.
647-
return ((DefaultListableBeanFactory) getParentBeanFactory()).isAutowireCandidate(beanName, descriptor, resolver);
622+
return ((DefaultListableBeanFactory) parent).isAutowireCandidate(beanName, descriptor, resolver);
648623
}
649-
else if (getParentBeanFactory() instanceof ConfigurableListableBeanFactory) {
624+
else if (parent instanceof ConfigurableListableBeanFactory) {
650625
// If no DefaultListableBeanFactory, can't pass the resolver along.
651-
return ((ConfigurableListableBeanFactory) getParentBeanFactory()).isAutowireCandidate(beanName, descriptor);
626+
return ((ConfigurableListableBeanFactory) parent).isAutowireCandidate(beanName, descriptor);
652627
}
653628
else {
654629
return true;
@@ -989,24 +964,74 @@ private void clearByTypeCache() {
989964
//---------------------------------------------------------------------
990965

991966
@Override
992-
public Object resolveDependency(DependencyDescriptor descriptor, String beanName,
967+
public <T> NamedBeanHolder<T> resolveNamedBean(Class<T> requiredType) throws BeansException {
968+
NamedBeanHolder<T> namedBean = resolveNamedBean(requiredType, (Object[]) null);
969+
if (namedBean != null) {
970+
return namedBean;
971+
}
972+
BeanFactory parent = getParentBeanFactory();
973+
if (parent instanceof AutowireCapableBeanFactory) {
974+
return ((AutowireCapableBeanFactory) parent).resolveNamedBean(requiredType);
975+
}
976+
return null;
977+
}
978+
979+
private <T> NamedBeanHolder<T> resolveNamedBean(Class<T> requiredType, Object... args) throws BeansException {
980+
Assert.notNull(requiredType, "Required type must not be null");
981+
String[] beanNames = getBeanNamesForType(requiredType);
982+
if (beanNames.length > 1) {
983+
ArrayList<String> autowireCandidates = new ArrayList<>();
984+
for (String beanName : beanNames) {
985+
if (!containsBeanDefinition(beanName) || getBeanDefinition(beanName).isAutowireCandidate()) {
986+
autowireCandidates.add(beanName);
987+
}
988+
}
989+
if (!autowireCandidates.isEmpty()) {
990+
beanNames = autowireCandidates.toArray(new String[autowireCandidates.size()]);
991+
}
992+
}
993+
if (beanNames.length == 1) {
994+
String beanName = beanNames[0];
995+
return new NamedBeanHolder<>(beanName, getBean(beanName, requiredType, args));
996+
}
997+
else if (beanNames.length > 1) {
998+
Map<String, Object> candidates = new LinkedHashMap<>();
999+
for (String beanName : beanNames) {
1000+
candidates.put(beanName, getBean(beanName, requiredType, args));
1001+
}
1002+
String primaryCandidate = determinePrimaryCandidate(candidates, requiredType);
1003+
if (primaryCandidate != null) {
1004+
return new NamedBeanHolder<>(primaryCandidate, getBean(primaryCandidate, requiredType, args));
1005+
}
1006+
String priorityCandidate = determineHighestPriorityCandidate(candidates, requiredType);
1007+
if (priorityCandidate != null) {
1008+
return new NamedBeanHolder<>(priorityCandidate, getBean(priorityCandidate, requiredType, args));
1009+
}
1010+
throw new NoUniqueBeanDefinitionException(requiredType, candidates.keySet());
1011+
}
1012+
return null;
1013+
}
1014+
1015+
@Override
1016+
public Object resolveDependency(DependencyDescriptor descriptor, String requestingBeanName,
9931017
Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException {
9941018

9951019
descriptor.initParameterNameDiscovery(getParameterNameDiscoverer());
9961020
if (Optional.class == descriptor.getDependencyType()) {
997-
return createOptionalDependency(descriptor, beanName);
1021+
return createOptionalDependency(descriptor, requestingBeanName);
9981022
}
9991023
else if (ObjectFactory.class == descriptor.getDependencyType() ||
10001024
ObjectProvider.class == descriptor.getDependencyType()) {
1001-
return new DependencyObjectProvider(descriptor, beanName);
1025+
return new DependencyObjectProvider(descriptor, requestingBeanName);
10021026
}
10031027
else if (javaxInjectProviderClass == descriptor.getDependencyType()) {
1004-
return new Jsr330ProviderFactory().createDependencyProvider(descriptor, beanName);
1028+
return new Jsr330ProviderFactory().createDependencyProvider(descriptor, requestingBeanName);
10051029
}
10061030
else {
1007-
Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary(descriptor, beanName);
1031+
Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary(
1032+
descriptor, requestingBeanName);
10081033
if (result == null) {
1009-
result = doResolveDependency(descriptor, beanName, autowiredBeanNames, typeConverter);
1034+
result = doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter);
10101035
}
10111036
return result;
10121037
}
@@ -1337,9 +1362,9 @@ protected boolean isPrimary(String beanName, Object beanInstance) {
13371362
if (containsBeanDefinition(beanName)) {
13381363
return getMergedLocalBeanDefinition(beanName).isPrimary();
13391364
}
1340-
BeanFactory parentFactory = getParentBeanFactory();
1341-
return (parentFactory instanceof DefaultListableBeanFactory &&
1342-
((DefaultListableBeanFactory) parentFactory).isPrimary(beanName, beanInstance));
1365+
BeanFactory parent = getParentBeanFactory();
1366+
return (parent instanceof DefaultListableBeanFactory &&
1367+
((DefaultListableBeanFactory) parent).isPrimary(beanName, beanInstance));
13431368
}
13441369

13451370
/**
@@ -1415,8 +1440,9 @@ private void checkBeanNotOfRequiredType(Class<?> type, DependencyDescriptor desc
14151440
}
14161441
}
14171442

1418-
if (getParentBeanFactory() instanceof DefaultListableBeanFactory) {
1419-
((DefaultListableBeanFactory) getParentBeanFactory()).checkBeanNotOfRequiredType(type, descriptor);
1443+
BeanFactory parent = getParentBeanFactory();
1444+
if (parent instanceof DefaultListableBeanFactory) {
1445+
((DefaultListableBeanFactory) parent).checkBeanNotOfRequiredType(type, descriptor);
14201446
}
14211447
}
14221448

0 commit comments

Comments
 (0)