5 Notifications and 3 ways to load notifications in spring

Source: Internet
Author: User
Tags aop throwable

5 Notifications and 3 ways to load notifications in spring

5 kinds of notifications in spring

Front notification: Code:/** * Forward notification: Called before executing the method.        The front notification class needs to implement the Methodbeforeadvice interface */public class Empbeforeadvice implements Methodbeforeadvice {@Override public void before (method arg0, object[] arg1, Object target) throws Throwable {System.out.            println ("Here is the Before method");            System.out.println ("Method name:" +arg0+ "method parameter:" +arg1.length+ "target object:" +target);                    System.out.println ("==================="); }    }

Post notification: Code:/**    *  post notification: Called after the call is done, the post notification class is to implement the Afterreturningadvice interface    * */   public class empafteradvice implements afterreturningadvice {         @Override        public void afterreturning (object arg0 ,  method arg1, object[] arg2,                 object target)  throws throwable {            // todo auto-generated method stub             system.out.println ("Here is the Afterreturning method.") ");            system.out.println ("returned objects:"  + arg0  +  the name of the method called: " + arg1 + " Method parameters: "                    + arg2.length +  "target object:"  + target);            system.out.println ("======================");           }   }  

 

Surround notification: Code:/**    *  surround notification: Called before and after the method is invoked. The class needs to inherit Methodinterceptor interface    * */   public class empreturning implements methodinterceptor{         @Override        public object invoke (methodinvocation arg0)  throws throwable {           // todo  auto-generated method stub             System.out.println ("Start of the Invoke method");            object obj=arg0.proceed ()//Let him execute the target method             SYSTEM.OUT.PRINTLN ("The end of the Invoke method");            system.out.println ("=====================");            return obj;//If you return a null, you will get an error       &NBSP}   }  

 

Exception notification: Code:/**    *  Exception Notification: Executes the notification when an exception is executed for the target class. This class inherits the Throwingadivice interface    * */   public class empexception implements  throwsadvice {       public void afterthrowing (Method  Method, object[] args, object target,                 exception ex)  {            system.out.println ("Here is the Afterthrowing method.") ");            system.out.println (the name of the calling method is: " + method Number of parameters for the  +  method: " + args.length                     +  the target object of the call: '  + target +  ' throws an exception: '  + EX);            system.out.println ("================");        }   }  

Reference notification: to be written

3 Ways to load notifications

Loading mode with proxy class: Code: <!--  Pre-notification  -->        <bean id= " Empbeforeadvice " class=" Cn.csdn.aop.EmpBeforeAdvice " />        <!- -  post notification (processing after the action of the method)  -->        <bean id= "Empafteradvice"  class= "Cn.csdn.aop.EmpAfterAdvice"  />        <!--  Surround notification ( Method before and after processing)  -->        <bean id= "empreturning"  class= " Cn.csdn.aop.EmpReturning " />        <!--  Exception notification (when a bug occurs during method execution)  -->        <bean id= "empexception"  class= " Cn.csdn.aop.EmpException " />        <!--  Target class  -->         <bean id= "Empserviceimpl"  class= "Cn.csdn.service.EmpServiceImpl" >            <property name= "Worktime" >                 <value>my worktime is 8 hours!</value>             </property>         </bean>        <!--  Configure a static advisor -->         <bean id= "Slbeforeadvistor"             class= "Org.springframework.aop.support.NameMatchMethodPointcutAdvisor" >             <property name= "Advice"  ref= "Empbeforeadvice" ></property>            <property name= "Mappednames" >                 <list>                     <value>sleep</value>                </list>            </property>        </bean >        <!--  Configure another static advisor -->         <bean id= "Wkaroundadvisor"            class= " Org.springframework.aop.support.NameMatchMethodPointcutAdvisor ">             <property name= "Advice"  ref= "empreturning" ></property>            <property name= "Mappednames" >                 <list>       & nbsp;            <value>work</value>                 </list>             </property>        </bean>        <!--  Agent-->        <bean id= "Proxyfactorybean"  class= "Org.springframework.aop.framework.ProxyFactoryBean" >               <!--  Set the name of the agent proxyinterfaces -->            <property name= "Proxyinterfaces" >                 <list>                     <value>cn.csdn.service.emPservice</value>                < /list>            </property>             <!--  Intercept name  -->             <property name= "Interceptornames" >                 <list>                     <!--define that notification in this, load that notice                         < Value>empbeforeadvice</value> <value>empafteradvice</value>                          <value>Empreturning</value> <value>empexception</value>                     -->                     <value>slbeforeadvistor </value>                     <value>wkaroundAdvisor</value>                    </list>             </property>               <!--  Target object  -->            <property name= "Target"  ref= "Empserviceimpl"  />        </bean>  

 

Default automatic load Notification: Code: <!--  Forward notification  -->        <bean id= " Empbeforeadvice " class=" Cn.csdn.aop.EmpBeforeAdvice " />        <!- -  post notification (processing after the action of the method)  -->        <bean id= "Empafteradvice"  class= "Cn.csdn.aop.EmpAfterAdvice"  />        <!--  Surround notification ( Method before and after processing)  -->        <bean id= "empreturning"  class= " Cn.csdn.aop.EmpReturning " />        <!--  Exception notification (when a bug occurs during method execution)  -->        <bean id= "empexception"  class= " Cn.csdn.aop.EmpException " />        <!--  Target class  -->         <bean id= "Empserviceimpl"  class= "Cn.csdn.service.EmpServiceImpl" >             <property name= "Worktime" >                 <value>my worktime is 8 hours!</value>             </property>         </bean>           <!--  Configure a static advisor -->         

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.