當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
第四章:Spring AOP
生活随笔
收集整理的這篇文章主要介紹了
第四章:Spring AOP
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
4.1:面向切面編程
AOP是在運(yùn)行期間將代碼切入到類的指定位置的編程思想。切面能幫助我們模塊化橫切關(guān)注點(diǎn),實(shí)現(xiàn)橫切關(guān)注點(diǎn)的復(fù)用。Spring在運(yùn)行期間將切面植入到指定的Bean中,實(shí)際是通過攔截方法調(diào)用的過程中插入了切面。
4.2:描述切點(diǎn)
SpringAOP中切點(diǎn)的定義使用了AspectJ的切點(diǎn)表達(dá)式。但是只支持AspectJ的部分切點(diǎn)表達(dá)式。arg(),@arg(),this(),target(),@target(),within()$within(),@annotation和execution()。這些切點(diǎn)指示器中execution()是用來實(shí)現(xiàn)執(zhí)行匹配的,其他指示器用來限定匹配的切點(diǎn)。
切點(diǎn)案例:
execution(* 包.包.類.方法(..))
execution(* 包.包.類.方法(..) && within (包A.*)) //附加條件是:包A下的任何方法被調(diào)用
execution(* 包.包.類.方法(..) && bean("beanID")) //附加條件是:匹配特定的bean
execution(* 包.包.類.方法(..) && !bean("beanID")) //附加條件是:不匹配特定的bean
execution(* 包.包.類.方法(int) && args(property)) //有參數(shù)的切點(diǎn)
4.3:使用注解創(chuàng)建切面
使用注解創(chuàng)建切面需要啟用AspectJ的自動(dòng)代理,然后使用@After、@AfterReturning、@AfterThrowing、@Around、@Before注解配合java類創(chuàng)建切面。
啟動(dòng)AspectJ自動(dòng)代理:
在java配置類中啟動(dòng):
@Configuration
@EnableAspectJAutoProxy //啟動(dòng)AspectJ自動(dòng)代理
@ComponentScan
public class ConcertConfig(){@Bean...}
在xml中啟動(dòng):
<beans>
<aop:aspectJ-autoproxy> //啟動(dòng)AspectJ自動(dòng)代理
</beans>
使用注解創(chuàng)建切面
package xxx;
/**
切點(diǎn)=切面+通知
*/
@Aspect
public class Audience{
//切點(diǎn)
@PointCut("execution(* 包.包.類.方法(..))")
public void performance(){} //用于承載切點(diǎn),方法體不重要,之后使用方法名可調(diào)用切面。
//通知
@Before("performance()")
public void helloBefore(){System.out.println("前置通知執(zhí)行")}
@AfterReturning("performance()")
public void helloAfterReturning(){System.out.println("返回后置通知執(zhí)行")
@AfterThrowing("performance()")
public void helloAfterThrowing(){System.out.println("異常后置通知執(zhí)行")}
}
使用注解創(chuàng)建環(huán)繞通知
package xxx;
@Aspect
public class Audience{
//切點(diǎn)
@PointCut("execution(* 包.包.類.方法(..))")
public void performance(){} //用于承載切點(diǎn),方法體不重要,之后使用方法名可調(diào)用切面。
//環(huán)繞通知
@Around("performance()")
public void helloAround(ProceedingJoinPoint jp){
try{
//執(zhí)行前置通知
jp.proceed();
//執(zhí)行后置通知
}catch(Throwable e){
//調(diào)用異常通知
}
}
}
4.4:在xml中聲明切面
一般切面
<aop:config>
<aop:aspect ref = "audience"> //ref 引用了ID為audience的Bean,這個(gè)bean中定義了通知方法。
<!--切點(diǎn)-->
<aop:pointcut id = "performance" expression = "execution(* 包.包.類.方法(..))" />
<!--環(huán)繞通知-->
<aop:before pointcut-ref = "performance" method = "通知A" />
<aop:after-returning pointcut-ref = "performance" method = "通知B" />
<aop:after-throwing> pointcut-ref = "performance" method = "通知C'/>
</aop:aspect>
</aop:config>
環(huán)繞通知切面
<aop:config>
<aop:aspect ref = "audience">
<!--切點(diǎn)-->
<aop:pointcut id = "performance" expression = "execution(* 包.包.類.方法(..))" />
<!--環(huán)繞通知-->
<aop:around pointcut-ref = "performance" method = "helloAround"> //指點(diǎn)環(huán)繞通知方法
</aop:aspect>
</aop:config>
4.5:注入AspectJ切面
???????????????????
轉(zhuǎn)載于:https://www.cnblogs.com/Xmingzi/p/9007553.html
總結(jié)
以上是生活随笔為你收集整理的第四章:Spring AOP的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用php蓝天采集器抓取今日头条ajax
- 下一篇: Python 工具——Anaconda+