這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)如何在SpringAOP中配置注解,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產(chǎn)品對外擴(kuò)展宣傳的重要窗口,一個合格的網(wǎng)站不僅僅能為公司帶來巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺,成都創(chuàng)新互聯(lián)公司面向各種領(lǐng)域:成都不銹鋼雕塑等成都網(wǎng)站設(shè)計、成都全網(wǎng)營銷解決方案、網(wǎng)站設(shè)計等建站排名服務(wù)。
使用注解實現(xiàn)SpringAOP的功能:
例子:
//表示這是被注入Spring容器中的 @Component //表示這是個切面類 @Aspect public class AnnotationHandler { /* * 在一個方法上面加上注解來定義切入點 * 這個切入點的名字就是這個方法的名字 * 這個方法本身不需要有什么作用 * 這個方法的意義就是:給這個 @Pointcut注解一個可以書寫的地方 * 因為注解只能寫在方法、屬性、類的上面,并且方法名作為切入點的名字 * */ //簡單來說就是將查到的方法用myPointCut()方法名代替 @Pointcut("execution(public * com.briup.aop.service..*.*(..))") public void myPointCut(){ } //注:這里面的所有方法的JoinPoint類型參數(shù)都可以去掉不寫,如果確實用不上的話 @Before("myPointCut()")//在myPointCut()中查到的方法之前切入 public void beforeTest(JoinPoint p){ System.out.println(p.getSignature().getName()+" before..."); } /* * @After和@AfterReturning * * @After標(biāo)注的方法會在切入點上的方法結(jié)束后被調(diào)用(不管是不是正常的結(jié)束). * @AfterReturning標(biāo)注的方法只會在切入點上的方法正常結(jié)束后才被調(diào)用. * */ @After("myPointCut()")//在myPointCut()中查到的方法之后切入 public void afterTest(JoinPoint p){ System.out.println(p.getSignature().getName()+" after..."); } @AfterReturning("myPointCut()") public void afterReturningTest(JoinPoint p){ System.out.println(p.getSignature().getName()+" afterReturning"); } @Around("myPointCut()")//在myPointCut()中查到的方法環(huán)繞切入 public Object aroundTest(ProceedingJoinPoint pjp)throws Throwable{ System.out.println(pjp.getSignature().getName()+" is start.."); //調(diào)用連接點的方法去執(zhí)行 Object obj = pjp.proceed(); System.out.println(pjp.getSignature().getName()+" is end.."); return obj; } //在切入點中的方法執(zhí)行期間拋出異常的時候,會調(diào)用這個 @AfterThrowing注解所標(biāo)注的方法 @AfterThrowing(value="myPointCut()",throwing="ex") public void throwingTest(JoinPoint p,Exception ex){ System.out.println(p.getSignature().getName()+" is throwing..."+ex.getMessage()); } }
xml配置:注意給例子中使用的其他的類上面也使用注解
<aop:aspectj-autoproxy/> <context:component-scan base-package="com.briup.aop"/> <!-- 讓Spring掃描注解 --> <context:component-scan base-package="com.briup.aop"></context:component-scan> <!-- 識別AspectJ的注解 --> <aop:aspectj-autoproxy/>
注意:<aop:aspectj-autoproxy proxy-target-class="true"/>這樣配置則是強(qiáng)制使用CGLIB進(jìn)行代理
上述就是小編為大家分享的如何在SpringAOP中配置注解了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
新聞標(biāo)題:如何在SpringAOP中配置注解
文章網(wǎng)址:http://chinadenli.net/article30/gicppo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站策劃、網(wǎng)頁設(shè)計公司、品牌網(wǎng)站設(shè)計、搜索引擎優(yōu)化、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)