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;
package com.yanshu;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@SpringBootApplication
@MapperScan({"com.yanshu.dao","com.yanshu.service"})
@EnableAspectJAutoProxy//启动@Aspect 注解
//@ConfigurationProperties("classpath:log4j.properties")
public class YsssApplication {
public static void main(String[] args) {
SpringApplication.run(YsssApplication.class, args);
}
}
@Component
public class HttpAspect {
private Logger logger= LoggerFactory.getLogger(HttpAspect.class);
@Pointcut("execution(public * com.yanshu.controller..*.*(..))")
public void log(){
}
@Before("log()")
public void doBefore(JoinPoint joinPoint){
logger.info("启动之前");
ServletRequestAttributes attributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request=attributes.getRequest();
//url
logger.info("url={}",request.getRequestURI());
//model
logger.info("mothod={}",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("启动完成");
}
@AfterReturning(pointcut = "log()",returning = "object")
public void doAfterRetruning(Object object){
logger.info("response={}",object.toString());
}
}