Spring自定義注解配置簡單日志示例
java在jdk1.5中引入了注解,spring框架也正好把java注解發(fā)揮得淋漓盡致。
下面會講解Spring中自定義注解的簡單流程,其中會涉及到spring框架中的AOP(面向切面編程)相關(guān)概念。 不清楚java注解的,可以先了解java自定義注解:Java自定義注解
一、創(chuàng)建自定義注解
requestUrl 為我們自定義的一個參數(shù)
package com.sam.annotation; import java.lang.annotation.*; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface MyLog { String requestUrl(); }
二、解析注解
此處使用了spring的AOP(面向切面編程)特性
通過@Aspect注解使該類成為切面類
通過@Pointcut 指定切入點 ,這里指定的切入點為MyLog注解類型,也就是被@MyLog注解修飾的方法,進入該切入點。
- @Before 前置通知:在某連接點之前執(zhí)行的通知,但這個通知不能阻止連接點之前的執(zhí)行流程(除非它拋出一個異常)。
- @Around 環(huán)繞通知:可以實現(xiàn)方法執(zhí)行前后操作,需要在方法內(nèi)執(zhí)行point.proceed(); 并返回結(jié)果。
- @AfterReturning 后置通知:在某連接點正常完成后執(zhí)行的通知:例如,一個方法沒有拋出任何異常,正常返回。
- @AfterThrowing 異常通知:在方法拋出異常退出時執(zhí)行的通知。
- @After 后置通知:在某連接點正常完成后執(zhí)行的通知:例如,一個方法沒有拋出任何異常,正常返回。
package com.sam.annotation; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.lang.reflect.Method; @Aspect //AOP 切面 @Component public class MyLogAspect { //切入點 @Pointcut(value = "@annotation(com.sam.annotation.MyLog)") private void pointcut() { } /** * 在方法執(zhí)行前后 * * @param point * @param myLog * @return */ @Around(value = "pointcut() && @annotation(myLog)") public Object around(ProceedingJoinPoint point, MyLog myLog) { System.out.println("++++執(zhí)行了around方法++++"); String requestUrl = myLog.requestUrl(); //攔截的類名 Class clazz = point.getTarget().getClass(); //攔截的方法 Method method = ((MethodSignature) point.getSignature()).getMethod(); System.out.println("執(zhí)行了 類:" + clazz + " 方法:" + method + " 自定義請求地址:" + requestUrl); try { return point.proceed(); //執(zhí)行程序 } catch (Throwable throwable) { throwable.printStackTrace(); return throwable.getMessage(); } } /** * 方法執(zhí)行后 * * @param joinPoint * @param myLog * @param result * @return */ @AfterReturning(value = "pointcut() && @annotation(myLog)", returning = "result") public Object afterReturning(JoinPoint joinPoint, MyLog myLog, Object result) { // HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); // HttpSession session = request.getSession(); System.out.println("++++執(zhí)行了afterReturning方法++++"); System.out.println("執(zhí)行結(jié)果:" + result); return result; } /** * 方法執(zhí)行后 并拋出異常 * * @param joinPoint * @param myLog * @param ex */ @AfterThrowing(value = "pointcut() && @annotation(myLog)", throwing = "ex") public void afterThrowing(JoinPoint joinPoint, MyLog myLog, Exception ex) { System.out.println("++++執(zhí)行了afterThrowing方法++++"); System.out.println("請求:" + myLog.requestUrl() + " 出現(xiàn)異常"); } }
三、使用自定義注解
在controller中直接使用注解@MyLog。(在service中也可以,但是在@Around中使用@annotation時,注解要寫在類上,不能寫在接口上
package com.sam.controller; import com.sam.annotation.MyLog; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/index") public class IndexController { @MyLog(requestUrl = "/index請求") @RequestMapping(method = RequestMethod.GET) public String index() { return "index"; } }
啟動項目,訪問 //localhost:8080/index 結(jié)果
++++執(zhí)行了around方法++++ 執(zhí)行了 類:class com.yfs.controller.IndexController 方法:public java.lang.String com.yfs.controller.IndexController.index() 自定義請求地址:/index請求 ++++執(zhí)行了afterReturning方法++++ 執(zhí)行結(jié)果:index
以上講解了Spring實現(xiàn)自定義注解的簡單流程,需要做更多的自定義操作,則需要在切面類里面進行編程了,比如,記錄日志信息的操作,日志信息寫入數(shù)據(jù)庫等。到此這篇關(guān)于Spring自定義注解配置簡單日志示例的文章就介紹到這了,更多相關(guān)Spring自定義注解日志內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
到此這篇關(guān)于Spring自定義注解配置簡單日志示例的文章就介紹到這了,更多相關(guān)Spring自定義注解日志內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis分頁插件PageHelper的配置和簡單使用方法(推薦)
在使用Java Spring開發(fā)的時候,Mybatis算是對數(shù)據(jù)庫操作的利器了。這篇文章主要介紹了Mybatis分頁插件PageHelper的配置和使用方法,需要的朋友可以參考下2017-12-12spring security集成cas實現(xiàn)單點登錄過程
這篇文章主要介紹了spring security集成cas實現(xiàn)單點登錄過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02詳談@Cacheable不起作用的原因:bean未序列化問題
這篇文章主要介紹了@Cacheable不起作用的原因:bean未序列化問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01基于springboot+vue實現(xiàn)垃圾分類管理系統(tǒng)
這篇文章主要為大家詳細介紹了基于springboot+vue實現(xiàn)垃圾分類管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07SpringMVC記錄我遇到的坑_AOP注解無效,切面不執(zhí)行的解決
這篇文章主要介紹了SpringMVC記錄我遇到的坑_AOP注解無效,切面不執(zhí)行的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07