微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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);

原文地址:https://www.jb51.cc/spring/432285.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐