spring boot使用 aop 处理方法的执行顺序

引入aop


		
			org.springframework.boot
			spring-boot-starter-aop
		

创建切面处理类(可以做调用方法前校验session的处理,因为)

package com.jzy.aspect;

import com.jzy.util.PublicUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/*
 * Created by zzb on 2017/10/23.
 *
 */
@Aspect
@Component
public class HttpAspect {
    private final  static Logger logger = LoggerFactory.getLogger(HttpAspect.class);
    /*拦截对对包下面的girlList方法(不管里面是什么参数)*/
//    @Before("execution(public * com.imooc.controller.EmailController.toIndex(..))")
    /*@Before("execution(public * com.imooc.controller.EmailController.*(..))")
    public void log(){
        System.out.println("6666666666");
    }

    @After("execution(public * com.imooc.controller.EmailController.*(..))")
    public void logAfter(){
        System.out.println("7777777777777777");
    }*/

    /*切点为controller包下面的所有类的所有方法*/
    @Pointcut("execution(public * com.jzy.controller.*.*(..))")
    public void log(){

    }

    /*进入方法之前执行*/
    @Before("log()")
    public void doBefore(JoinPoint joinPoint){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        logger.info("@Before:"+PublicUtil.getCurrentTimestamp());
        /*logger.info("开始拦截");
        //url
        logger.info("url={}",request.getRequestURL());
        //method 类型(GET ,POST .....)
        logger.info("method={}",request.getMethod());
        //ip
        logger.info("ip={}",request.getRemoteAddr());
        //类方法
        logger.info("class_method={}",joinPoint.getSignature().getDeclaringTypeName() +"."+ joinPoint.getSignature().getName());

        //参数
        logger.info("args={}",joinPoint.getArgs());*/
    }

    /*方法执行之后执行*/
    @After("log()")
    public void doAfter(){
        logger.info("doAfter:"+PublicUtil.getCurrentTimestamp());
    }

    //controller中结果返回之后调用
    @AfterReturning(returning = "object",pointcut = "log()")
    public void doAfterReturning(Object object){
        logger.info("doAfterReturning:"+ PublicUtil.getCurrentTimestamp());
//        logger.info("response={}",object.toString());//拦截到void类型的方法也会报错
    }

}

controller方法

@RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){
        logger.info("controller:"+PublicUtil.getCurrentTimestamp());
//        logger.info("邮件属性是:"+emailProperties.toString());
//        return new ModelAndView("user/index");
        emailRecordReposity.findOne(1);//执行一个数据库查询拉开时间间隔
        return "欢迎来到德莱联盟"+ PublicUtil.getCurrentTimestamp();
    }

最后附上一张验证结果图 (3和4由于执行的过程比较简单看不出来先后顺序,但其实@After是在Controller执行完返回结果之前执行的,总结下来,执行的顺序是@Before-》myMethod->@After->myMethod返回结果-》@AfterReturning


spring boot使用 aop 处理方法的执行顺序_第1张图片


你可能感兴趣的:(aop,spring,boot,aop)