【发布时间】:2011-10-08 12:20:59
【问题描述】:
通过xml配置Spring事务时,是绑定接口好还是具体好 交易类?我在想我们应该映射接口 事务处理,但我从 Spring 文档中找到了这个: “Spring 团队的建议是你只用 @Transactional 注释”。
请分享你的想法。
【问题讨论】:
标签: java spring interface transactions annotations
通过xml配置Spring事务时,是绑定接口好还是具体好 交易类?我在想我们应该映射接口 事务处理,但我从 Spring 文档中找到了这个: “Spring 团队的建议是你只用 @Transactional 注释”。
请分享你的想法。
【问题讨论】:
标签: java spring interface transactions annotations
事务边界是实现细节,因此它们应该由实现而不是接口来指定。
例如,假设在某些复杂情况下,您需要使用程序化事务管理 (TransactionTemplate) 而不是 @Transactional 作为您的一种方法。如果您在接口级别有@Transactional,那将是不可能的。
【讨论】:
接口是合约。交易性(通常)不是合同的一部分。这是一个实现细节。因此@Transactional 属于实现类,而不是接口。
【讨论】:
@Transactional 注释不是从接口继承的,所以如果您使用不同的代理方法(例如 proxy-target-class=true 或 aspectj )你的真实方法不会是事务性的。
参见 proxy-aop(用于标准事务注释) http://static.springsource.org/spring/docs/3.0.x/reference/aop.html#aop-understanding-aop-proxies
【讨论】: