|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2011 the original author or authors. |
| 2 | + * Copyright 2002-2012 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.transaction.interceptor;
|
18 | 18 |
|
19 |
| -import java.lang.reflect.Method; |
20 |
| -import java.util.Map; |
21 |
| - |
22 | 19 | import org.springframework.beans.factory.BeanFactory;
|
23 | 20 | import org.springframework.beans.factory.BeanFactoryUtils;
|
24 |
| -import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
25 |
| -import org.springframework.beans.factory.annotation.Qualifier; |
26 |
| -import org.springframework.beans.factory.config.BeanDefinition; |
27 | 21 | 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 | 22 | import org.springframework.transaction.PlatformTransactionManager;
|
32 |
| -import org.springframework.util.ObjectUtils; |
33 | 23 |
|
34 | 24 | /**
|
35 | 25 | * Utility methods for obtaining a PlatformTransactionManager by
|
36 | 26 | * {@link TransactionAttribute#getQualifier() qualifier value}.
|
37 | 27 | *
|
38 | 28 | * @author Juergen Hoeller
|
| 29 | + * @author Chris Beams |
39 | 30 | * @since 3.0.2
|
| 31 | + * @deprecated as of Spring 3.2 in favor of {@link BeanFactoryUtils} |
40 | 32 | */
|
| 33 | +@Deprecated |
41 | 34 | public abstract class TransactionAspectUtils {
|
42 | 35 |
|
43 | 36 | /**
|
44 |
| - * Obtain a PlatformTransactionManager from the given BeanFactory, |
45 |
| - * matching the given qualifier. |
46 |
| - * @param beanFactory the BeanFactory to get the PlatformTransactionManager bean from |
47 |
| - * @param qualifier the qualifier for selecting between multiple PlatformTransactionManager matches |
48 |
| - * @return the chosen PlatformTransactionManager (never <code>null</code>) |
49 |
| - * @throws IllegalStateException if no matching PlatformTransactionManager bean found |
| 37 | + * Obtain a PlatformTransactionManager from the given BeanFactory, matching the given qualifier. |
| 38 | + * @param beanFactory the BeanFactory to get the {@code PlatformTransactionManager} bean from |
| 39 | + * @param qualifier the qualifier for selecting between multiple {@code PlatformTransactionManager} matches |
| 40 | + * @return the chosen {@code PlatformTransactionManager} (never {@code null}) |
| 41 | + * @throws IllegalStateException if no matching {@code PlatformTransactionManager} bean found |
| 42 | + * @deprecated as of Spring 3.2 in favor of |
| 43 | + * {@link BeanFactoryUtils#qualifiedBeanOfType(BeanFactory, Class, String)} |
50 | 44 | */
|
51 | 45 | public static PlatformTransactionManager getTransactionManager(BeanFactory beanFactory, String qualifier) {
|
52 |
| - if (beanFactory instanceof ConfigurableListableBeanFactory) { |
53 |
| - // Full qualifier matching supported. |
54 |
| - return getTransactionManager((ConfigurableListableBeanFactory) beanFactory, qualifier); |
55 |
| - } |
56 |
| - else if (beanFactory.containsBean(qualifier)) { |
57 |
| - // Fallback: PlatformTransactionManager at least found by bean name. |
58 |
| - return beanFactory.getBean(qualifier, PlatformTransactionManager.class); |
59 |
| - } |
60 |
| - else { |
61 |
| - throw new IllegalStateException("No matching PlatformTransactionManager bean found for bean name '" + |
62 |
| - qualifier + "'! (Note: Qualifier matching not supported because given BeanFactory does not " + |
63 |
| - "implement ConfigurableListableBeanFactory.)"); |
64 |
| - } |
| 46 | + return BeanFactoryUtils.qualifiedBeanOfType(beanFactory, PlatformTransactionManager.class, qualifier); |
65 | 47 | }
|
66 | 48 |
|
67 | 49 | /**
|
68 |
| - * Obtain a PlatformTransactionManager from the given BeanFactory, |
69 |
| - * matching the given qualifier. |
70 |
| - * @param bf the BeanFactory to get the PlatformTransactionManager bean from |
71 |
| - * @param qualifier the qualifier for selecting between multiple PlatformTransactionManager matches |
72 |
| - * @return the chosen PlatformTransactionManager (never <code>null</code>) |
73 |
| - * @throws IllegalStateException if no matching PlatformTransactionManager bean found |
| 50 | + * Obtain a PlatformTransactionManager from the given BeanFactory, matching the given qualifier. |
| 51 | + * @param bf the BeanFactory to get the {@code PlatformTransactionManager} bean from |
| 52 | + * @param qualifier the qualifier for selecting between multiple {@code PlatformTransactionManager} matches |
| 53 | + * @return the chosen {@code PlatformTransactionManager} (never {@code null}) |
| 54 | + * @throws IllegalStateException if no matching {@code PlatformTransactionManager} bean found |
| 55 | + * @deprecated as of Spring 3.2 in favor of |
| 56 | + * {@link BeanFactoryUtils#qualifiedBeanOfType(BeanFactory, Class, String)} |
74 | 57 | */
|
75 | 58 | public static PlatformTransactionManager getTransactionManager(ConfigurableListableBeanFactory bf, String qualifier) {
|
76 |
| - Map<String, PlatformTransactionManager> tms = |
77 |
| - BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, PlatformTransactionManager.class); |
78 |
| - PlatformTransactionManager chosen = null; |
79 |
| - for (String beanName : tms.keySet()) { |
80 |
| - if (isQualifierMatch(qualifier, beanName, bf)) { |
81 |
| - if (chosen != null) { |
82 |
| - throw new IllegalStateException("No unique PlatformTransactionManager bean found " + |
83 |
| - "for qualifier '" + qualifier + "'"); |
84 |
| - } |
85 |
| - chosen = tms.get(beanName); |
86 |
| - } |
87 |
| - } |
88 |
| - if (chosen != null) { |
89 |
| - return chosen; |
90 |
| - } |
91 |
| - else { |
92 |
| - throw new IllegalStateException("No matching PlatformTransactionManager bean found for qualifier '" + |
93 |
| - qualifier + "' - neither qualifier match nor bean name match!"); |
94 |
| - } |
95 |
| - } |
96 |
| - |
97 |
| - /** |
98 |
| - * Check whether we have a qualifier match for the given candidate bean. |
99 |
| - * @param qualifier the qualifier that we are looking for |
100 |
| - * @param beanName the name of the candidate bean |
101 |
| - * @param bf the BeanFactory to get the bean definition from |
102 |
| - * @return <code>true</code> if either the bean definition (in the XML case) |
103 |
| - * or the bean's factory method (in the @Bean case) defines a matching qualifier |
104 |
| - * value (through <qualifier<> or @Qualifier) |
105 |
| - */ |
106 |
| - private static boolean isQualifierMatch(String qualifier, String beanName, ConfigurableListableBeanFactory bf) { |
107 |
| - if (bf.containsBean(beanName)) { |
108 |
| - try { |
109 |
| - BeanDefinition bd = bf.getMergedBeanDefinition(beanName); |
110 |
| - if (bd instanceof AbstractBeanDefinition) { |
111 |
| - AbstractBeanDefinition abd = (AbstractBeanDefinition) bd; |
112 |
| - AutowireCandidateQualifier candidate = abd.getQualifier(Qualifier.class.getName()); |
113 |
| - if ((candidate != null && qualifier.equals(candidate.getAttribute(AutowireCandidateQualifier.VALUE_KEY))) || |
114 |
| - qualifier.equals(beanName) || ObjectUtils.containsElement(bf.getAliases(beanName), qualifier)) { |
115 |
| - return true; |
116 |
| - } |
117 |
| - } |
118 |
| - if (bd instanceof RootBeanDefinition) { |
119 |
| - Method factoryMethod = ((RootBeanDefinition) bd).getResolvedFactoryMethod(); |
120 |
| - if (factoryMethod != null) { |
121 |
| - Qualifier targetAnnotation = factoryMethod.getAnnotation(Qualifier.class); |
122 |
| - if (targetAnnotation != null && qualifier.equals(targetAnnotation.value())) { |
123 |
| - return true; |
124 |
| - } |
125 |
| - } |
126 |
| - } |
127 |
| - } |
128 |
| - catch (NoSuchBeanDefinitionException ex) { |
129 |
| - // ignore - can't compare qualifiers for a manually registered singleton object |
130 |
| - } |
131 |
| - } |
132 |
| - return false; |
| 59 | + return BeanFactoryUtils.qualifiedBeanOfType(bf, PlatformTransactionManager.class, qualifier); |
133 | 60 | }
|
134 | 61 |
|
135 | 62 | }
|
0 commit comments