SPRING learning (59) - annotation-based programming AOP section

Part AOP aspects are described based on the XML configuration programming, in addition to the XML configuration programming AOP aspects, but also can be implemented by programming annotations manner AOP aspects herein, this annotation based AOP to introduce programming through a small example.

 

 

1, in the spring AOP into use, not only to be imported spring-aop.jar, spring-aspects.jar also need to import, and aspectjweaver.jar aopalliance.jar, but is spring-aspects.jar aspectjweaver.jar dependent, aopalliance. spring-aop.jar jar is dependent, in turn spring-aop.jar spring-mvc.jar dependent, so long as the introduced spring-mvc.jar and spring-aspects.jar on the line.

 

2、spring-aop-annotion.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <! - configuration automatically scan packages ->
    <context:component-scan base-package="springAopAnnotion"></context:component-scan>
    <! - where the type of automatic cut method is a method of generating a proxy object matching. ->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

 

3, is enhanced class Sleep.java

package springAopAnnotion;

import org.springframework.stereotype.Component;

// implementation classes added Spring's IOC container management 
@Component ( " Sleeping " )
 public  class Sleep {

    public void sleep(String who){
        System.out.println(who + " want to sleep!");
    }
}

4, strengthen class SleepAspect.java

package springAopAnnotion;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * * There are two ways to declare the entry point method
 * 1, a statement ahead of the entry point @PointCut, subsequent references an entry point in the cutting process
 * 2, a direct entry point in the statement cut method
 *     
 * There are five ways to cut
 * 1, the method of execution before @before
 * 2, after performing the method, before returning @after
 * 3, the method returns @afterReturning
 * 4, before and after the execution method @around
 * 5, throw an exception when @AfterThrowing
 * @Author qiaozhong
 */
@Component
@Aspect
public class SleepAspect {
    
    /**
     * A statement ahead of the entry point @PointCut, subsequent references an entry point in the cutting process
     * Cut-off point before the statement execution method
     */
    @Pointcut("execution(* springAopAnnotion.Sleep.*(..))")
    public void beforeMethod(){}
    
    /**
     * A statement ahead of the entry point @PointCut, subsequent references an entry point in the cutting process
     * After the cut-off point statement execution method
     */
    @Pointcut("execution(* springAopAnnotion.Sleep.*(..))")
    public void afterMethod(){}
    
    /**
     * Before execution method, cut method to weaving, the entry point for beforeMethod declared before ()
     * @param joinPoint
     */
    @Before("beforeMethod()")
    public void beforeAspect(JoinPoint joinPoint){
        . The System OUT .println ( " before performing the method, the parameter = " + joinPoint.getArgs () [ 0 ]);
    }
    
    /**
     * Methods executed, before returning to be woven into the cut method, the entry point for afterMethod declared before ()
     * @param joinPoint
     */
    @After("afterMethod()")
    public void afterAspect(){
        . The System OUT .println ( " the method performs " );
    }
    
    /**
     * Method returns, the weaving To cut, the entry point disposed directly in the present process
     * @param joinPoint
     */
    @AfterReturning(value="execution(* springAopAnnotion.Sleep.*(..))")
    public void afterReturnAspect(){
        . The System OUT .println ( " method returns " );
    }
    
     /**
     * Abnormality notification: The following code execution exception occurs when certain methods, the entry point arranged directly in the present process
     */
    @AfterThrowing(value="execution(* springAopAnnotion.Sleep.*(..))",throwing="e")
    public void afterThorwingMethod(JoinPoint jp, NullPointerException e){
        String methodName = jp.getSignature().getName();
        System.out.println("【异常通知】the method 【" + methodName + "】 occurs exception: " + e);
    }
    
      /**
      * Around advice: before and after the target method were performed to execute some code executed when the exception occurred some other code entry point disposed directly in the present process
      * @return 
      */
     @Around(value="execution(* springAopAnnotion.Sleep.*(..))")
     public void aroundMethod(ProceedingJoinPoint jp) {
         try {
             . System OUT .println ( " aroundMethod front section " );
             jp.proceed();
             . The System OUT .println ( " aroundMethod rear section " );
         } catch (Throwable e) {
             e.printStackTrace ();
         }
     }
}

 

5, the test class

package springAopAnnotion;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSleep {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("springConfig/spring-aop-annotion.xml");
        Sleep sleep = (Sleep)ac.getBean("sleeping");
        sleep.sleep("Little ball");
    }
}

 

result:

aroundMethod front section
Before the method is performed, the reference = Little Ball
Little ball want to sleep!
aroundMethod rear section
After the execution method
After the method returns

 

Guess you like

Origin www.cnblogs.com/gllegolas/p/11771627.html