Browse files

@Transactional qualifiers match against transaction manager definitio…

…ns in parent contexts as well (SPR-7679)
  • Loading branch information...
1 parent d4123d0 commit 0f75cebdff44791c85a62e422d998afe536da1e5 @jhoeller jhoeller committed Dec 11, 2011
View
38 ...ion/src/main/java/org/springframework/transaction/interceptor/TransactionAspectUtils.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2010 the original author or authors.
+ * Copyright 2002-2011 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.
@@ -21,6 +21,7 @@
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;
@@ -103,24 +104,29 @@ public static PlatformTransactionManager getTransactionManager(ConfigurableLista
* value (through &lt;qualifier<&gt; or @Qualifier)
*/
private static boolean isQualifierMatch(String qualifier, String beanName, ConfigurableListableBeanFactory bf) {
- if (bf.containsBeanDefinition(beanName)) {
- 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())) {
+ 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;
View
30 ...ansaction/src/test/java/org/springframework/transaction/config/AnnotationDrivenTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2010 the original author or authors.
+ * Copyright 2002-2011 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.
@@ -23,6 +23,8 @@
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.support.AopUtils;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.CallCountingTransactionManager;
import org.springframework.transaction.support.TransactionSynchronizationManager;
@@ -36,26 +38,18 @@
public void testWithProxyTargetClass() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("annotationDrivenProxyTargetClassTests.xml", getClass());
- CallCountingTransactionManager tm1 = context.getBean("transactionManager1", CallCountingTransactionManager.class);
- CallCountingTransactionManager tm2 = context.getBean("transactionManager2", CallCountingTransactionManager.class);
- TransactionalService service = context.getBean("service", TransactionalService.class);
- assertTrue(AopUtils.isCglibProxy(service));
- service.setSomething("someName");
- assertEquals(1, tm1.commits);
- assertEquals(0, tm2.commits);
- service.doSomething();
- assertEquals(1, tm1.commits);
- assertEquals(1, tm2.commits);
- service.setSomething("someName");
- assertEquals(2, tm1.commits);
- assertEquals(1, tm2.commits);
- service.doSomething();
- assertEquals(2, tm1.commits);
- assertEquals(2, tm2.commits);
+ doTestWithMultipleTransactionManagers(context);
}
public void testWithConfigurationClass() throws Exception {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("annotationDrivenConfigurationClassTests.xml", getClass());
+ AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
+ parent.register(TransactionManagerConfiguration.class);
+ parent.refresh();
+ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"annotationDrivenConfigurationClassTests.xml"}, getClass(), parent);
+ doTestWithMultipleTransactionManagers(context);
+ }
+
+ private void doTestWithMultipleTransactionManagers(ApplicationContext context) {
CallCountingTransactionManager tm1 = context.getBean("transactionManager1", CallCountingTransactionManager.class);
CallCountingTransactionManager tm2 = context.getBean("transactionManager2", CallCountingTransactionManager.class);
TransactionalService service = context.getBean("service", TransactionalService.class);
View
2 ...t/java/org/springframework/transaction/config/annotationDrivenConfigurationClassTests.xml
@@ -18,8 +18,6 @@
<bean id="txCheckingInterceptor" class="org.springframework.transaction.config.AnnotationDrivenTests$TransactionCheckingInterceptor"/>
- <bean id="transactionManagerConfig" class="org.springframework.transaction.config.TransactionManagerConfiguration"/>
-
<bean id="service" class="org.springframework.transaction.config.TransactionalService"/>
</beans>

0 comments on commit 0f75ceb

Please sign in to comment.