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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring AOP编程

發布時間:2025/5/22 编程问答 62 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring AOP编程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前提
導入aop需要的jar:
aspectjweaver.jar和aspectjrt.jar和cglib-nodep-2.1_3.jar

加入aop需要的命名空間:

xmlns:aop="http://www.springframework.org/schema/aop"http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd

?

spring提供了兩種切面使用方式:

基于注解方式和基于xml方式。

?

基于注解方式進行AOP開發

前提:

前面加上aop聲明
打開@Aspect的支持:<aop:aspectj-autoproxy/>

<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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:component-scan base-package="cn.itcast"/><aop:aspectj-autoproxy/>

?

一些有關注解:

@Aspect ?聲明切面,作用在類上

?

@Pointcut 聲明切入點

例子:

@Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean.*(..))")private void anyMethod(){}//聲明一個切入點,采用方法定義的形式

注意點:

  1、切入點的聲明和方法類似,但是切入點的名稱為方法名+(),比如上面的切入點名為anyMethod()

  2、切入點的掃描內容格式基本固定:execution(切入點表達式)

  切入點表達式:

    分解: 返回值類型 ?包名.類名.方法名(方法參數)

    可用的符號:

      * 通配符,表示任意

      .. 兩種意義,一個表示可變參,在參數列表中使用,一個表示:該包下的子包和該包,在package中使用

      !非

  切入點表達式例子:

返回值是String類型的:

execute(java.lang.String cn.itcast.service..*.*(..))

要求輸入參數的第一個參數是String類型:

execute(* cn.itcast.service..*.*(java.lang.String,..))

要求返回值不是void:

execute(!void cn.itcast.service..*.*(..))

?

通知作用在方法上

@Before?前置通知?

@Before("anyMethod()")

如果希望得到攔截到的切入點中的參數,可以這樣:

@Before(value="anyMethod() && args(name)") public void doBefore(String name){//前置通知,里面的參數為切入點名System.out.println("前置通知"+name); }


@AfterReturning  后置通知

@AfterReturning("anyMethod()")

如果希望在后置通知中得到方法的返回值:

@AfterReturning(pointcut="anyMethod()",returning="result") public void doAfterReturning(String result){System.out.println("后置通知"+result); }


@After 最終通知

@After("anyMethod()") public void doAfter(){ S  ystem.out.println("最終通知"); }

?

@AfterThrowing 例外通知

@AfterThrowing("anyMethod()")

如果希望例外通知中得到異常信息:

@AfterThrowing(pointcut="anyMethod()",throwing="e") public void doAfterThrowing(Exception e){ System.out.println("例外通知"+e); }

?

@Around?環繞通知

@Around("anyMethod()") public Object doBasicProfiling(ProceedingJoinPoint pjp)throws Throwable{Object result=null;if(true){//判斷用戶是否有權限System.out.println("進入方法");result=pjp.proceed();//這個方法類似于doFilterSystem.out.println("退出方法");}return result; }

注意:

  1、環繞通知的作用方法的格式是不變的,只有方法名和參數名可變

  2、環繞通知跟struts2中的攔截器很相似。ProceedingJoinPoint對象的proceed()方法和invocation.invoke()方法類似

?

基于xml形式進行AOP開發

我們原來的切面變成了普通的java類:

package cn.itcast.service;import org.aspectj.lang.ProceedingJoinPoint; import org.springframework.stereotype.Component;@Component("aspectBean") public class MyInterceptor {public void doBefore(){//前置通知System.out.println("前置通知");}public void doAfterReturning(String result){System.out.println("后置通知"+result);}public void doAfter(){System.out.println("最終通知");}public void doAfterThrowing(Exception e){System.out.println("例外通知"+e);}public Object doBasicProfiling(ProceedingJoinPoint pjp)throws Throwable{Object result=null;if(true){//判斷用戶是否有權限System.out.println("進入方法");result=pjp.proceed();//這個方法類似于doFilterSystem.out.println("退出方法");}return result;}} View Code

beans.xml的對應配置:

      <aop:config><aop:aspect id="asp" ref="aspectBean"><aop:pointcut id="mycut" expression="execution(* cn.itcast.service.impl..*.*(..))"/><aop:before method="doBefore" pointcut-ref="mycut"/> <aop:after-returning method="doAfterReturning" pointcut-ref="mycut" returning="result"/><aop:after method="doAfter" pointcut-ref="mycut"/><aop:after-throwing method="doAfterThrowing" pointcut-ref="mycut" throwing="e"/><aop:around method="doBasicProfiling" pointcut-ref="mycut"/></aop:aspect></aop:config>

前置通知的參數獲取使用xml配置的我沒有弄出來,不轉牛角尖了,知道的可以告訴下,謝謝!

總結

以上是生活随笔為你收集整理的spring AOP编程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。