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/a 与http://127.0.0.1:8087/demo/b
可以观察到日志被成功打印出