spring20:Aspectj实现前置通知@Before
生活随笔
收集整理的這篇文章主要介紹了
spring20:Aspectj实现前置通知@Before
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
定義一個接口?
package com.atChina.Test;public interface SomeService {public void doSome(); }定義接口的實現類?
package com.atChina.Test;public class SomeServiceImpl implements SomeService {@Overridepublic void doSome() {System.out.println("執行了doSome業務方法...");} }定義一個切面類?
package com.atChina.Test;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before;/*** @Aspect:來自aspectj框架,表示當前類是切面類* 切面類是用來給業務方法增強功能的類*/ @Aspect public class MyAspect {/*** @Before:前置通知* 屬性:value,表示切入點表達式(切面功能加入的位置)* 位置:在方法的上面* * 特點:* 1.在目標方法之前先執行* 2.不能改變目標方法的執行結果* 3.不會影響目標方法的執行*/@Before(value="execution(* com.atChina.Test.SomeServiceImpl.doSome())")public void beforeFunc(){System.out.println("前置通知:在目標方法之前,實現日志功能");} }定義配置文件
<?xml version="1.0" encoding="UTF-8"?> <!-- 引用Spring的多個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 "><!-- 聲明目標類對象 --><bean id="someService" class="com.atChina.Test.SomeServiceImpl" /><!-- 聲明切面類對象 --><bean id="myAspect" class="com.atChina.Test.MyAspect" /><!-- 聲明自動代理生成器,創建代理對象 --><aop:aspectj-autoproxy /> <!-- 尋找aspectj框架能夠識別的標簽 --> </beans>定義測試方法
@Testpublic void test1(){String configLocation = "com/atChina/Test/applicationContext.xml"; // 類路徑的根目錄ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);// 目標對象有接口,aspectj默認使用的是jdk動態代理SomeService proxy = (SomeService) ctx.getBean("someService");System.out.println(proxy.getClass().getName());proxy.doSome();}執行結果:?
?
?
?
?前置通知方法可以有參數
package com.atChina.Test;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before;/*** @Aspect:來自aspectj框架,表示當前類是切面類* 切面類是用來給業務方法增強功能的類*/ @Aspect public class MyAspect {/*** @Before:前置通知* 屬性:value,表示切入點表達式(切面功能加入的位置)* 位置:在方法的上面* * 特點:* 1.在目標方法之前先執行* 2.不能改變目標方法的執行結果* 3.不會影響目標方法的執行* * 前置通知方法可以有參數* JoinPoint連接點,是一個方法,表示切入點表達式中每一個方法*/@Before(value="execution(* *..SomeServiceImpl.do*(..))")public void beforeFunc(JoinPoint jp){System.out.println(jp.getSignature()); // 連接點的方法簽名Object[] args = jp.getArgs(); // 獲取參數數組System.out.println(args.length);System.out.println(args[0]); // 獲取具體參數值System.out.println("前置通知:在目標方法之前,實現日志功能");} }?
總結
以上是生活随笔為你收集整理的spring20:Aspectj实现前置通知@Before的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring19:AspectJ的初步介
- 下一篇: mybaits一:初步认识mybatis