java – @Transactional的自定义快捷方式注释无法正常工作

我正在尝试创建自定义注释以便快捷方式,就像在文档中引用一样:

   @Target({ElementType.METHOD,ElementType.TYPE})
   @Retention(RetentionPolicy.RUNTIME)
   @Transactional("order")
   public @interface OrderTx {
   }

但是当我使用自定义注释注释方法时,我得到一个例外:

No hibernate session bound to thread,and configuration does not allow creation…

使用@Transactional注释方法效果很好.

由于我正在注释的方法不属于从应用程序上下文实例化的Bean,我的猜测是AnnotationTransactionAspect不能使用自定义注释,并且AOP魔法不起作用.

如何获得快捷方式的自定义注释并在任何地方工作?

我错过了别的什么吗?

最佳答案
以下是AnnotationTransactionAspect中使用的切入点:

/**
 * Matches the execution of any public method in a type with the
 * Transactional annotation,or any subtype of a type with the
 * Transactional annotation.
 */
private pointcut executionOfAnyPublicmethodInAtTransactionalType() :
    execution(public * ((@Transactional *)+).*(..)) && @this(Transactional);

/**
 * Matches the execution of any method with the 
 * Transactional annotation.
 */
private pointcut executionOfTransactionalMethod() :
    execution(* *(..)) && @annotation(Transactional);

我会说非常清楚元注释不匹配(我甚至不认为有一个有效的aspectj切入点可以捕获元注释).所以我想你必须继承AbstractTransactionAspect并为这个切入点提供你自己的实现来捕获你的自定义注释:

/**
 * Concrete subaspects must implement this pointcut,to identify
 * transactional methods. For each selected joinpoint,TransactionMetadata
 * will be retrieved using Spring's TransactionAttributeSource interface.
 */
protected abstract pointcut transactionalMethodExecution(Object txObject);

相关文章

这篇文章主要介绍了spring的事务传播属性REQUIRED_NESTED的原...
今天小编给大家分享的是一文解析spring中事务的传播机制,相...
这篇文章主要介绍了SpringCloudAlibaba和SpringCloud有什么区...
本篇文章和大家了解一下SpringCloud整合XXL-Job的几个步骤。...
本篇文章和大家了解一下Spring延迟初始化会遇到什么问题。有...
这篇文章主要介绍了怎么使用Spring提供的不同缓存注解实现缓...