Skip to content

Commit a4b00c7

Browse files
committed
Introduce BeanFactoryAnnotationUtils
Commit 096693c refactored and deprecated TransactionAspectUtils, moving its #qualifiedBeanOfType and related methods into BeanFactoryUtils. This created a package cycle between beans.factory and beans.factory.annotation due to use of the beans.factory.annotation.Qualifier annotation in these methods. This commit breaks the package cycle by introducing beans.factory.annotation.BeanFactoryAnnotationUtils and moving these @Qualifier-related methods to it. It is intentionally similar in name and style to the familiar BeanFactoryUtils class for purposes of discoverability. There are no backward-compatibilty concerns associated with this change as the cycle was introduced, caught and now fixed before a release. Issue: SPR-6847
1 parent 4027b38 commit a4b00c7

File tree

6 files changed

+153
-115
lines changed

6 files changed

+153
-115
lines changed

spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncExecutionAspectSupport.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.beans.BeansException;
2626
import org.springframework.beans.factory.BeanFactory;
2727
import org.springframework.beans.factory.BeanFactoryAware;
28-
import org.springframework.beans.factory.BeanFactoryUtils;
28+
import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils;
2929
import org.springframework.core.task.AsyncTaskExecutor;
3030
import org.springframework.core.task.support.TaskExecutorAdapter;
3131
import org.springframework.util.Assert;
@@ -110,7 +110,8 @@ protected AsyncTaskExecutor determineAsyncExecutor(Method method) {
110110
Assert.notNull(this.beanFactory,
111111
"BeanFactory must be set on " + this.getClass().getSimpleName() +
112112
" to access qualified executor [" + qualifier + "]");
113-
executor = BeanFactoryUtils.qualifiedBeanOfType(this.beanFactory, Executor.class, qualifier);
113+
executor = BeanFactoryAnnotationUtils.qualifiedBeanOfType(
114+
this.beanFactory, Executor.class, qualifier);
114115
}
115116

116117
if (executor instanceof AsyncTaskExecutor) {

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

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.beans.factory;
1818

19-
import java.lang.reflect.Method;
2019

2120
import java.util.ArrayList;
2221
import java.util.Arrays;
@@ -25,14 +24,7 @@
2524
import java.util.Map;
2625

2726
import org.springframework.beans.BeansException;
28-
import org.springframework.beans.factory.annotation.Qualifier;
29-
import org.springframework.beans.factory.config.BeanDefinition;
30-
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
31-
import org.springframework.beans.factory.support.AbstractBeanDefinition;
32-
import org.springframework.beans.factory.support.AutowireCandidateQualifier;
33-
import org.springframework.beans.factory.support.RootBeanDefinition;
3427
import org.springframework.util.Assert;
35-
import org.springframework.util.ObjectUtils;
3628
import org.springframework.util.StringUtils;
3729

3830
/**
@@ -441,102 +433,4 @@ public static <T> T beanOfType(
441433
}
442434
}
443435

444-
/**
445-
* Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a
446-
* qualifier (e.g. via {@code <qualifier>} or {@code @Qualifier}) matching the given
447-
* qualifier, or having a bean name matching the given qualifier.
448-
* @param bf the BeanFactory to get the target bean from
449-
* @param beanType the type of bean to retrieve
450-
* @param qualifier the qualifier for selecting between multiple bean matches
451-
* @return the matching bean of type {@code T} (never {@code null})
452-
* @throws IllegalStateException if no matching bean of type {@code T} found
453-
* @since 3.2
454-
*/
455-
public static <T> T qualifiedBeanOfType(BeanFactory beanFactory, Class<T> beanType, String qualifier) {
456-
if (beanFactory instanceof ConfigurableListableBeanFactory) {
457-
// Full qualifier matching supported.
458-
return qualifiedBeanOfType((ConfigurableListableBeanFactory) beanFactory, beanType, qualifier);
459-
}
460-
else if (beanFactory.containsBean(qualifier)) {
461-
// Fallback: target bean at least found by bean name.
462-
return beanFactory.getBean(qualifier, beanType);
463-
}
464-
else {
465-
throw new IllegalStateException("No matching " + beanType.getSimpleName() +
466-
" bean found for bean name '" + qualifier +
467-
"'! (Note: Qualifier matching not supported because given " +
468-
"BeanFactory does not implement ConfigurableListableBeanFactory.)");
469-
}
470-
}
471-
472-
/**
473-
* Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a
474-
* qualifier (e.g. {@code <qualifier>} or {@code @Qualifier}) matching the given
475-
* qualifier
476-
* @param bf the BeanFactory to get the target bean from
477-
* @param beanType the type of bean to retrieve
478-
* @param qualifier the qualifier for selecting between multiple bean matches
479-
* @return the matching bean of type {@code T} (never {@code null})
480-
* @throws IllegalStateException if no matching bean of type {@code T} found
481-
*/
482-
private static <T> T qualifiedBeanOfType(ConfigurableListableBeanFactory bf, Class<T> beanType, String qualifier) {
483-
Map<String, T> candidateBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, beanType);
484-
T matchingBean = null;
485-
for (String beanName : candidateBeans.keySet()) {
486-
if (isQualifierMatch(qualifier, beanName, bf)) {
487-
if (matchingBean != null) {
488-
throw new IllegalStateException("No unique " + beanType.getSimpleName() +
489-
" bean found for qualifier '" + qualifier + "'");
490-
}
491-
matchingBean = candidateBeans.get(beanName);
492-
}
493-
}
494-
if (matchingBean != null) {
495-
return matchingBean;
496-
}
497-
else {
498-
throw new IllegalStateException("No matching " + beanType.getSimpleName() +
499-
" bean found for qualifier '" + qualifier + "' - neither qualifier " +
500-
"match nor bean name match!");
501-
}
502-
}
503-
504-
/**
505-
* Check whether the named bean declares a qualifier of the given name.
506-
* @param qualifier the qualifier to match
507-
* @param beanName the name of the candidate bean
508-
* @param bf the {@code BeanFactory} from which to retrieve the named bean
509-
* @return {@code true} if either the bean definition (in the XML case)
510-
* or the bean's factory method (in the {@code @Bean} case) defines a matching
511-
* qualifier value (through {@code <qualifier>} or {@code @Qualifier})
512-
*/
513-
private static boolean isQualifierMatch(String qualifier, String beanName, ConfigurableListableBeanFactory bf) {
514-
if (bf.containsBean(beanName)) {
515-
try {
516-
BeanDefinition bd = bf.getMergedBeanDefinition(beanName);
517-
if (bd instanceof AbstractBeanDefinition) {
518-
AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
519-
AutowireCandidateQualifier candidate = abd.getQualifier(Qualifier.class.getName());
520-
if ((candidate != null && qualifier.equals(candidate.getAttribute(AutowireCandidateQualifier.VALUE_KEY))) ||
521-
qualifier.equals(beanName) || ObjectUtils.containsElement(bf.getAliases(beanName), qualifier)) {
522-
return true;
523-
}
524-
}
525-
if (bd instanceof RootBeanDefinition) {
526-
Method factoryMethod = ((RootBeanDefinition) bd).getResolvedFactoryMethod();
527-
if (factoryMethod != null) {
528-
Qualifier targetAnnotation = factoryMethod.getAnnotation(Qualifier.class);
529-
if (targetAnnotation != null && qualifier.equals(targetAnnotation.value())) {
530-
return true;
531-
}
532-
}
533-
}
534-
}
535-
catch (NoSuchBeanDefinitionException ex) {
536-
// ignore - can't compare qualifiers for a manually registered singleton object
537-
}
538-
}
539-
return false;
540-
}
541-
542436
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright 2002-2012 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.annotation;
18+
19+
import java.lang.reflect.Method;
20+
21+
import java.util.Map;
22+
23+
import org.springframework.beans.factory.BeanFactory;
24+
import org.springframework.beans.factory.BeanFactoryUtils;
25+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
26+
import org.springframework.beans.factory.config.BeanDefinition;
27+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
28+
import org.springframework.beans.factory.support.AbstractBeanDefinition;
29+
import org.springframework.beans.factory.support.AutowireCandidateQualifier;
30+
import org.springframework.beans.factory.support.RootBeanDefinition;
31+
import org.springframework.util.ObjectUtils;
32+
33+
/**
34+
* Convenience methods performing bean lookups related to annotations, for example
35+
* Spring's {@link Qualifier @Qualifier} annotation.
36+
*
37+
* @author Chris Beams
38+
* @since 3.2
39+
* @see BeanFactoryUtils
40+
*/
41+
public class BeanFactoryAnnotationUtils {
42+
43+
/**
44+
* Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a
45+
* qualifier (e.g. via {@code <qualifier>} or {@code @Qualifier}) matching the given
46+
* qualifier, or having a bean name matching the given qualifier.
47+
* @param bf the BeanFactory to get the target bean from
48+
* @param beanType the type of bean to retrieve
49+
* @param qualifier the qualifier for selecting between multiple bean matches
50+
* @return the matching bean of type {@code T} (never {@code null})
51+
* @throws IllegalStateException if no matching bean of type {@code T} found
52+
* @since 3.2
53+
*/
54+
public static <T> T qualifiedBeanOfType(BeanFactory beanFactory, Class<T> beanType, String qualifier) {
55+
if (beanFactory instanceof ConfigurableListableBeanFactory) {
56+
// Full qualifier matching supported.
57+
return qualifiedBeanOfType((ConfigurableListableBeanFactory) beanFactory, beanType, qualifier);
58+
}
59+
else if (beanFactory.containsBean(qualifier)) {
60+
// Fallback: target bean at least found by bean name.
61+
return beanFactory.getBean(qualifier, beanType);
62+
}
63+
else {
64+
throw new IllegalStateException("No matching " + beanType.getSimpleName() +
65+
" bean found for bean name '" + qualifier +
66+
"'! (Note: Qualifier matching not supported because given " +
67+
"BeanFactory does not implement ConfigurableListableBeanFactory.)");
68+
}
69+
}
70+
71+
/**
72+
* Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a
73+
* qualifier (e.g. {@code <qualifier>} or {@code @Qualifier}) matching the given
74+
* qualifier
75+
* @param bf the BeanFactory to get the target bean from
76+
* @param beanType the type of bean to retrieve
77+
* @param qualifier the qualifier for selecting between multiple bean matches
78+
* @return the matching bean of type {@code T} (never {@code null})
79+
* @throws IllegalStateException if no matching bean of type {@code T} found
80+
*/
81+
private static <T> T qualifiedBeanOfType(ConfigurableListableBeanFactory bf, Class<T> beanType, String qualifier) {
82+
Map<String, T> candidateBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, beanType);
83+
T matchingBean = null;
84+
for (String beanName : candidateBeans.keySet()) {
85+
if (isQualifierMatch(qualifier, beanName, bf)) {
86+
if (matchingBean != null) {
87+
throw new IllegalStateException("No unique " + beanType.getSimpleName() +
88+
" bean found for qualifier '" + qualifier + "'");
89+
}
90+
matchingBean = candidateBeans.get(beanName);
91+
}
92+
}
93+
if (matchingBean != null) {
94+
return matchingBean;
95+
}
96+
else {
97+
throw new IllegalStateException("No matching " + beanType.getSimpleName() +
98+
" bean found for qualifier '" + qualifier + "' - neither qualifier " +
99+
"match nor bean name match!");
100+
}
101+
}
102+
103+
/**
104+
* Check whether the named bean declares a qualifier of the given name.
105+
* @param qualifier the qualifier to match
106+
* @param beanName the name of the candidate bean
107+
* @param bf the {@code BeanFactory} from which to retrieve the named bean
108+
* @return {@code true} if either the bean definition (in the XML case)
109+
* or the bean's factory method (in the {@code @Bean} case) defines a matching
110+
* qualifier value (through {@code <qualifier>} or {@code @Qualifier})
111+
*/
112+
private static boolean isQualifierMatch(String qualifier, String beanName, ConfigurableListableBeanFactory bf) {
113+
if (bf.containsBean(beanName)) {
114+
try {
115+
BeanDefinition bd = bf.getMergedBeanDefinition(beanName);
116+
if (bd instanceof AbstractBeanDefinition) {
117+
AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
118+
AutowireCandidateQualifier candidate = abd.getQualifier(Qualifier.class.getName());
119+
if ((candidate != null && qualifier.equals(candidate.getAttribute(AutowireCandidateQualifier.VALUE_KEY))) ||
120+
qualifier.equals(beanName) || ObjectUtils.containsElement(bf.getAliases(beanName), qualifier)) {
121+
return true;
122+
}
123+
}
124+
if (bd instanceof RootBeanDefinition) {
125+
Method factoryMethod = ((RootBeanDefinition) bd).getResolvedFactoryMethod();
126+
if (factoryMethod != null) {
127+
Qualifier targetAnnotation = factoryMethod.getAnnotation(Qualifier.class);
128+
if (targetAnnotation != null && qualifier.equals(targetAnnotation.value())) {
129+
return true;
130+
}
131+
}
132+
}
133+
}
134+
catch (NoSuchBeanDefinitionException ex) {
135+
// ignore - can't compare qualifiers for a manually registered singleton object
136+
}
137+
}
138+
return false;
139+
}
140+
141+
}

spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.apache.commons.logging.LogFactory;
3131
import org.springframework.beans.BeansException;
3232
import org.springframework.beans.factory.BeanFactory;
33-
import org.springframework.beans.factory.BeanFactoryUtils;
33+
import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils;
3434
import org.springframework.core.annotation.AnnotationUtils;
3535
import org.springframework.test.annotation.NotTransactional;
3636
import org.springframework.test.annotation.Rollback;
@@ -155,7 +155,7 @@ public String getName() {
155155
// qualifier matching (only exposed on the internal BeanFactory,
156156
// not on the ApplicationContext).
157157
BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();
158-
tm = BeanFactoryUtils.qualifiedBeanOfType(bf, PlatformTransactionManager.class, qualifier);
158+
tm = BeanFactoryAnnotationUtils.qualifiedBeanOfType(bf, PlatformTransactionManager.class, qualifier);
159159
}
160160
else {
161161
tm = getTransactionManager(testContext);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.beans.factory.BeanFactoryUtils;
2828
import org.springframework.beans.factory.InitializingBean;
2929
import org.springframework.beans.factory.ListableBeanFactory;
30+
import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils;
3031
import org.springframework.core.NamedThreadLocal;
3132
import org.springframework.transaction.NoTransactionException;
3233
import org.springframework.transaction.PlatformTransactionManager;
@@ -242,7 +243,7 @@ protected PlatformTransactionManager determineTransactionManager(TransactionAttr
242243
}
243244
String qualifier = txAttr.getQualifier();
244245
if (StringUtils.hasLength(qualifier)) {
245-
return BeanFactoryUtils.qualifiedBeanOfType(this.beanFactory, PlatformTransactionManager.class, qualifier);
246+
return BeanFactoryAnnotationUtils.qualifiedBeanOfType(this.beanFactory, PlatformTransactionManager.class, qualifier);
246247
}
247248
else if (this.transactionManagerBeanName != null) {
248249
return this.beanFactory.getBean(this.transactionManagerBeanName, PlatformTransactionManager.class);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.springframework.beans.factory.BeanFactory;
2020
import org.springframework.beans.factory.BeanFactoryUtils;
21+
import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils;
2122
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2223
import org.springframework.transaction.PlatformTransactionManager;
2324

@@ -40,10 +41,10 @@ public abstract class TransactionAspectUtils {
4041
* @return the chosen {@code PlatformTransactionManager} (never {@code null})
4142
* @throws IllegalStateException if no matching {@code PlatformTransactionManager} bean found
4243
* @deprecated as of Spring 3.2 in favor of
43-
* {@link BeanFactoryUtils#qualifiedBeanOfType(BeanFactory, Class, String)}
44+
* {@link BeanFactoryAnnotationUtils#qualifiedBeanOfType(BeanFactory, Class, String)}
4445
*/
4546
public static PlatformTransactionManager getTransactionManager(BeanFactory beanFactory, String qualifier) {
46-
return BeanFactoryUtils.qualifiedBeanOfType(beanFactory, PlatformTransactionManager.class, qualifier);
47+
return BeanFactoryAnnotationUtils.qualifiedBeanOfType(beanFactory, PlatformTransactionManager.class, qualifier);
4748
}
4849

4950
/**
@@ -53,10 +54,10 @@ public static PlatformTransactionManager getTransactionManager(BeanFactory beanF
5354
* @return the chosen {@code PlatformTransactionManager} (never {@code null})
5455
* @throws IllegalStateException if no matching {@code PlatformTransactionManager} bean found
5556
* @deprecated as of Spring 3.2 in favor of
56-
* {@link BeanFactoryUtils#qualifiedBeanOfType(BeanFactory, Class, String)}
57+
* {@link BeanFactoryAnnotationUtils#qualifiedBeanOfType(BeanFactory, Class, String)}
5758
*/
5859
public static PlatformTransactionManager getTransactionManager(ConfigurableListableBeanFactory bf, String qualifier) {
59-
return BeanFactoryUtils.qualifiedBeanOfType(bf, PlatformTransactionManager.class, qualifier);
60+
return BeanFactoryAnnotationUtils.qualifiedBeanOfType(bf, PlatformTransactionManager.class, qualifier);
6061
}
6162

6263
}

0 commit comments

Comments
 (0)