SpringBoot aop demo

package jyl.mix.springbootaop.aop;

import com.google.gson.Gson;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 打日志操作
 * 定义Aspect
 */
@Aspect
@Component
public class LogAop {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String REQUEST_LOG = "request[url:{}, method:{}, clientIp:{}, params:{}]";

    @Autowired
    private Gson gson;

    /**
     * 定义PointCut 即jyl.mix.springbootaop.controller包内的所有public方法
     */
    @Pointcut("execution(public * jyl.mix.springbootaop.controller..*.*(..))")
    public void URLlog() {
    }

    /**
     * 切点方法执行前进行的操作
     * 此处打印请求的各种参数
     * @param joinPoint
     */
    @Before("URLlog()")
    public void logBefore(JoinPoint joinPoint) {
        HttpServletRequest request = (((ServletRequestAttributes) RequestContextHolder
            .getRequestAttributes())).getRequest();
        logger.info(REQUEST_LOG, request.getRequestURL().toString(),
            request.getMethod(), request.getRemoteAddr(), Arrays.toString(joinPoint.getArgs()));
    }

    /**
     * 方法返回后执行的内容
     * @param returnObj
     */
    @AfterReturning(returning = "returnObj", pointcut = "URLlog()")
    public void logAfter(Object returnObj) {
        logger.info("response : " + gson.toJson(returnObj));
    }
}
package jyl.mix.springbootaop.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoAController {

    @RequestMapping("/demo/a")
    public String showA(){
        return "demoA";
    }
}
package jyl.mix.springbootaop.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoBController {

    @RequestMapping("/demo/b")
    public String showA(){
        return "demoB";
    }
}
package jyl.mix.springbootaop;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;

@SpringBootApplication
public class AopApplicationContext implements EmbeddedServletContainerCustomizer {

    @Value("8087")
    private int port;

    public static void main(String[] args) {
        SpringApplication.run(AopApplicationContext.class, args);
    }

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        container.setPort(port);
    }
}

浏览器分别访问http://127.0.0.1:8087/demo/ahttp://127.0.0.1:8087/demo/b
可以观察到日志被成功打印出
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值