欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解使用Spring Boot的AOP处理自定义注解

程序员文章站 2023-12-17 18:27:10
上一篇文章java 注解介绍讲解了下java注解的基本使用方式,并且通过自定义注解实现了一个简单的测试工具;本篇文章将介绍如何使用spring boot的aop来简化处理自...

上一篇文章java 注解介绍讲解了下java注解的基本使用方式,并且通过自定义注解实现了一个简单的测试工具;本篇文章将介绍如何使用spring boot的aop来简化处理自定义注解,并将通过实现一个简单的方法执行时间统计工具为样例来讲解这些内容。

aop概念

面向侧面的程序设计(aspect-oriented programming,aop,又译作面向方面的程序设计、观点导向编程、剖面导向程序设计)是计算机科学中的一个术语,指一种程序设计范型。该范型以一种称为侧面(aspect,又译作方面)的语言构造为基础,侧面是一种新的模块化机制,用来描述分散在对象、类或函数中的横切关注点(crosscutting concern)。

侧面的概念源于对面向对象的程序设计的改进,但并不只限于此,它还可以用来改进传统的函数。与侧面相关的编程概念还包括元对象协议、主题(subject)、混入(mixin)和委托。

注释:以上定义源自中文*(如果访问不了,可以通过修改系统的hosts文件访问, 198.35.26.96 zh.wikipedia.org #中文* ,只能帮到这了,如果还是上不了,那就麻烦上网搜索下怎么修改系统的hosts文件,不同系统下hosts文件位置不一样,如果是linux或者mac系统,我就直接告诉你吧,一般文件路径是 /etc/hosts ),aop这个词的翻译有点和国内主流叫法不一致,国内主流都把aop译做「面向切面编程」,大家不要拘泥于叫法,知道指的是同一个东西即可。

估计,你看了这个定义也是懵的,如果想深入了解可以去知乎看看大佬们是如何掰扯的 什么是面向切面编程aop? 。我这边还是就直接上例子了吧。

spring boot的aop环境准备

在 pom.xml 中引入相应的依赖模块

<!-- spring boot依赖包 -->
<parent>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-parent</artifactid>
  <version>1.5.1.release</version>
</parent>
<dependencies>
  <!-- aop依赖模块 -->
  <dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-aop</artifactid>
  </dependency>
  <!-- web依赖模块 -->
  <dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-web</artifactid>
  </dependency>
</dependencies>

先实现一个简单的web请求处理

一个简单的处理web请求的controller。

package com.craneyuan.controller;

import com.craneyuan.service.ihelloworldservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.restcontroller;

@restcontroller
public class helloworldcontroller {
  @autowired
  private ihelloworldservice helloworldservice;

  @requestmapping(value = "/hello", method = requestmethod.get)
  public string hello(string name) {
    return helloworldservice.gethellomessage(name);
  }
}

一个简单的helloworld服务实现类,接口的定义我就不展示代码了。

package com.craneyuan.service.impl;

import com.craneyuan.annotation.analysisactuator;
import com.craneyuan.service.ihelloworldservice;
import org.springframework.beans.factory.annotation.value;
import org.springframework.stereotype.service;

import java.util.optional;

@service
public class helloworldserviceimpl implements ihelloworldservice {

  public string gethellomessage(string name) {
    return "hello " + optional.ofnullable(name).orelse("world!");
  }

}

这样一个简单的web服务就弄好了,你可以启动项目用 curl 命令调用试下,例如: curl -xget -i "http://127.0.0.1:8080/hello?name=java" ,如果一切顺利的话,你将会得到类似下面这样的响应:

http/1.1 200
content-type: text/plain;charset=utf-8
content-length: 11
date: thu, 11 jan 2018 09:45:38 gmt

hello java

使用自定义注解来统计方法的执行时间

先定义一个用来统计方法执行时间的注解。

package com.craneyuan.annotation;

import java.lang.annotation.elementtype;
import java.lang.annotation.retention;
import java.lang.annotation.retentionpolicy;
import java.lang.annotation.target;

@target(elementtype.method)
@retention(retentionpolicy.runtime)
public @interface analysisactuator {
  string note() default "";
}

然后定义一个切面,来处理刚刚定义的注解。

package com.craneyuan.aspect;

import com.craneyuan.annotation.analysisactuator;
import org.aspectj.lang.joinpoint;
import org.aspectj.lang.annotation.after;
import org.aspectj.lang.annotation.aspect;
import org.aspectj.lang.annotation.before;
import org.aspectj.lang.annotation.pointcut;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.core.annotation.order;
import org.springframework.stereotype.component;

@aspect
@component
public class analysisactuatoraspect {
  final static logger log = loggerfactory.getlogger(analysisactuatoraspect.class);

  threadlocal<long> begintime = new threadlocal<>();

  @pointcut("@annotation(analysisactuator)")
  public void servicestatistics(analysisactuator analysisactuator) {
  }

  @before("servicestatistics(analysisactuator)")
  public void dobefore(joinpoint joinpoint, analysisactuator analysisactuator) {
    // 记录请求到达时间
    begintime.set(system.currenttimemillis());
    log.info("cy666 note:{}", analysisactuator.note());
  }

  @after("servicestatistics(analysisactuator)")
  public void doafter(analysisactuator analysisactuator) {
    log.info("cy666 statistic time:{}, note:{}", system.currenttimemillis() - begintime.get(), analysisactuator.note());
  }

}

最后,只要在需要统计执行时间的方法上加上 @analysisactuator 注解就行了。

package com.craneyuan.service.impl;

import com.craneyuan.annotation.analysisactuator;
import com.craneyuan.service.ihelloworldservice;
import org.springframework.beans.factory.annotation.value;
import org.springframework.stereotype.service;

import java.util.optional;

@service
public class helloworldserviceimpl implements ihelloworldservice {

  @analysisactuator(note = "获取聊天信息方法")
  public string gethellomessage(string name) {
    return "hello " + optional.ofnullable(name).orelse("world!");
  }

}

启动项目,用 curl 命令随便调用一下,如果顺利的话就可以观察到切面打印的日志了。

...
cy666 statistic time:4, note:获取聊天信息方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: