日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring20:Aspectj实现前置通知@Before

發(fā)布時(shí)間:2025/6/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring20:Aspectj实现前置通知@Before 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

定義一個(gè)接口?

package com.atChina.Test;public interface SomeService {public void doSome(); }

定義接口的實(shí)現(xiàn)類?

package com.atChina.Test;public class SomeServiceImpl implements SomeService {@Overridepublic void doSome() {System.out.println("執(zhí)行了doSome業(yè)務(wù)方法...");} }

定義一個(gè)切面類?

package com.atChina.Test;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before;/*** @Aspect:來自aspectj框架,表示當(dāng)前類是切面類* 切面類是用來給業(yè)務(wù)方法增強(qiáng)功能的類*/ @Aspect public class MyAspect {/*** @Before:前置通知* 屬性:value,表示切入點(diǎn)表達(dá)式(切面功能加入的位置)* 位置:在方法的上面* * 特點(diǎn):* 1.在目標(biāo)方法之前先執(zhí)行* 2.不能改變目標(biāo)方法的執(zhí)行結(jié)果* 3.不會(huì)影響目標(biāo)方法的執(zhí)行*/@Before(value="execution(* com.atChina.Test.SomeServiceImpl.doSome())")public void beforeFunc(){System.out.println("前置通知:在目標(biāo)方法之前,實(shí)現(xiàn)日志功能");} }

定義配置文件

<?xml version="1.0" encoding="UTF-8"?> <!-- 引用Spring的多個(gè)Schema空間的格式定義文件 --> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd "><!-- 聲明目標(biāo)類對(duì)象 --><bean id="someService" class="com.atChina.Test.SomeServiceImpl" /><!-- 聲明切面類對(duì)象 --><bean id="myAspect" class="com.atChina.Test.MyAspect" /><!-- 聲明自動(dòng)代理生成器,創(chuàng)建代理對(duì)象 --><aop:aspectj-autoproxy /> <!-- 尋找aspectj框架能夠識(shí)別的標(biāo)簽 --> </beans>

定義測試方法

@Testpublic void test1(){String configLocation = "com/atChina/Test/applicationContext.xml"; // 類路徑的根目錄ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);// 目標(biāo)對(duì)象有接口,aspectj默認(rèn)使用的是jdk動(dòng)態(tài)代理SomeService proxy = (SomeService) ctx.getBean("someService");System.out.println(proxy.getClass().getName());proxy.doSome();}

執(zhí)行結(jié)果:?

?

?

?

?前置通知方法可以有參數(shù)

package com.atChina.Test;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before;/*** @Aspect:來自aspectj框架,表示當(dāng)前類是切面類* 切面類是用來給業(yè)務(wù)方法增強(qiáng)功能的類*/ @Aspect public class MyAspect {/*** @Before:前置通知* 屬性:value,表示切入點(diǎn)表達(dá)式(切面功能加入的位置)* 位置:在方法的上面* * 特點(diǎn):* 1.在目標(biāo)方法之前先執(zhí)行* 2.不能改變目標(biāo)方法的執(zhí)行結(jié)果* 3.不會(huì)影響目標(biāo)方法的執(zhí)行* * 前置通知方法可以有參數(shù)* JoinPoint連接點(diǎn),是一個(gè)方法,表示切入點(diǎn)表達(dá)式中每一個(gè)方法*/@Before(value="execution(* *..SomeServiceImpl.do*(..))")public void beforeFunc(JoinPoint jp){System.out.println(jp.getSignature()); // 連接點(diǎn)的方法簽名Object[] args = jp.getArgs(); // 獲取參數(shù)數(shù)組System.out.println(args.length);System.out.println(args[0]); // 獲取具體參數(shù)值System.out.println("前置通知:在目標(biāo)方法之前,實(shí)現(xiàn)日志功能");} }

?

總結(jié)

以上是生活随笔為你收集整理的spring20:Aspectj实现前置通知@Before的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。