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

Raf*_*ael 6 java spring transactions aspectj spring-aop

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

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

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

没有hibernate会话绑定到线程,配置不允许创建...

虽然注释方法与@Transactional完美的工作.

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

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

我错过了别的什么吗?

Sea*_*oyd 2

以下是使用的切入点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);
Run Code Online (Sandbox Code Playgroud)

我想说很明显元注释不匹配(而且我什至不认为有一个有效的切入点可以捕获元注释)。所以我想你必须子类化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);
Run Code Online (Sandbox Code Playgroud)