spring --解析自定义注解SpringAOP(配合@Aspect)
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)的更多相关文章
- 利用Spring AOP自定义注解解决日志和签名校验
转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...
- (转)利用Spring AOP自定义注解解决日志和签名校验
一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: ...
- spring AOP自定义注解方式实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
- spring AOP自定义注解 实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
- 使用Spring Aop自定义注解实现自动记录日志
百度加自己琢磨,以下亲测有效,所以写下来记录,也方便自己回顾浏览加深印象之类,有什么问题可以评论一起解决,不完整之处也请大佬指正,一起进步哈哈(1)首先配置文件: <!-- 声明自动为sprin ...
- JAVA自定义注解SpringAOP
原文:https://my.oschina.net/wangnian/blog/801348 前言:Annotation(注解)是JDK5.0及以后版本引入的,它的作用就是负责注解其他注解.现在开发过 ...
- Spring aop+自定义注解统一记录用户行为日志
写在前面 本文不涉及过多的Spring aop基本概念以及基本用法介绍,以实际场景使用为主. 场景 我们通常有这样一个需求:打印后台接口请求的具体参数,打印接口请求的最终响应结果,以及记录哪个用户在什 ...
- Spring Cache 自定义注解
1.在使用spring cache注解如cacheable.cacheevict.cacheput过程中有一些问题: 比如,我们在查到一个list后,可以将list缓存到一个键对应的区域里:当新增.修 ...
- 结合spring 实现自定义注解
注解类 import java.lang.annotation.*; /** * Created by Administrator on 2016/6/28. */ //ElementType.MET ...
随机推荐
- eclipse调整字体大小
window->preferences->general->appearance->colors and fonts-> 双击Text Font 就调整字体大小了
- NodeJS NPM HTTPS
npm config set registry http://registry.npmjs.org/
- jquery Treeview插件的使用及复选框的级联
本文是对jquery的Treeview插件使用的实例介绍 效果图如下: 文件结构如下:
- SpringBoot Web项目中中如何使用Junit
Junit这种老技术,现在又拿出来说,不为别的,某种程度上来说,更是为了要说明它在项目中的重要性. 凭本人的感觉和经验来说,在项目中完全按标准都写Junit用例覆盖大部分业务代码的,应该不会超过一半. ...
- ORA-00257: archiver error的解决方法
背景:多个用户同时做测试数据,有时候突然Oracle系统就崩溃了,然后报一个ORA-00257: archiver error. Connect internal only, until freed的 ...
- elasticsearch 5.x集群安装
1. 下载 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.1.tar.gz 2. 解压 为便于 ...
- LSTM java 实现
由于实验室事情缘故,需要将Python写的神经网络转成Java版本的,但是python中的numpy等啥包也不知道在Java里面对应的是什么工具,所以索性直接寻找一个现成可用的Java神经网络框架,于 ...
- 20145331 《Java程序设计》第3周学习总结
20145331 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 认识对象 •对象(Object):存在的具体实体,具有明确的状态和行为 •类(Class):具有相同属性和行 ...
- Qt+VS编译出错:Two or more files with the name of moc_Geometry.cpp will produce outputs to the same location
warning MSB8027: Two or more files with the name of moc_Geometry.cpp will produce outputs to the sam ...
- MySQL_ERROR 1231 (42000) at line XX in file 'file_name' Variable 'time_zone' can't be
类似的错误信息如下 ERROR 1231 (42000) at line 10370 in file: '/root/sql/ultrax_170322.dmp': Variable 'time_zo ...