註解建立Aop切面程式設計

2021-09-24 03:55:34 字數 2885 閱讀 4942

使用註解實現切點程式設計

看一下使用xml配置實現切面程式設計 ,dao

// dao 層

public

class

studentdao

}

aspect切面類

// 切面類

public

class

studentaspect

public

void

after()

}

測試類test

// 測試類

public

class

test

}

xml

<?xml version="1.0" encoding="utf-8"?>

xmlns

=""xmlns:xsi

=""xmlns:aop

=""xmlns:context

=""xsi:schemalocation

="/spring-beans-3.2.xsd

/spring-aop-3.2.xsd

/spring-context-3.2.xsd"

>

"studentdao"

class

="cn.shaojie.dao.studentdao"

>

bean

>

"studentaspect"

class

="cn.shaojie.aspect.studentaspect"

>

bean

>

<

aop:config

>

<

aop:aspectid=

"studentaspect"

ref="studentaspect"

>

<

aop:pointcutid=

"study"

expression

="execution(* cn.shaojie.dao.studentdao.study(..))"

/>

<

aop:before

method

="before"

pointcut-ref

="study"

/>

<

aop:after

method

="after"

pointcut-ref

="study"

/>

aop:aspect

>

aop:config

>

beans

>

以上這個是正常的xml配置切面程式設計的過程,接下來我們來看一下使用註解實現切面程式設計

dao

@component

@enableaspectjautoproxy

@componentscan

("cn.shaojie"

)public

class

studentdao

}

@component註解將這個類載入到ioc容器中,@enableaspectjautoproxy開啟切面的自動**,@componentscan("cn.shaojie")掃瞄包下的所有類使用當前的類作為ioc的容器。

aspect切面類

@component

@aspect

public

class

studentaspect

@after

("studentaspect()"

)public

void

after()

@before

("studentaspect()"

)public

void

before()

}

@aspect將這個類作為切面類,@pointcut將這個方法作為切點方法,@after將方法作為後置通知,@before將方法作為前置通知。

測試類test

public

class

test

}

xml檔案內容

xmlns

=""xmlns:xsi

=""xmlns:aop

=""xmlns:context

=""xsi:schemalocation

="/spring-beans-3.2.xsd

/spring-aop-3.2.xsd

/spring-context-3.2.xsd"

>

<

context:component-scan

base-package

="cn.shaojie"

>

context:component-scan

>

beans

>

到此使用註解建立aop切面程式設計到此結束。

Spring 切面程式設計AOP註解

aop aspect oriented programming 切面程式設計通過預編譯方式和執行期動態 實現程式功能的統一維護的一種技術,是spring框架中乙個重點內容也是函式式程式設計的一種衍生范型。在spring中使用aop的業務只需要關注自己業務本身,將日誌記錄 效能統計 安全控制 事務處理...

面向切面程式設計(AOP)應用,日誌切面,基於註解

名詞解釋 aspect 定義乙個切面 component order 1 public class systemlogaspect 或者配置檔案 aop config 定義乙個切面 aspect component order 1 public class systemlogaspect contr...

AOP 切面程式設計

簡介 如果你很熟悉面向方面程式設計 aop 你就會知道給 增加 切面 可以使 更清晰並且具有可維護性。但是aop通常都依賴於第三方類庫或者硬編碼的.net特性來工作。雖然這些實現方式的好處大於它們的複雜程度,但是我仍然在尋找一種實現aop的更為簡單的方式,來試我的 更為清晰。我將它們單獨移出來,並命...