當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring六:Spring AOP API 上
生活随笔
收集整理的這篇文章主要介紹了
Spring六:Spring AOP API 上
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
PointCut
PointCut是依靠NameMatchMethodPointCut實現(xiàn),根據(jù)方法名字進行匹配;成員變量有:mappedNames,匹配方法名的集合。
?
?
?
?
?
Before Advice
- 是一個簡單的通知類型
- 只有在進入方法之前被調(diào)用,不需要MethodInvocation對象
- 前置通知可以在連接點執(zhí)行前插入自定義行為,但不能改變返回值。
- 實現(xiàn)方式是:實現(xiàn)MethodBeforeAdvice抽象類。
?Throws Advice
- 如果連接點拋出異常,throw advice 在連接點返回后被調(diào)用。
- 如果throws-advice本身方法拋出異常,那么他將覆蓋原有的異常。
- 接口org.springframework.aop.ThrowsAdvice不包含任何方法,僅僅是一個申明,實現(xiàn)類需要實現(xiàn)下面的方法。
- 實現(xiàn)方式是實現(xiàn)ThrowsAdvice接口類。
?
?
如下是Throws Advice的聲明:
After Returning Advice
- 是一個后置通知,他必須實現(xiàn)org.springframework.aop.AfterReturningAdvice接口。
- 可以訪問返回值(但是不能修改返回值),被調(diào)用的方法,方法參數(shù),和目標。
- 如果拋出異常,將拋出攔截器鏈,替代返回值。
?
Advice APi inspring:Advisor是僅包含一個切入點表達式關聯(lián)的單個通知方面,除了Introduction是,Advice可以用于任何通知。
?
ProxyFactoryBean
創(chuàng)建Spring Aop代理的基本方法是使用org.springframework.aop.framework.proxyFactoryBean;可以完全控制切入點和通知以及他們的順序。
優(yōu)點:
XMl配置如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> 11 12 13 <bean id="xinAfterReturningAdvice" class="Aop_Class.XinAfterReturningAdvice"></bean> 14 15 <bean id="xinBeforeAdvice" class="Aop_Class.XinBeforeAdvice"></bean> 16 17 <bean id="xinMethodInterceptor" class="Aop_Class.XinMethodInterceptor"></bean> 18 19 <bean id="xinThrowsAdvice" class="Aop_Class.XinThrowsAdvice"></bean> 20 21 <bean id="bizLogicImpleTarget" class="Aop_Class.BizLogicImpl"></bean> 22 23 <!-- 以上是將所有的class導入容器中 --> 24 25 <bean id="pointCutBean" class="org.springframework.aop.support.NameMatchMethodPointcut"> 26 <property name="mappedNames"> 27 <list> 28 <value>sa*</value> 29 <!-- 以sa開始的函數(shù) --> 30 </list> 31 </property> 32 </bean> 33 34 <bean id="defaultAdvice" class="org.springframework.aop.support.DefaultPointcutAdvisor"> 35 <property name="advice" ref="xinBeforeAdvice"/> 36 <property name="pointcut" ref="pointCutBean"/> 37 </bean> 38 39 <bean id="bizLogicImpl" class="org.springframework.aop.framework.ProxyFactoryBean"> 40 41 <property name="proxyInterfaces"> 42 <value>Aop_Class.BizLogic</value> 43 </property> 44 <property name="target"> 45 <ref bean="bizLogicImpleTarget"></ref> 46 </property> 47 48 <property name="interceptorNames"> 49 <list> 50 <!-- <value>defaultAdvice</value> --> 51 <value>xinAfterReturningAdvice</value> 52 <value>xinBeforeAdvice</value> 53 <value>xinMethodInterceptor</value> 54 <value>xinThrowsAdvice</value> 55 </list> 56 </property> 57 </bean> 58 59 60 </beans>?
?
?
?
?
轉載于:https://www.cnblogs.com/yangdagaoge/articles/10060418.html
總結
以上是生活随笔為你收集整理的Spring六:Spring AOP API 上的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于JSP的健身俱乐部会员管理系统的设计
- 下一篇: gradle idea java ssm