1:首先,声明自定义注解

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface DtTransactional { /*
* Whether need to rollback
*/
public boolean includeLocalTransaction() default true; public boolean confirmMethodExist() default true; /*
* Allow [confirmMethod] is null if [confirmMethodExist] is false
*/
public String confirmMethod() default ""; public String cancelMethod(); }

自定义注解定义的属性方法,如果没有 default “” ,则使用该注解时该属性为必填 ;

2:定义切面处理类

@Aspect
@Component
@Slf4j
public class DistributedTransactionAspect{ @Autowired
private DistributedTransactionInterceptor distributedTransactionInterceptor; @Pointcut("@annotation(com.sysware.cloud.commons.dts.annotation.DtTransactional)")
public void distributedTransactionService() { } @Around("distributedTransactionService()")
public Object interceptDtTransactionalMethod(ProceedingJoinPoint pjp) throws Throwable {
log.debug("interface-ITransactionRunning-start---->");
Object obj = distributedTransactionInterceptor.interceptDtTransactionalMethod(pjp);
log.debug("interface-ITransactionRunning-end---->");
return obj;
}
}

定义切面处理类关键点:

  1:类上面用@Aspect 注解修饰 。

2:定义切点,用@Pointcut("@annotation(com.sysware.cloud.commons.dts.annotation.DtTransactional)") 注解定义切点,

  表示扫描所有用@DtTransactional注解标识的方法 。

3:用@Around @Before @After 注解标识,标识拦截方法的时机 ;@Before是在所拦截方法执行之前执行一段逻辑。

  @After 是在所拦截方法执行之后执行一段逻辑。@Around是可以同时在所拦截方法的前后执行一段逻辑。

4:参数ProceedingJoinPoint pjp 通过pjp 可以获取注解信息,注解的参数,方法名,方法参数类型,方法参数等数据,然后对数据做一些统一处理。

spring --解析自定义注解SpringAOP(配合@Aspect)的更多相关文章

  1. 利用Spring AOP自定义注解解决日志和签名校验

    转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...

  2. (转)利用Spring AOP自定义注解解决日志和签名校验

    一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: ...

  3. spring AOP自定义注解方式实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

  4. spring AOP自定义注解 实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

  5. 使用Spring Aop自定义注解实现自动记录日志

    百度加自己琢磨,以下亲测有效,所以写下来记录,也方便自己回顾浏览加深印象之类,有什么问题可以评论一起解决,不完整之处也请大佬指正,一起进步哈哈(1)首先配置文件: <!-- 声明自动为sprin ...

  6. JAVA自定义注解SpringAOP

    原文:https://my.oschina.net/wangnian/blog/801348 前言:Annotation(注解)是JDK5.0及以后版本引入的,它的作用就是负责注解其他注解.现在开发过 ...

  7. Spring aop+自定义注解统一记录用户行为日志

    写在前面 本文不涉及过多的Spring aop基本概念以及基本用法介绍,以实际场景使用为主. 场景 我们通常有这样一个需求:打印后台接口请求的具体参数,打印接口请求的最终响应结果,以及记录哪个用户在什 ...

  8. Spring Cache 自定义注解

    1.在使用spring cache注解如cacheable.cacheevict.cacheput过程中有一些问题: 比如,我们在查到一个list后,可以将list缓存到一个键对应的区域里:当新增.修 ...

  9. 结合spring 实现自定义注解

    注解类 import java.lang.annotation.*; /** * Created by Administrator on 2016/6/28. */ //ElementType.MET ...

随机推荐

  1. Android 4.4 音量调节流程分析(一)

    最近在做Android Audio方面的工作,有需求是在调节Volume_Up_Key & Volume_Down_key时,Spearker or Headset每音阶的衰减变为3db左右. ...

  2. RedisTemplate访问Redis数据结构

    https://www.jianshu.com/p/7bf5dc61ca06 Redis 数据结构简介 Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字 ...

  3. composer错误_Content-Length mismatch, received 84697 bytes out of the expected..

    使用composer下载源码出现错误 [Composer\Downloader\TransportException] Content-Length mismatch, received bytes ...

  4. web.xml<context-param>与<init-param>的作用与区别

    <context-param>的作用:web.xml的配置中<context-param>配置作用1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件we ...

  5. IDEA 安装插件报错 Plugin Lombok Plugin was not installed: Cannot download

    报错 解决 成功下载插件 3

  6. JAVA面试题整理(1)-基础

    1.List 和 Set 的区别  共同点:它们都是Collection的子接口 区别: List:这个接口能够精准的记录每一个元素的插入位置(换句话说就是这个接口内容所有元素是按照顺序去保存的),使 ...

  7. hive + hadoop 环境搭建

    机器规划: 主机 ip 进程 hadoop1 10.183.225.158 hive server hadoop2 10.183.225.166 hive client 前置条建: kerberos部 ...

  8. bzoj 3545: [ONTAK2010]Peaks

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1124  Solved: 304[Submit][Status][Discuss] Descripti ...

  9. 事务的学习,从jdbc开始:jdbc对事务的支持与实现

    在使用spring对项目进行开发时,所有的事务都是由spring来管理的.这样一来我们就可以不需要操心事务,可以专心的处理业务代码. 但是,事务的底层究竟是如何实现的呢?那就从jdbc开始学习. 在使 ...

  10. Scrapy之Scrapy shell

    Scrapy Shell Scrapy终端是一个交互终端,我们可以在未启动spider的情况下尝试及调试代码,也可以用来测试XPath或CSS表达式,查看他们的工作方式,方便我们爬取的网页中提取的数据 ...