From 6b7c1d72e855fcee2ba1edb852af3d9f472dc551 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 12 Jun 2015 22:31:41 +0200 Subject: [PATCH] Introduce alias for 'value' attribute in @Transactional Issue: SPR-11393 --- ...nManagersTransactionalSqlScriptsTests.java | 6 +- .../transaction/annotation/Transactional.java | 13 +++- ...ationTransactionNamespaceHandlerTests.java | 5 ++ .../EnableTransactionManagementTests.java | 61 +++++++++---------- 4 files changed, 49 insertions(+), 36 deletions(-) diff --git a/spring-test/src/test/java/org/springframework/test/context/jdbc/MultipleDataSourcesAndTransactionManagersTransactionalSqlScriptsTests.java b/spring-test/src/test/java/org/springframework/test/context/jdbc/MultipleDataSourcesAndTransactionManagersTransactionalSqlScriptsTests.java index 3250692ff1..f1a8cee972 100644 --- a/spring-test/src/test/java/org/springframework/test/context/jdbc/MultipleDataSourcesAndTransactionManagersTransactionalSqlScriptsTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/jdbc/MultipleDataSourcesAndTransactionManagersTransactionalSqlScriptsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -48,7 +48,7 @@ import static org.junit.Assert.*; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @DirtiesContext -@Transactional("txMgr1") +@Transactional(transactionManager = "txMgr1") @SqlConfig(dataSource = "dataSource1", transactionManager = "txMgr1") public class MultipleDataSourcesAndTransactionManagersTransactionalSqlScriptsTests { @@ -66,7 +66,7 @@ public class MultipleDataSourcesAndTransactionManagersTransactionalSqlScriptsTes } @Test - @Transactional("txMgr2") + @Transactional(transactionManager = "txMgr2") @Sql(scripts = "data-add-catbert.sql", config = @SqlConfig(dataSource = "dataSource2", transactionManager = "txMgr2")) public void database2() { assertUsers(new JdbcTemplate(dataSource2), "Dilbert", "Catbert"); diff --git a/spring-tx/src/main/java/org/springframework/transaction/annotation/Transactional.java b/spring-tx/src/main/java/org/springframework/transaction/annotation/Transactional.java index 73a5b2a0b4..82f57607a2 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/annotation/Transactional.java +++ b/spring-tx/src/main/java/org/springframework/transaction/annotation/Transactional.java @@ -23,6 +23,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.core.annotation.AliasFor; import org.springframework.transaction.TransactionDefinition; /** @@ -55,14 +56,24 @@ import org.springframework.transaction.TransactionDefinition; @Documented public @interface Transactional { + /** + * Alias for {@link #transactionManager}. + * @see #transactionManager + */ + @AliasFor(attribute = "transactionManager") + String value() default ""; + /** * A qualifier value for the specified transaction. *

May be used to determine the target transaction manager, * matching the qualifier value (or the bean name) of a specific * {@link org.springframework.transaction.PlatformTransactionManager} * bean definition. + * @since 4.2 + * @see #value */ - String value() default ""; + @AliasFor(attribute = "value") + String transactionManager() default ""; /** * The transaction propagation type. diff --git a/spring-tx/src/test/java/org/springframework/transaction/annotation/AnnotationTransactionNamespaceHandlerTests.java b/spring-tx/src/test/java/org/springframework/transaction/annotation/AnnotationTransactionNamespaceHandlerTests.java index 19406b65d3..de1dd777f8 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/annotation/AnnotationTransactionNamespaceHandlerTests.java +++ b/spring-tx/src/test/java/org/springframework/transaction/annotation/AnnotationTransactionNamespaceHandlerTests.java @@ -37,6 +37,7 @@ import org.springframework.transaction.event.TransactionalEventListenerFactory; /** * @author Rob Harrop * @author Juergen Hoeller + * @author Sam Brannen */ public class AnnotationTransactionNamespaceHandlerTests extends TestCase { @@ -129,6 +130,10 @@ public class AnnotationTransactionNamespaceHandlerTests extends TestCase { public void saveQualifiedFoo() { } + @Transactional(transactionManager = "qualifiedTransactionManager") + public void saveQualifiedFooWithAttributeAlias() { + } + @Transactional public void exceptional(Throwable t) throws Throwable { throw t; diff --git a/spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java b/spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java index 0469f48732..d413269df8 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java +++ b/spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java @@ -17,6 +17,7 @@ package org.springframework.transaction.annotation; import java.util.Map; + import javax.annotation.PostConstruct; import org.junit.Test; @@ -43,52 +44,49 @@ import static org.junit.Assert.*; * * @author Chris Beams * @author Stephane Nicoll + * @author Sam Brannen * @since 3.1 */ public class EnableTransactionManagementTests { @Test public void transactionProxyIsCreated() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(EnableTxConfig.class, TxManagerConfig.class); - ctx.refresh(); + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(EnableTxConfig.class, TxManagerConfig.class); TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class); - assertThat("testBean is not a proxy", AopUtils.isAopProxy(bean), is(true)); + assertTrue("testBean is not a proxy", AopUtils.isAopProxy(bean)); Map services = ctx.getBeansWithAnnotation(Service.class); - assertThat("Stereotype annotation not visible", services.containsKey("testBean"), is(true)); + assertTrue("Stereotype annotation not visible", services.containsKey("testBean")); + ctx.close(); } @Test public void transactionProxyIsCreatedWithEnableOnSuperclass() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(InheritedEnableTxConfig.class, TxManagerConfig.class); - ctx.refresh(); + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(InheritedEnableTxConfig.class, TxManagerConfig.class); TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class); - assertThat("testBean is not a proxy", AopUtils.isAopProxy(bean), is(true)); + assertTrue("testBean is not a proxy", AopUtils.isAopProxy(bean)); Map services = ctx.getBeansWithAnnotation(Service.class); - assertThat("Stereotype annotation not visible", services.containsKey("testBean"), is(true)); + assertTrue("Stereotype annotation not visible", services.containsKey("testBean")); + ctx.close(); } @Test public void txManagerIsResolvedOnInvocationOfTransactionalMethod() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(EnableTxConfig.class, TxManagerConfig.class); - ctx.refresh(); + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(EnableTxConfig.class, TxManagerConfig.class); TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class); // invoke a transactional method, causing the PlatformTransactionManager bean to be resolved. bean.findAllFoos(); + ctx.close(); } @Test public void txManagerIsResolvedCorrectlyWhenMultipleManagersArePresent() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(EnableTxConfig.class, MultiTxManagerConfig.class); - ctx.refresh(); + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(EnableTxConfig.class, MultiTxManagerConfig.class); TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class); // invoke a transactional method, causing the PlatformTransactionManager bean to be resolved. bean.findAllFoos(); + ctx.close(); } /** @@ -103,33 +101,35 @@ public class EnableTransactionManagementTests { "Do you actually have org.springframework.aspects on the classpath?"); } catch (Exception ex) { - assertThat(ex.getMessage().contains("AspectJTransactionManagementConfiguration"), is(true)); + assertThat(ex.getMessage(), containsString("AspectJTransactionManagementConfiguration")); } } @Test public void transactionalEventListenerRegisteredProperly() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(EnableTxConfig.class); - ctx.refresh(); - assertTrue(ctx.containsBean(TransactionManagementConfigUtils - .TRANSACTIONAL_EVENT_LISTENER_FACTORY_BEAN_NAME)); + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(EnableTxConfig.class); + assertTrue(ctx.containsBean(TransactionManagementConfigUtils.TRANSACTIONAL_EVENT_LISTENER_FACTORY_BEAN_NAME)); assertEquals(1, ctx.getBeansOfType(TransactionalEventListenerFactory.class).size()); + ctx.close(); } @Test public void spr11915() { - AnnotationConfigApplicationContext ctx = - new AnnotationConfigApplicationContext(Spr11915Config.class); - + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr11915Config.class); TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class); - bean.saveQualifiedFoo(); + CallCountingTransactionManager txManager = ctx.getBean("qualifiedTransactionManager", CallCountingTransactionManager.class); - CallCountingTransactionManager txManager = ctx - .getBean("qualifiedTransactionManager", CallCountingTransactionManager.class); + bean.saveQualifiedFoo(); assertThat(txManager.begun, equalTo(1)); assertThat(txManager.commits, equalTo(1)); assertThat(txManager.rollbacks, equalTo(0)); + + bean.saveQualifiedFooWithAttributeAlias(); + assertThat(txManager.begun, equalTo(2)); + assertThat(txManager.commits, equalTo(2)); + assertThat(txManager.rollbacks, equalTo(0)); + + ctx.close(); } @@ -138,12 +138,10 @@ public class EnableTransactionManagementTests { static class EnableTxConfig { } - @Configuration static class InheritedEnableTxConfig extends EnableTxConfig { } - @Configuration @EnableTransactionManagement(mode=AdviceMode.ASPECTJ) static class EnableAspectJTxConfig { @@ -168,7 +166,6 @@ public class EnableTransactionManagementTests { } } - @Configuration static class TxManagerConfig { @@ -184,7 +181,6 @@ public class EnableTransactionManagementTests { } - @Configuration static class MultiTxManagerConfig extends TxManagerConfig implements TransactionManagementConfigurer { @@ -198,4 +194,5 @@ public class EnableTransactionManagementTests { return txManager2(); } } + }