javascript
Spring框架笔记(二十二)——切点表达式的重用
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
在編寫(xiě) AspectJ 切面時(shí), 可以直接在通知注解中書(shū)寫(xiě)切入點(diǎn)表達(dá)式. 但同一個(gè)切點(diǎn)表達(dá)式可能會(huì)在多個(gè)通知中重復(fù)出現(xiàn).
在 AspectJ 切面中, 可以通過(guò) @Pointcut 注解將一個(gè)切入點(diǎn)聲明成簡(jiǎn)單的方法. 切入點(diǎn)的方法體通常是空的, 因?yàn)閷⑶腥朦c(diǎn)定義與應(yīng)用程序邏輯混在一起是不合理的.?
切入點(diǎn)方法的訪問(wèn)控制符同時(shí)也控制著這個(gè)切入點(diǎn)的可見(jiàn)性. 如果切入點(diǎn)要在多個(gè)切面中共用, 最好將它們集中在一個(gè)公共的類(lèi)中. 在這種情況下, 它們必須被聲明為 public. 在引入這個(gè)切入點(diǎn)時(shí), 必須將類(lèi)名也包括在內(nèi). 如果類(lèi)沒(méi)有與這個(gè)切面放在同一個(gè)包中, 還必須包含包名.
其他通知可以通過(guò)方法名稱(chēng)引入該切入點(diǎn).
好吧,我們之前提過(guò)連接點(diǎn)、切點(diǎn)的定義。現(xiàn)在還能分清嗎?如果這里需要做個(gè)分辨,那么你可以將切點(diǎn)理解為切點(diǎn)表達(dá)式,連接點(diǎn)為各個(gè)通知的JointPoint參數(shù)。
我們看兩個(gè)切面的定義,其中一個(gè)切面的類(lèi)方法中有一個(gè)方法用于聲明切點(diǎn)表達(dá)式,這樣該切點(diǎn)類(lèi)的其他通知都可以使用這個(gè)方法來(lái)注解各個(gè)通知的切點(diǎn)表達(dá)式;其他切點(diǎn)的通知也可以只用,但要寫(xiě)清類(lèi)名。
配置文件:
<?xml?version="1.0"?encoding="UTF-8"?> <beans?xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-4.1.xsdhttp://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-4.1.xsd"><context:component-scan?base-package="com.happBKs.spring.aopbasic.aop1"></context:component-scan><!--?使用AspectJ注解起作用。自動(dòng)為匹配的類(lèi)生成代理對(duì)象??--><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>測(cè)試代碼:
@Testpublic?void?testSpringAOP(){//1.?創(chuàng)建spring?的?IOC?容器ApplicationContext?ac=new?ClassPathXmlApplicationContext("applicationContext.xml");//2.?從IOC容器獲取bean實(shí)例AtithmeticCalculate?atithmeticCalculate?=?(AtithmeticCalculate)ac.getBean(AtithmeticCalculate.class);//考察一下代理對(duì)象是否生成System.out.println(atithmeticCalculate.getClass().getName());//3.?使用beanSystem.out.println("Example?1:");int?result=atithmeticCalculate.add(10,?5);System.out.println(result); // System.out.println("\r\nExample?2:"); // int?result2=atithmeticCalculate.div(10,?0); // System.out.println(result2);}輸出結(jié)果:
com.sun.proxy.$Proxy10
Example 1:
data validate---begin to add with [10, 5]
data is OK
data validate 2---begin to add with [10, 5]
data is OK
15
轉(zhuǎn)載于:https://my.oschina.net/happyBKs/blog/483829
總結(jié)
以上是生活随笔為你收集整理的Spring框架笔记(二十二)——切点表达式的重用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 关于如何使用反编译器,获取源码清单
- 下一篇: 办公室颈椎运动