AOP-AOP function test

1, the business logic class section and to put Spring container, which is cut and note type (@Aspect);

2, the notification method based on the cut label notification annotation, to tell when and where Spring Run (@Pointcut ( "execution ()"));

3, open the annotation-based AOP mode (@EnableAspectJAutoProxy).

@EnableAspectJAutoProxy
@Configuration
public class MainConfigAop {

    @Bean
    public MathCal mathCal() {
        return new MathCal();
    }

    @Bean
    public LogAspect logAspect() {
        return new LogAspect();
    }
}
public class MathCal {

    public int div(int i, int j) {
        System.out.println("mathCal.div");
        return i/j;
    }
}
@Aspect
public class LogAspect {

    @Pointcut("execution(public int com.yyc.bean.MathCal.*(..))")
    public void pointCut() {

    }

    @Before("pointCut()")
    public void methodStart(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        System.out.println(joinPoint.getSignature().getName()+"除法开始。。。参数列表为{"+ Arrays.asList(args)+"}");
    }


    @After("pointCut()")
    public void methodEnd(JoinPoint joinPoint) {
        System.out.println(joinPoint.getSignature().getName()+"除法结束。。。");
    }

    @AfterReturning(value = "pointCut()", returning = "result")
    public void methodReturn(Object result) {
        System.out.println("除法返回。。。"+result);
    }

    @AfterThrowing(value = "pointCut()", throwing = "exception")
    public void methodException(Exception exception) {
        System.out.println("除法异常。。。"+exception);
    }
}
div division begins. . . Parameter list is {[. 1,. 1 ]} 
mathCal.div 
div division ends. . . 
Division returned. . . 1

 

Guess you like

Origin www.cnblogs.com/AyasatoMayoi/p/10931106.html
AOP