栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

003 | 搭上基于SpringBoot事务思想实战专车

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

003 | 搭上基于SpringBoot事务思想实战专车

前言

如果这是你第二次看到师长,说明你在觊觎我的美色!

点赞+关注再看,养成习惯

没别的意思,就是需要你的窥屏_

专车介绍

该趟专车是开往基于Spring Boot事务思想实战的专车,在上一篇 搭上SpringBoot事务源码分析专车[1]中我们详细介绍了Spring Boot事务实现的原理,这一篇是基于上一篇的实战。

在实战之前,我们再次回顾下上篇文章讲解的重点:

  • 后置处理器:对Bean进行拦截并处理
  • 切面:由切点和通知组成
  • 切点:用于匹配符合的类和方法
  • 通知:用于代理处理
专车问题
  • 如何利用后置处理器对Bean进行拦截并处理?
  • 如何定义切面?
  • 如何定义切点?
  • 如何定义通知?
  • 如何实现自动配置?
专车分析

实现是以Spring Boot为基础,需要添加如下依赖


    
 org.springframework.boot
 spring-boot-starter-web
    

    
 org.projectlombok
 lombok
    

按照如上提到的问题依次定义

定义bean后置处理器,特别注意,如果项目中使用到了事务特性,就不需要重复定义


@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(BeanDefinition.ROLE_INFRASTRUCTURE)
    @Bean
    public BeanFactorySystemLogAdvisor beanFactorySystemLogAdvisor() {
 BeanFactorySystemLogAdvisor advisor = new BeanFactorySystemLogAdvisor();
 advisor.setAdvice(systemLogInterceptor());
 return advisor;
    }

    
    @Bean
    public SystemLogInterceptor systemLogInterceptor() {
 return new SystemLogInterceptor();
    }

    
    @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注解的实现方式,完成了日志切面功能。

专车总结
  • 首先我们需要定义一个Bean后置处理器,用于拦截处理Bean
  • 然后定义切面,在切面中定义切点
  • 切点中实现切入的逻辑,比如此处我们的实现逻辑就是查找类或方法上是否含有@SystemLog注解
  • 定义通知,完成代理工作
  • 自动装配,将我们的切面、通知、Bean后置处理器声明在配置类中
  • 集成业务
专车回顾

回顾下开头的五个问题:

  • 如何利用后置处理器对Bean进行拦截并处理?直接在配置类中声明后置处理器
  • 如何定义切面?继承AbstractBeanFactoryPointcutAdvisor,并在配置类中中声明
  • 如何定义切点?继承StaticMethodMatcherPointcut,实现matches方法
  • 如何定义通知?实现MethodInterceptor接口,实现invoke方法
  • 如何实现自动配置?自定义配置类,声明所有需要加入容器的Bean
最后

师长,【java进阶架构师】号主,短短一年在各大平台斩获15W+程序员关注,专注分享Java进阶、架构技术、高并发、微服务、BAT面试、redis专题、JVM调优、Springboot源码、mysql优化等20大进阶架构专题。

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号