Two kinds of spring AOP configuration

  1. xml configuration
  • Interface proxy method to be defined

 

public interface TestAop {
    public void print(String s);
}
  • Implementation for the interface

 

public  class TestAopImp the implements TestAop {
     public  void Print (String S) { 
        System.out.println ( "specific business logic" ); 
    } 
}
  • Defined section (before and after the operation to be performed by the proxy method)
public  class LogUtil {
     public  void logbefore (the JoinPoint Joinpoint) { // Joinpoint proxy method 
        System.out.println ( "log record before business process" ); 
    } 

    public  void logAfter (the JoinPoint Joinpoint) { 
        System.out.println ( " after logging service processing " ); 
    } 

//     @Around (" Print () ")
 //     public void doAround (ProceedingJoinPoint PJP) throws the Throwable {
 //         System.out.println (" start processing service "); 
/// /         pjp.proceed ();
 //         System.out.println ( "end processing business");
 //     } 

    //Exception occurs during the processing of the 
    public  void doAfterThrowing (Exception E) { 
        System.out.println ( "Exception Notification:" + E); 
    } 
    
    public  void doAfterReturning (Result Object) { 
        System.out.println ( "Rear notification: "+ Result); 
    } 
}
  • xml configuration file
 <bean id="testAop" class="AOP.TestAopImp"/>
 <bean id="LogUtil" class="AOP.LogUtil"/>
    <aop:config>
        <aop:aspect id="aspect" ref="LogUtil">
            <aop:pointcut id="PointtestAop" expression="execution(* AOP.TestAopImp.print*(..))"/>
            <aop:before method="logbefore" pointcut-ref="PointtestAop"/>
            <aop:after method="logAfter" pointcut-ref="PointtestAop"/>
            <!--<aop:around method="doAround" pointcut-ref="PointtestAop"/>-->
            <aop:after-returning method="doAfterReturning" pointcut-ref="PointtestAop" returning="result"/>
            <aop:after-throwing method="doAfterThrowing" throwing="e" pointcut-ref="PointtestAop"/>
        </aop:aspect>
    </aop:config>

  2 . Notes Configuration

  • Open notes plus xml configuration file <aop: aspectj-autoproxy />
  • Guide packet (packet not take notes nonconducting)
    <dependency>
            <groupId>aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.5.3</version>
     </dependency>
     <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
     </dependency>
  • Xml interface and implementation class and the same configuration, the following definitions section
@Aspect
     public  class LogUtil { 
    @Pointcut ( "Execution (* AOP.TestAopImp.print (String))" )
     public  void Print () { 
    } 

    
    @Before ( "Print ()" )
     public  void logbefore (the JoinPoint Joinpoint) { // Joinpoint proxy method 
        System.out.println ( "before the logging service processing" ); 
    } 

    @After ( "Print ()" )
     public  void logAfter (the JoinPoint Joinpoint) { 
        System.out.println ( "after logging service processing " ); 
    } 

//     @Around (" Print () "
)
//    void doAround public (ProceedingJoinPoint PJP) throws the Throwable {
 //         System.out.println ( "start processing service"); 
////         pjp.proceed ();
 //         System.out.println ( "End Processing Business");
 //     } 

    // exception occurs during the processing of 
    the @AfterThrowing (= the pointcut "Print ()", the throwing = "e" )
     public  void doAfterThrowing (exception E) { 
        System.out.println ( +: "exception notification" E ); 
    } 
    
    @AfterReturning (the pointcut = "Print ()", returning = "Result" )
     public  void doAfterReturning (Result Object) { 
        the System.out.the println ( "post-notification:" +println( result);
    }

****************important point****************

  1. around the cut point is equal to the tangent point before adding after the tangent point, both when using one selected from the group pjp.proceed () function is executed is equivalent to the agent
  2. : For the execution order of several tangent point
     the try
    {
        // perform pre-notification;     // perform target method;     // return notification performed; } catche (Exception E) {     ; // notify abnormality } the finally {     // After execution Notify is set; }
        

        









 

Guess you like

Origin www.cnblogs.com/ll9507/p/11294436.html