国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

基于spring@aspect注解的aop實現過程代碼實例

瀏覽:178日期:2023-09-18 08:36:11

@AspectJ 作為通過 Java 5 注釋注釋的普通的 Java 類,它指的是聲明 aspects 的一種風格。通過在你的基于架構的 XML 配置文件中包含以下元素,@AspectJ 支持是可用的。

第一步:編寫切面類

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@Componentpublic class AnnotationAspectJ { //定義切面('execution(* com.dascom.common.aop.*.*(..))) //當前配置的意思是所有添加了SuiteMessage的注解的方法作為切點 @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----'); } //環繞通知 @Around('logPointCut()') public Object around(ProceedingJoinPoint point) throws Throwable { long beginTime = System.currentTimeMillis(); // 執行方法 Object result = point.proceed(); // 執行時長(毫秒) 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'> <!-- 配置自動掃描的包 --> <context:component-scan base-package='com.dascom.hawk.app.web.tool'></context:component-scan> <!-- 自動為切面方法中匹配的方法所在的類生成代理對象。 proxy-target- 這個的作用是struts的控制類都基礎的actionSupport,必須添加這個,不然會報錯 --> <aop:aspectj-autoproxy proxy-target- /> </beans>

第三步:搞定。爽歪歪~~~

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 四川省| 襄垣县| 琼中| 福州市| 五峰| 三台县| 巴彦县| 康平县| 东阿县| 余姚市| 桓仁| 河南省| 兴安盟| 庆云县| 南汇区| 武乡县| 同心县| 昂仁县| 平泉县| 瑞昌市| 电白县| 秀山| 德钦县| 乌审旗| 海伦市| 盘锦市| 甘谷县| 泽普县| 平远县| 湘乡市| 高密市| 南皮县| 高淳县| 贵定县| 九江市| 来凤县| 大埔区| 松桃| 武川县| 商河县| 剑阁县|