spring入門2
1. aop : 分爲 JDK動態代理 無耦合 cglib動態代理; 是一個開源項目 ,code生成類庫. 不需要前提條件 有耦合
2. spring 自動選擇 使用哪個動態機制, 有接口用動態代理, 無接口用cglib;
3. spring整合aspectj 框架實現的aop *
4.五種通知, 前置 ,後置 ,環繞 ,異常拋出通知, 引介通知
5.spring 配置文件拆分, <import resource="導入其他部分的配置文件">
6. aspectj 切點 傳統開發, 可以使用<aop:xxx> 來簡化操作.
1.導入名稱空間. 兩個bean, 目標和通知
2.<aop:config>自動代理 <aop: pointcut expression="excution()" id=""> 聲明切點
<aop: advisor adivce-ref="" pointcut-ref=""> 切面, 只能包含該一個切點和一個增強,
<aop:aspect> 定義aspectj 框架的切面. 他可以包含多個切點和多個通知.
7.1.execution() 用於描述方法 【掌握】
語法:execution(修飾符 返回值 包.類.方法名(參數) throws異常)
修飾符,一般省略
public 公共方法
* 任意
返回值,不能省略
void 返回沒有值
String 返回值字符串
* 任意
包,[省略]
com.itheima.crm 固定包
com.itheima.crm.*.service crm包下面子包任意 (例如:com.itheima.crm.staff.service
com.itheima.crm.. crm包下面的所有子包(含自己)
com.itheima.crm.*.service.. crm包下面任意子包,固定目錄service,service目錄任意包
類,[省略]
UserServiceImpl 指定類
*Impl 以Impl結尾
User* 以User開頭
* 任意
方法名,不能省略
addUser 固定方法
add* 以add開頭
*Do 以Do結尾
* 任意
(參數)
() 無參
(int) 一個整型
(int ,int) 兩個
(..) 參數任意
throws ,可省略,一般不寫。
綜合1
execution(* com.itheima.crm.*.service..*.*(..))
綜合2
<aop:pointcut expression="execution(* com.itheima.*WithCommit.*(..)) ||
execution(* com.itheima.*Service.*(..))" id="myPointCut"/>
7. aspectj 框架實現 aop aspect: 切面=切點+通知(多個切點和多個通知的組合) aspectj: 第三方框架,可以使用部分語法
定義通知類型6種, 前置,後置,環繞,拋出,引介,最終通知(不過是否有異常,都會執行.)
8. 在 aspectj 中它的增強可以不實現任何接口,只需要定義出增強功能
xml文件中: <aop:config> <aop:aspect ref="通知"> <aop:before method="">
<aop:after-throwing method="afterThrowing" pointcut-ref="delPointCut" throwing="ex"/>
環繞通知方法 public object around(proceedingjoinpoint pjp){ pjp.proceed(); }
異常拋出通知 public void afterThrowing(JoinPoint jp,Throwable ex) {
System.out.println("發現了異常。。。。"+ex)
9. 關於各個通知的參數
前置通知:參數 joinpoint jp, jp.getSignature().getDeclaringTypeName() 獲取目標類名
後置通知: 參數 joinpoint jp, object val 要在xml中配置 returning="val" 注意參數名要一致
環繞通知 : 參數 proceedingjoinpoint pjp, Object obj = pjp.proceed() 執行目標行爲
異常拋出通知: 參數 JoinPoint jp ,Throwable ex 在xml中 throwing="ex"
最終通知: after(JoinPoint jp ) jp.getSignature().getName() 方法名;
10.環繞通知:日誌操作,權限校驗, 性能監控,事物管理 最終通知: 資源釋放.
11. 關於代理方式的選擇: 在xml中 <aop:cofig proxy-target-class的值默認爲false,代表有接口使用proxy >
12 : 註解開發: <context:component-scan base-pakeage> 掃描指定包下的註解;
開啓aspectj註解自動代理功能: <aop:aspectj-autoproxy/>
通知中: 類上面: @component @aspect 聲明當前bean是一個切面 方法上面: @before("execution(表達式)")
後置通知
@AfterReturning(value = "execution(* *.update(..))", returning = "value")
環繞通知: @around("execution(* *.update(..))")
異常通知: @afterthrowing(value = "execution(* *.update(..))", throwing = "ex")
最終通知: @after("execution(* *.update(..))")
13. 創建無參數方法: 在參數上面: @pointcut("execution(* *.update(..)")
最終通知: @after("方法名() || 方法名2()")
14. 選擇代理方式: 在xml中 <aop: aspe...... proxy-target-class=false>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.