使用註解實現切點程式設計
看一下使用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的更為簡單的方式,來試我的 更為清晰。我將它們單獨移出來,並命...