日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot注解方式实现aop及常规方式

發布時間:2024/1/8 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot注解方式实现aop及常规方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文介紹springboot實現aop的兩種方式
首先需要引入對應依賴:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.9.1</version></dependency>

在啟動類上面加上注解

@EnableAspectJAutoProxy

其實不加這個注解也可以,aop照樣會生效,我們查看spring-boot-autoconfigure的依賴,查看spring.factories文件會發現以下配置

然后查看AopAutoConfiguration類會發現,當yml沒有對應配置時,默認為true
下面展示常規方式實現aop的示例:

/*** @Description* @Author maruko* @Date 2022/11/22 17:04* @Version 1.0*/ @Aspect @Component public class AspectTest {@Pointcut("execution(* com.zjf.demo.controller.UserController.*(..))||execution(* com.zjf.demo.controller.KafkaController.*(..))")public void pointExpression() {}@Before("pointExpression()")public void before(JoinPoint joinPoint) { // System.err.println(joinPoint.toString()); // Object[] args = joinPoint.getArgs(); // for (Object arg : args) { // System.err.println(arg); // }// System.err.println(joinPoint.getSignature().toLongString()); // System.err.println(joinPoint.getSignature().toShortString()); // System.err.println(joinPoint.getSignature().getName()); // System.err.println(joinPoint.getSignature().toString()); // System.err.println(joinPoint.getKind()); // System.err.println(joinPoint.getTarget().toString()); // System.err.println(joinPoint.getThis().toString()); // System.err.println(joinPoint.getStaticPart()); // System.err.println(joinPoint.getSourceLocation()); // System.err.println(joinPoint.toLongString()); // System.err.println(joinPoint.toShortString());System.err.println("前置通知,方法名為:" + joinPoint.getSignature().getName() + " 參數為:" + Arrays.asList(joinPoint.getArgs()));}// @After("execution(* com.zjf.demo.controller.UserController.*(..))")public void after(JoinPoint joinPoint) {System.err.println("后置通知,方法名為:" + joinPoint.getSignature().getName() + " 參數為:" + Arrays.asList(joinPoint.getArgs()));}// @AfterReturning(pointcut = "execution(* com.zjf.demo.controller.UserController.*(..))", returning = "result")public void afterReturning(JoinPoint joinPoint, Object result) {System.err.println("返回通知,方法名為:" + joinPoint.getSignature().getName() + " 參數為:" + Arrays.asList(joinPoint.getArgs()) + " 返回結果為:" + result);}// @AfterThrowing(value = "execution(* com.zjf.demo.controller.UserController.*(..))", throwing = "exception")public void afterThrowing(JoinPoint joinPoint, Exception exception) {System.err.println("異常通知,方法名為:" + joinPoint.getSignature().getName() + " 參數為:" + Arrays.asList(joinPoint.getArgs()) + " 異常為:" + exception);}// @Around("execution(* com.zjf.demo.controller.UserController.*(..))")public Object aroundMethod(ProceedingJoinPoint joinPoint) {String methodName = joinPoint.getSignature().getName();List<Object> args = Arrays.asList(joinPoint.getArgs());Object result = null;try {//前置通知System.err.println("前置通知,方法名為:" + joinPoint.getSignature().getName() + " 參數為:" + Arrays.asList(joinPoint.getArgs()));result = joinPoint.proceed();//返回通知System.err.println("返回通知,方法名為:" + joinPoint.getSignature().getName() + " 參數為:" + Arrays.asList(joinPoint.getArgs()) + " 返回結果為:" + result);} catch (Throwable e) {// 異常通知System.err.println("異常通知,方法名為:" + joinPoint.getSignature().getName() + " 參數為:" + Arrays.asList(joinPoint.getArgs()) + " 異常為:" + e);throw new RuntimeException(e);}//后置通知System.err.println("后置通知,方法名為:" + joinPoint.getSignature().getName() + " 參數為:" + Arrays.asList(joinPoint.getArgs()));return result;}}

注解方式實現如下:
首先定義注解:

@Target(ElementType.METHOD) //定義注解的使用范圍為方法 @Retention(RetentionPolicy.RUNTIME ) public @interface AopAnnotation { }

示例如下:
/**

  • @Description

  • @Author maruko

  • @Date 2022/11/23 9:33

  • @Version 1.0
    */
    @Aspect
    @Component
    @Order(1)
    public class AspectAnnotationTest {

    @Pointcut(“@annotation(com.zjf.demo.annotation.AopAnnotation)”)
    public void pointExpression() {

    }

    @Before(“pointExpression()”)
    public void before(JoinPoint joinPoint) {
    // System.err.println(joinPoint.toString());
    // Object[] args = joinPoint.getArgs();
    // for (Object arg : args) {
    // System.err.println(arg);
    // }

// System.err.println(joinPoint.getSignature().toLongString());
// System.err.println(joinPoint.getSignature().toShortString());
// System.err.println(joinPoint.getSignature().getName());
// System.err.println(joinPoint.getSignature().toString());
// System.err.println(joinPoint.getKind());
// System.err.println(joinPoint.getTarget().toString());
// System.err.println(joinPoint.getThis().toString());
// System.err.println(joinPoint.getStaticPart());
// System.err.println(joinPoint.getSourceLocation());
// System.err.println(joinPoint.toLongString());
// System.err.println(joinPoint.toShortString());
System.err.println(“注解方式實現,前置通知,方法名為:” + joinPoint.getSignature().getName() + " 參數為:" + Arrays.asList(joinPoint.getArgs()));
}
}

總結

以上是生活随笔為你收集整理的springboot注解方式实现aop及常规方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。