Spring aspect-Oriented Programming example (XML configuration form vs@ annotation form)

Source: Internet
Author: User
Tags aop

First, the XML configuration form

1. Add a facet-oriented configuration in the spring configuration file
The method in the slice class is executed when any method of the Com.activemq.service.impl.ConsumerServiceImpl interface implementation class is called.

2. Write Slice class

Attention:
1) can not do the Web layer (for example: com.activemq.action.ConsumerController) Proxy insert operation, the Pro-test is invalid.
(Before the web layer is considered invalid, in fact, is not, invalid because the slice configuration is located in the file, If it is spring-mvc.xml (SPRINGMVC profile), it is valid if it is not valid in Applicationcontext.xml (Spring configuration file). )
2) <aop:pointcut> If it is in the <aop:aspect> element, the named Pointcut can only be accessed by the elements defined in the current <aop:aspect>. to be able to be accessed by all the enhancements defined in the entire <aop:config> element, you must define a pointcut under <aop:config>.

ii. @Aspect Annotated form

1. Configuration

Enable @aspect in spring configuration file Applicationcontext.xml and declare the notification class

<!---<id= "Aspectbean"  class= "Icom.axx.action.AopAspect"/><!----<  />

2. Write notification class

 Packageicom.axx.action;ImportJava.util.HashMap;ImportJava.util.Map;ImportOrg.aspectj.lang.JoinPoint;ImportOrg.aspectj.lang.annotation.After;ImportOrg.aspectj.lang.annotation.Aspect;ImportOrg.aspectj.lang.annotation.Before;Importorg.springframework.beans.factory.annotation.Autowired;ImportIcom.axx.dao.AopAspectDao;/*** for the Section class * is used to save the trunk patrol phone port call interface information. * @authorWangxiangyu **/@Aspect Public classaopaspect {@Autowired Aopaspectdao Aopaspectdao;  Public Static FinalThe String EDP = "Execution (* icom.axx.service.impl.*). *(..))"; /*** Pre-notification: Execute before Target split method call*/@Before (EDP) Public voidDobefore (Joinpoint jp) {}/*** Final Notification: * Executes after the target method call (regardless of whether the target method has an exception)*/@After (EDP) Public voidDoafter (joinpoint JP) {//Get class nameString ClassName =jp.gettarget (). GetClass (). GetName (); //method name to invokeString MethodName =jp.getsignature (). GetName (); //parameters of the calling methodobject[] Argarr =Jp.getargs (); String Arg= ""; if(NULL! = Argarr && argarr.length>0) {             for(Object argobj:argarr) {arg+=argobj.tostring (); }        }        //If the pass parameter is greater than 300 characters, only the first 300 characters are intercepted.         if(Arg.length () >300) {arg= arg.substring (0, 300); } Map<string, string> param =NewHashmap<string, string>(); Param.put ("ClassName", ClassName); Param.put ("MethodName", MethodName); Param.put ("Param_list", ARG); //Insert Tb_base_mobile_gxxj_recordaopaspectdao.saveinvokeinfo (param); }}

Spring aspect-Oriented Programming example (XML configuration form [email protected] annotation form)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.