1. ما هو AOP
AOP هو اختصار للبرمجة الموجهة نحو الجانب ، مما يعني البرمجة الموجهة نحو الجانب. AOP هو في الواقع استمرار لنموذج تصميم GOF.
2. بعض المصطلحات حول الربيع AOP:
A. الجانب: في الربيع AOP ، يمكن تنفيذ FACET باستخدام فئة عامة أو في فئة عادية مع aspect
ب.
نصيحة: إجراء يتم تنفيذه على نقطة مشتركة محددة في القسم. هناك أنواع مختلفة من الإخطارات ، بما في ذلك "حول" ، "قبل" و "بعد". تستخدم العديد من أطر عمل AOP ، بما في ذلك Spring ، اعتراضات كنماذج للإخطار والحفاظ على سلسلة اعتراضية محورها نقطة الارتباط.
D. PointCut: تحديد واحد أو مجموعة من الأساليب ، والتي يمكن أن تنشئ الإخطارات عند تنفيذ هذه الأساليب. يستخدم SPRING SideJ Point-Cut Syntax بشكل افتراضي.
3. نوع الإخطار
A.
B. الإخطار بعد العودة (@AfterReturning): يتم تنفيذ الإخطار بعد نقطة اتصال (نقطة انضمام) بشكل طبيعي: على سبيل المثال ، لا ترمي الطريقة أي استثناء ويعود بشكل طبيعي
C. الإخطار بعد التخلص من الاستثناء (AfterThroing): تم تنفيذ الإخطار عندما يلقي الطريقة الاستثناء وينتهي
D. بعد الإخطار (After): تم تنفيذ الإخطار عندما يخرج نقطة اتصال (سواء كان ذلك عائدًا طبيعيًا أو مخرجًا غير طبيعي)
E. المحيط الإخطار (@AROUND): الإخطارات المحيطة بنقطة انضمام ، مثل مكالمات الطريقة. هذا هو أقوى نوع الإخطار ، ويمكن للإشعارات المحيطة إكمال السلوك المخصص قبل وبعد مكالمات الطريقة. كما أنه يختار ما إذا كان يجب متابعة تنفيذ نقاط الاتصال أو إرجاع قيمة الإرجاع الخاصة بهم مباشرة أو رمي استثناء لإنهاء التنفيذ
4. ASPECTJ تكوين AOP
يحتوي تكوين الربيع AOP على نمطان:
أ.
B. SideJ Style = تطبيق AOP الربيع باستخدام نموذج التعليق التوضيحي
5. مثال
testaspect
حزمة com.spring.aop ؛ / *** القسم*/ الفئة العامة testaspect {public void doAfter (JoinPoint jp) {system.out.println ("طريقة إنهاء السجل:" + jp.getTarget (). getClass (). getName () + ". } الكائن العام doAround (proseingJoinpoint PJP) يلقي رمي {long time = system.currentTimeMillis () ؛ كائن Retval = pjp.proceed () ؛ الوقت = system.currentTimeMillis () - الوقت ؛ System.out.println ("وقت العملية:" + time + "MS") ؛ إرجاع Retval. } public void debefore (JoinPoint jp) {system.out.println ("السجل طريقة بدء التشغيل:" + jp.getTarget (). getClass (). getName () + ". } public void dothrowing (JoinPoint JP ، throw ex) {system.out.println ("method" + jp.getTarget (). getClass (). getName () + ". System.out.println (ex.getMessage ()) ؛ } private void sendex (String ex) {// todo send sms أو elemt there}} حزمة com.spring.service ؛ / ** * واجهة A */ Interface Aservice {public void fooa (String _msg) ؛ باطل البارا () ؛ } حزمة com.spring.service ؛ / ***فئة التنفيذ من الواجهة A*/ public class aserviceImpl تنفذ aservice {public void bara () {system.out.println ("aserviceimpl.bara ()") ؛ } public void fooa (string _msg) {system.out.println ("aserviceimpl.fooa (msg:"+_ msg+")") ؛ }} حزمة com.spring.service ؛ / ** * فئة الخدمة B */ الفئة العامة BServiceImpl {public void barb (String _msg ، int _type) {system.out.println ("bserviceimpl.barb (msg:"+_ msg+"type:"+_ type+") ؛ إذا كان (_type == 1) رمي جديد غير alfictalargumentException ("استثناء اختبار") ؛ } public void foOb () {system.out.println ("bserviceimpl.foob ()") ؛ }}ApplicationContext
<؟ Xmlns: aop = "http://www.springframework.org/schema/aop" http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http: الافتراضي-autowire = "AutoDetect"> <aop: config> <aop: application id = "testaspect" ref = "sideBean"> <!-تكوين جميع طرق جميع الفئات أو الواجهات تحت com.spring.service-> <aop: pointcut id = "businsservice" التعبير = "التنفيذ (*com.spring.service. pointcut-ref = "businessservice" method = "debefore"/> <aop: بعد pointcut-ref = "businessService" method = "doAfter"/> <aop: ound ouldercut-ref = "businessService" = "doRound"/> <aop: pointcut-in-rething-ref = </aop: config> <bean id = "sideBean"/> <bean id = "aservice"> </bean> </bans>
اختبار الفئة Aoptest
يمتد الفئة العامة AOPTest الملخصي expendencyInjectionSpringConteStts {private aservice aservice ؛ BServiceImpl Bservice الخاص ؛ سلسلة محمية [] getConfigLocations () {string [] configs = new string [] {"/applicationContext.xml"} ؛ إرجاع التكوينات ؛ } / *** اختبار الاتصال العادي* / public void testCall () {system.out.println ("اختبار springtest Junit") ؛ aservice.fooa ("Junit Test Fooa") ؛ aservice.bara () ؛ bservice.foob () ؛ bservice.barb ("Junit Test Barb" ، 0) ؛ } / ** * اختبار ما بعد الرمي * / public void testthrow () {try {bservice.barb ("Junit Call Barb" ، 1) ؛ } catch (alfulalArgumentException e) {}} public void setaservice (aservice service) {aservice = service ؛ } public void setBservice (BserviceImpl Service) {bservice = service ؛ }}
نتائج التشغيل كما يلي:
السجل بداية الطريقة: com.spring.service.aserviceimpl.fooa aserviceimpl.fooa (msg: junit test fooa) طريقة إنهاء السجل: com.spring.serviceympl.fooa time: 0 ms log method: spring.service.aserviceimpl.bara com.spring.service.AserviceImpl.Bara وقت العملية: 0 ms سجل السجل الطريقة: com.spring.service.bserviceimpl.foob bserviceimpl.foob () سجل نهاية السجل: com.spring.service.bserviceimpl.foob. bserviceimpl.barb (msg: junit test barb type: 0) السجل طريقة إنهاء السجل: com.spring.service.bserviceimpl.barb الوقت: 0 ms log method: com.spring.service.bserviceimpl.barb bserviceimpl.barb (msg: junit call type: com.spring.service.bserviceimpl.barb طريقة com.spring.service.bserviceimpl.barb استثناء اختبار استثناء