AOP操作AspectJ註解實現增強功能

2021-10-24 02:30:49 字數 1966 閱讀 7781

先在xml檔案裡配置上aop和context,用context來開啟註解掃瞄,aop生成增強物件

!-- 開啟註解掃瞄--

>

package

="cn.zsp.spring5.aopnno"

>

<

/context:component-scan>

<

!--開啟aspect生成增強物件--

>

<

/aop:aspectj-autoproxy>

<

/beans>這是乙個被增強類:

package cn.zsp.spring5.aopnno;

import org.springframework.stereotype.component;

@component

public

class

user

}

這是增強類

package cn.zsp.spring5.aopnno;

import org.aspectj.lang.proceedingjoinpoint;

import org.aspectj.lang.annotation.*;

import org.springframework.stereotype.component;

@component

@aspect

//生成**物件

public

class

userproxy

@after

(value =

"execution(* cn.zsp.spring5.aopnno.user.add(..))"

)public

void

after()

@afterreturning

(value =

"execution(* cn.zsp.spring5.aopnno.user.add(..))"

)public

void

afterreturn()

@afterthrowing

(value =

"execution(* cn.zsp.spring5.aopnno.user.add(..))"

)public

void

afterthrowing()

@around

(value =

"execution(* cn.zsp.spring5.aopnno.user.add(..))"

)public

void

around

(proceedingjoinpoint point)

throws throwable

}

這是我的測試類

package cn.zsp.spring5.testdemo;

import cn.zsp.spring5.aopnno.user;

import org.junit.test;

public

class

testdemo

}

AspectJ基於註解的AOP 實現

配置檔案 demo aspect public class myaspect signature signature joinpoint.getsignature system.out.println 方法的定義簽名signature signature string name joinpoint....

AOP操作(AspectJ配置檔案)

1.建立兩個類,乙個增強類,乙個被增強類,建立方法 被增強類 public class book 增強類public class bookproxy 2.在spring配置檔案中建立兩個類物件 bean id book class hrf.spring5.aopxml.book bean bean ...

Spring整合AspectJ 實現AOP

org.springframework.boot spring boot starter aop component 交給spring容器管理 aspect 告訴spring,此類為乙個切面類 public class cacheaspect around docache public object...