如果这是你第二次看到师长,说明你在觊觎我的美色!
点赞+关注再看,养成习惯
没别的意思,就是需要你的窥屏^_^
该趟专车是开往基于spring boot事务思想实战的专车,在上一篇 搭上springboot事务源码分析专车[1]中我们详细介绍了spring boot事务实现的原理,这一篇是基于上一篇的实战。
在实战之前,我们再次回顾下上篇文章讲解的重点:
实现是以spring boot为基础,需要添加如下依赖
<dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> </dependency> </dependencies>
按照如上提到的问题依次定义
定义bean后置处理器,特别注意,如果项目中使用到了事务特性,就不需要重复定义
/** * 一定要声明infrastructureadvisorautoproxycreator,用于实现bean的后置处理 * * @return */ @bean public infrastructureadvisorautoproxycreator infrastructureadvisorautoproxycreator() { return new infrastructureadvisorautoproxycreator(); }
定义切面
public class beanfactorysystemlogadvisor extends abstractbeanfactorypointcutadvisor { /** * 定义切点 */ private final systemlogpointcut point = new systemlogpointcut(); @override public pointcut getpointcut() { return this.point; } }
定义切点
public class systemlogpointcut extends staticmethodmatcherpointcut { @override public boolean matches(method method, class<?> targetclass) { // 查找类上@systemlog注解属性 annotationattributes attributes = annotatedelementutils.findmergedannotationattributes( targetclass, systemlog.class, false, false); if (objects.nonnull(attributes)) { return true; } // 查找方法上@systemlog注解属性 attributes = annotatedelementutils.findmergedannotationattributes( method, systemlog.class, false, false); return objects.nonnull(attributes); } }
定义通知
@slf4j public class systemloginterceptor implements methodinterceptor, serializable { @override public object invoke(methodinvocation invocation) throws throwable { method method = invocation.getmethod(); string classname = method.getdeclaringclass().getsimplename(); string methodname = method.getname(); log.info("======[" + classname + "#" + methodname + " method begin execute]======"); arrays.stream(invocation.getarguments()).foreach(argument -> log.info("======[execute method argument:" + argument + "]======")); long time1 = clock.systemdefaultzone().millis(); object result = invocation.proceed(); long time2 = clock.systemdefaultzone().millis(); log.info("======[method execute time:" + (time2 - time1) + "]======"); return result; } }
自动配置
@configuration public class proxysystemlogconfiguration { /** * 定义切面 * 此处一定要指定@role注解 * * @return */ @role(beandefinition.role_infrastructure) @bean public beanfactorysystemlogadvisor beanfactorysystemlogadvisor() { beanfactorysystemlogadvisor advisor = new beanfactorysystemlogadvisor(); advisor.setadvice(systemloginterceptor()); return advisor; } /** * 定义通知 * * @return */ @bean public systemloginterceptor systemloginterceptor() { return new systemloginterceptor(); } /** * 一定要声明infrastructureadvisorautoproxycreator,用于实现bean的后置处理 * * @return */ @bean public infrastructureadvisorautoproxycreator infrastructureadvisorautoproxycreator() { return new infrastructureadvisorautoproxycreator(); } }
定义注解
@target({elementtype.method, elementtype.type}) @retention(retentionpolicy.runtime) @documented public @interface systemlog { }
定义控制器
@restcontroller public class systemlogcontroller { @autowired private systemlogservice systemlogservice; @getmapping("/log") public string hello(@requestparam("name") string name) throws interruptedexception { return systemlogservice.log(name); } }
定义业务方法
@slf4j @service public class systemlogservice { @systemlog public string log(string name) throws interruptedexception { log.info("执行业务方法"); timeunit.seconds.sleep(1); return "hello " + name; } }
定义启动类
@springbootapplication public class transactionimitateapplication { public static void main(string[] args) { springapplication.run(transactionimitateapplication.class, args); } }
访问http://localhost:8080/log?name=advisor
查看控制台
2019-08-23 11:13:36.029 info 23227 --- [nio-8080-exec-1] c.b.example.config.systemloginterceptor : ======[systemlogservice#log method begin execute]======2019-08-23 11:13:36.030 info 23227 --- [nio-8080-exec-1] c.b.example.config.systemloginterceptor : ======[execute method argument:advisor]======2019-08-23 11:13:36.038 info 23227 --- [nio-8080-exec-1] c.boot.example.service.systemlogservice : 执行业务方法2019-08-23 11:13:37.038 info 23227 --- [nio-8080-exec-1] c.b.example.config.systemloginterceptor : ======[method execute time:1004]======
可以看到通过模拟@transaction注解的实现方式,完成了日志切面功能。
回顾下开头的五个问题:
师长,【java进阶架构师】号主,短短一年在各大平台斩获15w+程序员关注,专注分享java进阶、架构技术、高并发、微服务、bat面试、redis专题、jvm调优、springboot源码、mysql优化等20大进阶架构专题,关注【java进阶架构师】回复【架构】领取2019架构师完整视频一套。
转载说明:请务必注明来源(本文首发于公众号:【java进阶架构师】)
如对本文有疑问, 点击进行留言回复!!
IntelliJ IDEA 自定义控制台输出多颜色格式功能 --- 安装Grep Console插件
2020苹果cmsv10非海螺模板苹果cms模板苹果cms10好看的模板带多功能后台
JavaWeb阶段 - Cookie与Session学习总结
网友评论