Spring自定義注解配置簡單日志示例
java在jdk1.5中引入了注解,spring框架也正好把java注解發(fā)揮得淋漓盡致。
下面會(huì)講解Spring中自定義注解的簡單流程,其中會(huì)涉及到spring框架中的AOP(面向切面編程)相關(guān)概念。 不清楚java注解的,可以先了解java自定義注解:Java自定義注解
一、創(chuàng)建自定義注解
requestUrl 為我們自定義的一個(gè)參數(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 指定切入點(diǎn) ,這里指定的切入點(diǎn)為MyLog注解類型,也就是被@MyLog注解修飾的方法,進(jìn)入該切入點(diǎn)。
- @Before 前置通知:在某連接點(diǎn)之前執(zhí)行的通知,但這個(gè)通知不能阻止連接點(diǎn)之前的執(zhí)行流程(除非它拋出一個(gè)異常)。
- @Around 環(huán)繞通知:可以實(shí)現(xiàn)方法執(zhí)行前后操作,需要在方法內(nèi)執(zhí)行point.proceed(); 并返回結(jié)果。
- @AfterReturning 后置通知:在某連接點(diǎn)正常完成后執(zhí)行的通知:例如,一個(gè)方法沒有拋出任何異常,正常返回。
- @AfterThrowing 異常通知:在方法拋出異常退出時(shí)執(zhí)行的通知。
- @After 后置通知:在某連接點(diǎn)正常完成后執(zhí)行的通知:例如,一個(gè)方法沒有拋出任何異常,正常返回。
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 {
//切入點(diǎn)
@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 + " 自定義請(qǐng)求地址:" + 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("請(qǐng)求:" + myLog.requestUrl() + " 出現(xiàn)異常");
}
}三、使用自定義注解
在controller中直接使用注解@MyLog。(在service中也可以,但是在@Around中使用@annotation時(shí),注解要寫在類上,不能寫在接口上
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請(qǐng)求")
@RequestMapping(method = RequestMethod.GET)
public String index() {
return "index";
}
}啟動(dòng)項(xiàng)目,訪問 //localhost:8080/index 結(jié)果
++++執(zhí)行了around方法++++ 執(zhí)行了 類:class com.yfs.controller.IndexController 方法:public java.lang.String com.yfs.controller.IndexController.index() 自定義請(qǐng)求地址:/index請(qǐng)求 ++++執(zhí)行了afterReturning方法++++ 執(zhí)行結(jié)果:index
以上講解了Spring實(shí)現(xiàn)自定義注解的簡單流程,需要做更多的自定義操作,則需要在切面類里面進(jìn)行編程了,比如,記錄日志信息的操作,日志信息寫入數(shù)據(jù)庫等。到此這篇關(guān)于Spring自定義注解配置簡單日志示例的文章就介紹到這了,更多相關(guān)Spring自定義注解日志內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
到此這篇關(guān)于Spring自定義注解配置簡單日志示例的文章就介紹到這了,更多相關(guān)Spring自定義注解日志內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringSecurity之SecurityContextHolder使用解讀
這篇文章主要介紹了SpringSecurity之SecurityContextHolder使用解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
SpringBoot中最常用的5個(gè)內(nèi)置對(duì)象使用方法
這篇文章主要給大家介紹了關(guān)于SpringBoot中最常用的5個(gè)內(nèi)置對(duì)象使用的相關(guān)資料,在學(xué)習(xí)springboot的過程中,發(fā)現(xiàn)了springboot非常多的優(yōu)點(diǎn)和特性,需要的朋友可以參考下2023-08-08
Java實(shí)現(xiàn)計(jì)算一個(gè)月有多少天和多少周
這篇文章主要介紹了Java實(shí)現(xiàn)計(jì)算一個(gè)月有多少天和多少周,本文直接給出實(shí)例代碼,需要的朋友可以參考下2015-06-06
SpringSecurity實(shí)現(xiàn)前后端分離的示例詳解
Spring Security默認(rèn)提供賬號(hào)密碼認(rèn)證方式,具體實(shí)現(xiàn)是在UsernamePasswordAuthenticationFilter 中,這篇文章主要介紹了SpringSecurity實(shí)現(xiàn)前后端分離的示例詳解,需要的朋友可以參考下2023-03-03
springboot集成camunda的實(shí)現(xiàn)示例
本文主要介紹了springboot集成camunda的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10

