提交 6b7c1d72 编写于 作者: S Sam Brannen

Introduce alias for 'value' attribute in @Transactional

Issue: SPR-11393
上级 6fc38831
/*
* 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");
......
......@@ -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 <em>qualifier</em> value for the specified transaction.
* <p>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.
......
......@@ -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;
......
......@@ -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();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册