Skip to content

Commit

Permalink
@transactional qualifiers match against transaction manager definitio…
Browse files Browse the repository at this point in the history
…ns in parent contexts as well (SPR-7679)
  • Loading branch information
jhoeller committed Dec 11, 2011
1 parent d4123d0 commit 0f75ceb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 36 deletions.
Original file line number Original file line Diff line number Diff line change
@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@


import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
Expand Down Expand Up @@ -103,24 +104,29 @@ public static PlatformTransactionManager getTransactionManager(ConfigurableLista
* value (through &lt;qualifier<&gt; or @Qualifier) * value (through &lt;qualifier<&gt; or @Qualifier)
*/ */
private static boolean isQualifierMatch(String qualifier, String beanName, ConfigurableListableBeanFactory bf) { private static boolean isQualifierMatch(String qualifier, String beanName, ConfigurableListableBeanFactory bf) {
if (bf.containsBeanDefinition(beanName)) { if (bf.containsBean(beanName)) {
BeanDefinition bd = bf.getMergedBeanDefinition(beanName); try {
if (bd instanceof AbstractBeanDefinition) { BeanDefinition bd = bf.getMergedBeanDefinition(beanName);
AbstractBeanDefinition abd = (AbstractBeanDefinition) bd; if (bd instanceof AbstractBeanDefinition) {
AutowireCandidateQualifier candidate = abd.getQualifier(Qualifier.class.getName()); AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
if ((candidate != null && qualifier.equals(candidate.getAttribute(AutowireCandidateQualifier.VALUE_KEY))) || AutowireCandidateQualifier candidate = abd.getQualifier(Qualifier.class.getName());
qualifier.equals(beanName) || ObjectUtils.containsElement(bf.getAliases(beanName), qualifier)) { if ((candidate != null && qualifier.equals(candidate.getAttribute(AutowireCandidateQualifier.VALUE_KEY))) ||
return true; qualifier.equals(beanName) || ObjectUtils.containsElement(bf.getAliases(beanName), qualifier)) {
}
}
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; 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 false;
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,8 @@
import org.aopalliance.intercept.MethodInvocation; import org.aopalliance.intercept.MethodInvocation;


import org.springframework.aop.support.AopUtils; 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.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.CallCountingTransactionManager; import org.springframework.transaction.CallCountingTransactionManager;
import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.transaction.support.TransactionSynchronizationManager;
Expand All @@ -36,26 +38,18 @@ public class AnnotationDrivenTests extends TestCase {


public void testWithProxyTargetClass() throws Exception { public void testWithProxyTargetClass() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("annotationDrivenProxyTargetClassTests.xml", getClass()); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("annotationDrivenProxyTargetClassTests.xml", getClass());
CallCountingTransactionManager tm1 = context.getBean("transactionManager1", CallCountingTransactionManager.class); doTestWithMultipleTransactionManagers(context);
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);
} }


public void testWithConfigurationClass() throws Exception { 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 tm1 = context.getBean("transactionManager1", CallCountingTransactionManager.class);
CallCountingTransactionManager tm2 = context.getBean("transactionManager2", CallCountingTransactionManager.class); CallCountingTransactionManager tm2 = context.getBean("transactionManager2", CallCountingTransactionManager.class);
TransactionalService service = context.getBean("service", TransactionalService.class); TransactionalService service = context.getBean("service", TransactionalService.class);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@


<bean id="txCheckingInterceptor" class="org.springframework.transaction.config.AnnotationDrivenTests$TransactionCheckingInterceptor"/> <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"/> <bean id="service" class="org.springframework.transaction.config.TransactionalService"/>


</beans> </beans>

0 comments on commit 0f75ceb

Please sign in to comment.