2012-08-01 14 views
8

मेरे शोध से मुझे पता है कि AspectJ का उपयोग करने के दो तरीके हैं। सबसे पहले A.aj वर्ग और दूसरा A.java में एनोटेशन @Aspect जोड़कर बना रहा है।AspectJ: दो प्रकार के ट्यूटोरियल

मैं इस दूसरी तरह के लिए एक अच्छा ट्यूटोरियल के लिए देख रहा था, विशेष रूप से

@After("call(void fooMethod())") 
@Around("call(void sendAndReceive())") 
@Before("execution(String greeting(..)) && args(context)") 

तरह लाइनों के बारे में है, लेकिन मैं नहीं जानता कि वे किस तरह कहा जाता है।

क्या आप कुछ ट्यूटोरियल की सिफारिश कर सकते हैं?

उत्तर

9

एनोटेशन की भूमिका पर जोर देने के लिए इस शैली को @AspectJ कहा जाता है। official docs और @AspectJ cheat sheet पर एक नज़र डालें।

1

एनोटेशन और एक्सएमएल तरीके:

एनोटेशन रास्ता: मिनिमल एक्सएमएल कॉन्फ़िग फ़ाइल:

<!-- Enable autoproxy to pick up all Java files tagged as @Aspect behave like Aspects --> 
<aspectj-autoproxy/> 
<!-- define bean --> 
<!-- Note: MyUselessAspect.java should exist and this class must be tagged as @Aspect --> 
<bean id="myUselessAspect" class="...MyUselessAspect" /> 

एक्सएमएल रास्ता: मिनिमल एक्सएमएल विन्यास:

<aop:config> 
    <aop:aspect ref="myUselessAspect"> 
     <!-- this point-cut picks all methods of any return type, from any package/class with any number of Parameters --> 
    <aop:before method="doSomethingBeforeMethodCall" pointcut="execution(* *.*(..))"/> 
    <aop:after method="doSomethingAfterMethodCall" pointcut="execution(* *.*(..))"/> 
    </aop:aspect>   
</aop:config> 
<!-- No need to Annotate this java Class as @Aspect. Neither you need to define any 
Point-cuts or Advices in the Java file. The <aop:config> tag takes care of everything --> 
<bean id="myUselessAspect" class="...MyUselessAspect"></bean> 

कोड में कोई बदलाव आवश्यक नहीं है।

पूर्व अनुरोध: aop नाम स्थान एक्सएमएल फ़ाइल

+0

AspectJ उपयोग करने वाले लोगों के लाभ के लिए ध्यान देने योग्य बात में मौजूद होना चाहिए, लेकिन वसंत से परिचित नहीं - विक्रम का जवाब यहाँ वसंत के कॉन्फ़िगरेशन में पहलुओं को कॉन्फ़िगर कैसे संबोधित कर रहा है फ़ाइल। इसके अलावा, वह एक्सएमएल कॉन्फ़िगरेशन फ़ाइल के माध्यम से एक पहलू को परिभाषित करने के लिए * तीसरा * तरीका पेश कर रहा है। एक समान क्षमता AspectJ कॉन्फ़िगरेशन फ़ाइल (आमतौर पर 'aop.xml' कहा जाता है) में उपलब्ध होती है, जब आप सीधे वसंत के बजाय AspectJ में प्रोग्रामिंग कर रहे होते हैं। दुर्भाग्यवश, यह मूल प्रश्न को संबोधित नहीं कर रहा है। –

संबंधित मुद्दे