基于spring@aspect注解的aop實(shí)現(xiàn)過(guò)程代碼實(shí)例
@AspectJ 作為通過(guò) Java 5 注釋注釋的普通的 Java 類,它指的是聲明 aspects 的一種風(fēng)格。通過(guò)在你的基于架構(gòu)的 XML 配置文件中包含以下元素,@AspectJ 支持是可用的。
第一步:編寫(xiě)切面類
package com.dascom.hawk.app.web.tool; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Aspect @Component public class AnnotationAspectJ { //定義切面("execution(* com.dascom.common.aop.*.*(..))) //當(dāng)前配置的意思是所有添加了SuiteMessage的注解的方法作為切點(diǎn) @Pointcut("@annotation(com.dascom.common.annotation.SuiteMessage)") public void logPointCut() { } //前置通知 @Before("logPointCut()") public void before(JoinPoint point) { String calssName = point.getTarget().getClass().getName(); String method = point.getSignature().getName(); System.out.println(calssName + " : " + method); } //后置通知 @After("logPointCut()") public void after(JoinPoint point) { String method = point.getSignature().getName(); System.out.println(method + ": end----"); } //環(huán)繞通知 @Around("logPointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { long beginTime = System.currentTimeMillis(); // 執(zhí)行方法 Object result = point.proceed(); // 執(zhí)行時(shí)長(zhǎng)(毫秒) long time = System.currentTimeMillis() - beginTime; //異步保存日志 System.out.println(time); return result; } }
第二步:在spring的配置文件中添加注解掃描
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置自動(dòng)掃描的包 --> <context:component-scan base-package="com.dascom.hawk.app.web.tool"></context:component-scan> <!-- 自動(dòng)為切面方法中匹配的方法所在的類生成代理對(duì)象。 proxy-target-class="true" 這個(gè)的作用是struts的控制類都基礎(chǔ)的actionSupport,必須添加這個(gè),不然會(huì)報(bào)錯(cuò) --> <aop:aspectj-autoproxy proxy-target-class="true" /> </beans>
第三步:搞定。爽歪歪~~~
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解決spring項(xiàng)目找不到Aspect依賴注解的問(wèn)題
- Spring使用AspectJ的注解式實(shí)現(xiàn)AOP面向切面編程
- 在Spring 中使用@Aspect 控制自定義注解的操作
- Spring-基于Spring使用自定義注解及Aspect實(shí)現(xiàn)數(shù)據(jù)庫(kù)切換操作
- Spring AspectJ AOP框架注解原理解析
- Spring Aop之AspectJ注解配置實(shí)現(xiàn)日志管理的方法
- 詳解SpringBoot AOP 攔截器(Aspect注解方式)
- spring使用aspect注解切面不起作用的排查過(guò)程及解決
相關(guān)文章
MybatisPlus更新時(shí)部分失敗的問(wèn)題解決
這篇文章主要為大家詳細(xì)介紹了MybatisPlus更新時(shí)部分失敗的問(wèn)題分析和解決方法,文中的代碼示例講解的非常詳細(xì),需要的朋友可以參考下2023-06-06mybatis-plus 擴(kuò)展批量新增的實(shí)現(xiàn)
本文主要介紹了mybatis-plus 擴(kuò)展批量新增的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01Java中notify和notifyAll的區(qū)別及何時(shí)使用
本文主要介紹了Java中notify和notifyAll的區(qū)別及何時(shí)使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09SpringBoot項(xiàng)目不占用端口啟動(dòng)的方法
這篇文章主要介紹了SpringBoot項(xiàng)目不占用端口啟動(dòng)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08java向下轉(zhuǎn)型基礎(chǔ)知識(shí)點(diǎn)及實(shí)例
在本篇文章里小編給大家整理的是一篇關(guān)于java向下轉(zhuǎn)型基礎(chǔ)知識(shí)點(diǎn)及實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-05-05詳解在spring boot中消息推送系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn)
這篇文章主要介紹了詳解在spring boot中消息推送系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05Mybatis如何實(shí)現(xiàn)@Select等注解動(dòng)態(tài)組合SQL語(yǔ)句
這篇文章主要介紹了Mybatis如何實(shí)現(xiàn)@Select等注解動(dòng)態(tài)組合SQL語(yǔ)句,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07EL表達(dá)式的隱式對(duì)象_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了EL表達(dá)式的隱式對(duì)象,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07