若是这是你第二次看到师长,说明你在觊觎个人美色!java
点赞+关注再看,养成习惯mysql
没别的意思,就是须要你的窥屏^_^程序员
该趟专车是开往基于Spring Boot事务思想实战的专车,在上一篇 搭上SpringBoot事务源码分析专车[1]中咱们详细介绍了Spring Boot事务实现的原理,这一篇是基于上一篇的实战。web
在实战以前,咱们再次回顾下上篇文章讲解的重点:面试
实现是以Spring Boot为基础,须要添加以下依赖redis
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
按照如上提到的问题依次定义spring
定义bean后置处理器,特别注意,若是项目中使用到了事务特性,就不须要重复定义sql
/** * 必定要声明InfrastructureAdvisorAutoProxyCreator,用于实现bean的后置处理 * * @return */ @Bean public InfrastructureAdvisorAutoProxyCreator infrastructureAdvisorAutoProxyCreator() { return new InfrastructureAdvisorAutoProxyCreator(); }
定义切面mysql优化
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进阶架构师】)