Spring以註解方式使用aop

2021-08-14 03:42:07 字數 2164 閱讀 3082

7.spring以註解方式使用

<?

xmlversion="1.0"

encoding="utf-8"

?>

<

beans

xmlns:xsi=""

xmlns=""

xmlns:context=""

xmlns:aop=""

xsi:schemalocation=" /spring-beans-4.2.xsd /spring-context-4.2.xsd /spring-aop-4.2.xsd "

>

<

bean

name="userservice"

class="cn.

example

.service.userserviceimpl"

>

bean

>

<

bean

name="myadvice"

class="cn.

example

.e_annotationaop.myadvice"

>

bean

>

<

aop:aspectj-autoproxy

>

aop:aspectj-autoproxy

>

beans

>

spring註解使用

aop通知類(目標方法執行前後要執行的**所在類):

//通知類

@aspect

//表示該類是乙個通知類

public

classmyadvice

//前置通知

//指定該方法是前置通知,並制定切入點

@before("myadvice.pc()")

public

voidbefore(){

system.out

.println("這是前置通知!!");

//後置通知 @

afterreturning("execution(* cn.

example

.service.*serviceimpl.*(..))")

public

voidafterreturning(){

system.out

.println("這是後置通知(如果出現異常不會呼叫)!!");

//環繞通知

@around("execution(* cn.

example

.service.*serviceimpl.*(..))")

publicobject around(proceedingjoinpoint pjp)throwsthrowable {

system.out

.println("這是環繞通知之前的部分!!");

object proceed = pjp.proceed();//呼叫目標方法

system.out

.println("這是環繞通知之後的部分!!");

returnproceed;

//異常通知

@afterthrowing("execution(* cn.

example

.service.*serviceimpl.*(..))")

public

voidafterexception(){

system.out

.println("出事啦!出現異常了!!");

//後置通知

@after("execution(* cn.

example

.service.*serviceimpl.*(..))")

public

voidafter(){

system.out

.println("這是後置通知(出現異常也會呼叫)!!");

目標類和測試類省略

spring之基於aspectj註解aop使用

在配置檔案中開啟aop自動 1 在增強類上面使用 aspect註解 2 在增強方法上面配置不同型別通知 增強類 aspect public class myuser 後置通知 afterreturning value execution cn.aop.user.update public void ...

使用註解方式配置Spring

在配置檔案中新增如下 spring會自動掃瞄改包下的所有註解 component user 當分不清時用它 controller user web層 service user service層 repository dao層 不寫名稱預設是類名小寫 scope scopename prototype...

Spring使用註解方式注入多例的方式

目前spring netty的開發方式這麼火熱,想把netty註冊成spring元件就一定得用多例的方式,我不由得想吐槽明明這麼常見的需求網上相關部落格都少的很,這裡給出spring使用註解注入多例的方式 在需要多例呼叫的類上加 scope prototype 在進行注入時,不能直接使用 autow...