Hours 8: Surround notice

.1) around advice: Dian final and other places may be notified, the most powerful of a notification when an abnormality occurs before and after the target method Dian;

  You can get full control of the target method (whether the target method before Dian Dian Dian parameters after the execution Dian return values, etc.)

  1. Import corresponding jar package has been pre-notification example here do not explain the excessive

  2. Write a business class and write around advice classes

    2.1 Business class code as follows

package net.bdqn.hbz.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * Around advice
 */
public class Surround  implements MethodInterceptor {

    /**
     *
     * @param invocation
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Object reult=null;
        try{
            System.out.println ( "pre-notification is performed" );
             // execution of the target method 
            reult = invocation.proceed ();
             // to get some information about the notification through the post 
            System.out.println ( "post-notification: target Object: "+ invocation.getThis () +" , the target method: "+ invocation.getMethod () getName ( ) +.", the target number of parameters: "+ 
                    . invocation.getArguments () length +", the return value: " + reult);
        }catch (Exception e){
            System.out.println ( "surround performed notification" );
        }
        return star;
    }
}

      Some information 2.1.1 target method can be obtained by MethodInvocation

      2.1.2 How to control whether to perform a method goal is to invocation.proceed (); do not write to write is not executed

   3. The entry point for a connecting section and

    3.1 exception notification class injecting them into the container SpringIOC

<! -     injection Surround notification class -> 
    < the bean ID = "Surround" class = "net.bdqn.hbz.aop.Surround" > </ the bean >

    3.2 Configuration section and entry point for relationships

    <! -    the association notification addStudent and surround -> 
    < AOP: config > 
<! -         the configuration information of the entry point -> 
        < AOP: the pointcut ID = "addSurround" expression The = "Execution (public void addStudent (NET. bdqn.hbz.pojo.Student)) " /> 
! <-         configure connection -> 
        < AOP: Advisor the advice-REF =" Surround " the pointcut-REF =" addSurround " > </ AOP: Advisor > 
    </ AOP: config >

  4. bottom surround notified implemented by intercepting

Guess you like

Origin www.cnblogs.com/thisHBZ/p/12511716.html