加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 01:30:38 所属栏目:大数据 来源:网络整理
导读:我正在尝试创建自定义注释以便快捷方式,就像在文档中引用一样: @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Transactional("order") public @interface OrderTx { } 但是当我使用自定义注释注释方法时,我得到一

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读