Refactor and deprecate TransactionAspectUtils

TransactionAspectUtils contains a number of methods useful in
retrieving a bean by type+qualifier. These methods are functionally
general-purpose save for the hard coding of PlatformTransactionManager
class literals throughout.

This commit generifies these methods and moves them into
BeanFactoryUtils primarily in anticipation of their use by async method
execution interceptors and aspects when performing lookups for qualified
executor beans e.g. via @Async("qualifier").

The public API of TransactionAspectUtils remains backward compatible;
all methods within have been deprecated, and all calls to those methods
throughout the framework refactored to use the new BeanFactoryUtils
variants instead.
This commit is contained in:
Chris Beams
2012-05-19 19:30:58 +03:00
parent e71cd06a46
commit 096693c46f
4 changed files with 135 additions and 99 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -242,7 +242,7 @@ public abstract class TransactionAspectSupport implements BeanFactoryAware, Init
}
String qualifier = txAttr.getQualifier();
if (StringUtils.hasLength(qualifier)) {
return TransactionAspectUtils.getTransactionManager(this.beanFactory, qualifier);
return BeanFactoryUtils.qualifiedBeanOfType(this.beanFactory, PlatformTransactionManager.class, qualifier);
}
else if (this.transactionManagerBeanName != null) {
return this.beanFactory.getBean(this.transactionManagerBeanName, PlatformTransactionManager.class);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,120 +16,47 @@
package org.springframework.transaction.interceptor;
import java.lang.reflect.Method;
import java.util.Map;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.AutowireCandidateQualifier;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.ObjectUtils;
/**
* Utility methods for obtaining a PlatformTransactionManager by
* {@link TransactionAttribute#getQualifier() qualifier value}.
*
* @author Juergen Hoeller
* @author Chris Beams
* @since 3.0.2
* @deprecated as of Spring 3.2 in favor of {@link BeanFactoryUtils}
*/
@Deprecated
public abstract class TransactionAspectUtils {
/**
* Obtain a PlatformTransactionManager from the given BeanFactory,
* matching the given qualifier.
* @param beanFactory the BeanFactory to get the PlatformTransactionManager bean from
* @param qualifier the qualifier for selecting between multiple PlatformTransactionManager matches
* @return the chosen PlatformTransactionManager (never <code>null</code>)
* @throws IllegalStateException if no matching PlatformTransactionManager bean found
* Obtain a PlatformTransactionManager from the given BeanFactory, matching the given qualifier.
* @param beanFactory the BeanFactory to get the {@code PlatformTransactionManager} bean from
* @param qualifier the qualifier for selecting between multiple {@code PlatformTransactionManager} matches
* @return the chosen {@code PlatformTransactionManager} (never {@code null})
* @throws IllegalStateException if no matching {@code PlatformTransactionManager} bean found
* @deprecated as of Spring 3.2 in favor of
* {@link BeanFactoryUtils#qualifiedBeanOfType(BeanFactory, Class, String)}
*/
public static PlatformTransactionManager getTransactionManager(BeanFactory beanFactory, String qualifier) {
if (beanFactory instanceof ConfigurableListableBeanFactory) {
// Full qualifier matching supported.
return getTransactionManager((ConfigurableListableBeanFactory) beanFactory, qualifier);
}
else if (beanFactory.containsBean(qualifier)) {
// Fallback: PlatformTransactionManager at least found by bean name.
return beanFactory.getBean(qualifier, PlatformTransactionManager.class);
}
else {
throw new IllegalStateException("No matching PlatformTransactionManager bean found for bean name '" +
qualifier + "'! (Note: Qualifier matching not supported because given BeanFactory does not " +
"implement ConfigurableListableBeanFactory.)");
}
return BeanFactoryUtils.qualifiedBeanOfType(beanFactory, PlatformTransactionManager.class, qualifier);
}
/**
* Obtain a PlatformTransactionManager from the given BeanFactory,
* matching the given qualifier.
* @param bf the BeanFactory to get the PlatformTransactionManager bean from
* @param qualifier the qualifier for selecting between multiple PlatformTransactionManager matches
* @return the chosen PlatformTransactionManager (never <code>null</code>)
* @throws IllegalStateException if no matching PlatformTransactionManager bean found
* Obtain a PlatformTransactionManager from the given BeanFactory, matching the given qualifier.
* @param bf the BeanFactory to get the {@code PlatformTransactionManager} bean from
* @param qualifier the qualifier for selecting between multiple {@code PlatformTransactionManager} matches
* @return the chosen {@code PlatformTransactionManager} (never {@code null})
* @throws IllegalStateException if no matching {@code PlatformTransactionManager} bean found
* @deprecated as of Spring 3.2 in favor of
* {@link BeanFactoryUtils#qualifiedBeanOfType(BeanFactory, Class, String)}
*/
public static PlatformTransactionManager getTransactionManager(ConfigurableListableBeanFactory bf, String qualifier) {
Map<String, PlatformTransactionManager> tms =
BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, PlatformTransactionManager.class);
PlatformTransactionManager chosen = null;
for (String beanName : tms.keySet()) {
if (isQualifierMatch(qualifier, beanName, bf)) {
if (chosen != null) {
throw new IllegalStateException("No unique PlatformTransactionManager bean found " +
"for qualifier '" + qualifier + "'");
}
chosen = tms.get(beanName);
}
}
if (chosen != null) {
return chosen;
}
else {
throw new IllegalStateException("No matching PlatformTransactionManager bean found for qualifier '" +
qualifier + "' - neither qualifier match nor bean name match!");
}
}
/**
* Check whether we have a qualifier match for the given candidate bean.
* @param qualifier the qualifier that we are looking for
* @param beanName the name of the candidate bean
* @param bf the BeanFactory to get the bean definition from
* @return <code>true</code> if either the bean definition (in the XML case)
* or the bean's factory method (in the @Bean case) defines a matching qualifier
* value (through &lt;qualifier<&gt; or @Qualifier)
*/
private static boolean isQualifierMatch(String qualifier, String beanName, ConfigurableListableBeanFactory bf) {
if (bf.containsBean(beanName)) {
try {
BeanDefinition bd = bf.getMergedBeanDefinition(beanName);
if (bd instanceof AbstractBeanDefinition) {
AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
AutowireCandidateQualifier candidate = abd.getQualifier(Qualifier.class.getName());
if ((candidate != null && qualifier.equals(candidate.getAttribute(AutowireCandidateQualifier.VALUE_KEY))) ||
qualifier.equals(beanName) || ObjectUtils.containsElement(bf.getAliases(beanName), qualifier)) {
return true;
}
}
if (bd instanceof RootBeanDefinition) {
Method factoryMethod = ((RootBeanDefinition) bd).getResolvedFactoryMethod();
if (factoryMethod != null) {
Qualifier targetAnnotation = factoryMethod.getAnnotation(Qualifier.class);
if (targetAnnotation != null && qualifier.equals(targetAnnotation.value())) {
return true;
}
}
}
}
catch (NoSuchBeanDefinitionException ex) {
// ignore - can't compare qualifiers for a manually registered singleton object
}
}
return false;
return BeanFactoryUtils.qualifiedBeanOfType(bf, PlatformTransactionManager.class, qualifier);
}
}