springboot使用AOP

springboot使用AOP

概述

AOP是一种编程范式,与语言无关,是一 种程序设计思想 ;
面向切面 (AOP): Aspect Oriented Programming 
面向对象 (OOP) :Object Oriented Programming 
面向过程 (POP) :Procedure Oriented Programming 

使用案例:

  • pom
  <!-- aop -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2.创建aop类
import java.text.SimpleDateFormat;
import java.util.Date;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
/**service层的aop增强
 */
@Aspect//aop必须注解
@Configuration//注入spring
public class ServiceAop {
    private final static Logger  logger=LoggerFactory.getLogger(ServiceAop.class);//使用日志
    //设置增强的方法----
    //service层所有方法都调用
   //Pointcut定义公用方法execution可以被其他注解使用
    @Pointcut("execution(public * com.demo1.mall.service.*.*(..))")  //具体到每一个方法
    public void webLog(){} 

    @Around("webLog()")//环绕,或者此处直接写"execution(public * com.demo1.mall.service.*.*(..))"
    public void ServiceAopAround(ProceedingJoinPoint prothrows Throwable {
        //logger.info("pre handle");//日志
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date start=new Date();
        //执行前时间
        logger.info("开始时间:"+(sdf.format(start)));//日志
        //调用方法
        pro.proceed();
        //执行后时间
        Date stop=new Date();
        logger.info("结束时间:"+(sdf.format(stop)));//日志
        //总时间
        long time=stop.getTime()-start.getTime();
        logger.info("时间总计:"+time+"ms");//日志
    }
}

/**service层的aop增强
 */
@Aspect
@Configuration
public class ServiceAop {
	private final static Logger  logger=LoggerFactory.getLogger(ServiceAop.class);
	@Pointcut("execution(public * com.demo1.mall.service.*.*(..))")  //service层所有方法都调用
    public void webLog(){} 
	@Around("webLog()")
	public Object ServiceAopAround(ProceedingJoinPoint pro) throws Throwable {
		//调用方法
		Object object=pro.proceed();//获取方法返回值
		return object;
	}
}

获取请求信息,显示日志
    /**
     * 记录日志信息
     */
    @Before("log()")
    public void doBefore(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();//获取请求
        HttpServletRequest request = attributes.getRequest();

        //url
        logger.info("url={}", request.getRequestURL());

        //method
        logger.info("method={}", request.getMethod());//获取请求get/post类型

        //ip
        logger.info("ip={}", request.getRemoteAddr());//获取请求ip

        //类方法
        logger.info("class_method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());//获取方法全路径名

        //参数
        logger.info("args={}", joinPoint.getArgs());//获取方法参数
    }

url=http://12Z.0,0,1:8080/girls
method=GET
ip=127.0.0.1
class_method=com.imooc.controller.GirlController.girlList
args={}
url=http://12Z.0,0,1:8080/girls/20
method=GET
ip=127.0.0.1
class_method=com.imooc.controller.GirlController.girlList
args=20

获取返回信息,设置日志
    @AfterReturning(returning = "object", pointcut = "log()")//获取返回的信息
    public void doAfterReturning(Object object) {//返回值
        logger.info("response={}", object.toString());
    }





posted @ 2018-12-25 19:19  紫月java  阅读(228)  评论(0编辑  收藏  举报