生活随笔
收集整理的這篇文章主要介紹了
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的示例:
@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
.getSignature().getName() + " 參數為:" + Arrays.asList(joinPoint
.getArgs()));}public void after(JoinPoint joinPoint
) {System.err
.println("后置通知,方法名為:" + joinPoint
.getSignature().getName() + " 參數為:" + Arrays.asList(joinPoint
.getArgs()));}public void afterReturning(JoinPoint joinPoint
, Object result
) {System.err
.println("返回通知,方法名為:" + joinPoint
.getSignature().getName() + " 參數為:" + Arrays.asList(joinPoint
.getArgs()) + " 返回結果為:" + result
);}public void afterThrowing(JoinPoint joinPoint
, Exception exception
) {System.err
.println("異常通知,方法名為:" + joinPoint
.getSignature().getName() + " 參數為:" + Arrays.asList(joinPoint
.getArgs()) + " 異常為:" + exception
);}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及常规方式的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。