Spring自定義注解配置簡(jiǎn)單日志示例
java在jdk1.5中引入了注解,spring框架也正好把java注解發(fā)揮得淋漓盡致。
下面會(huì)講解Spring中自定義注解的簡(jiǎn)單流程,其中會(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(面向切面編程)特性
通過(guò)@Aspect注解使該類成為切面類
通過(guò)@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è)方法沒(méi)有拋出任何異常,正常返回。
- @AfterThrowing 異常通知:在方法拋出異常退出時(shí)執(zhí)行的通知。
- @After 后置通知:在某連接點(diǎn)正常完成后執(zhí)行的通知:例如,一個(gè)方法沒(méi)有拋出任何異常,正常返回。
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í),注解要寫(xiě)在類上,不能寫(xiě)在接口上
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)目,訪問(wèn) //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)自定義注解的簡(jiǎn)單流程,需要做更多的自定義操作,則需要在切面類里面進(jìn)行編程了,比如,記錄日志信息的操作,日志信息寫(xiě)入數(shù)據(jù)庫(kù)等。到此這篇關(guān)于Spring自定義注解配置簡(jiǎn)單日志示例的文章就介紹到這了,更多相關(guān)Spring自定義注解日志內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
到此這篇關(guān)于Spring自定義注解配置簡(jiǎn)單日志示例的文章就介紹到這了,更多相關(guān)Spring自定義注解日志內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
線程池中execute與submit的區(qū)別說(shuō)明
這篇文章主要介紹了線程池execute與submit的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Mybatis分頁(yè)插件PageHelper的配置和簡(jiǎn)單使用方法(推薦)
在使用Java Spring開(kāi)發(fā)的時(shí)候,Mybatis算是對(duì)數(shù)據(jù)庫(kù)操作的利器了。這篇文章主要介紹了Mybatis分頁(yè)插件PageHelper的配置和使用方法,需要的朋友可以參考下2017-12-12
spring security集成cas實(shí)現(xiàn)單點(diǎn)登錄過(guò)程
這篇文章主要介紹了spring security集成cas實(shí)現(xiàn)單點(diǎn)登錄過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
詳談@Cacheable不起作用的原因:bean未序列化問(wèn)題
這篇文章主要介紹了@Cacheable不起作用的原因:bean未序列化問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
基于springboot+vue實(shí)現(xiàn)垃圾分類管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了基于springboot+vue實(shí)現(xiàn)垃圾分類管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
SpringMVC記錄我遇到的坑_AOP注解無(wú)效,切面不執(zhí)行的解決
這篇文章主要介紹了SpringMVC記錄我遇到的坑_AOP注解無(wú)效,切面不執(zhí)行的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
spring?boot集成redisson的最佳實(shí)踐示例
這篇文章主要為大家介紹了spring?boot集成redisson的最佳實(shí)踐示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03

