<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring-boot.version}</version>
</dependency>
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration
在这个类中存在有一个静态内部类:
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration.DispatcherServletConfiguration
public interface Condition {
boolean matches(ConditionContext var1, AnnotatedTypeMetadata var2);
}
protected static Class<?> resolve(String className, ClassLoader classLoader) throws ClassNotFoundException {
return classLoader != null ? Class.forName(className, false, classLoader) : Class.forName(className);
}
/META-INF/spring-factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=自动配置类全路径名称
public PrintLog {
}
//自动注入application配置文件中已log.switch开头的配置参数
public class LogProperties {
//是否启用打印日志功能
private Boolean enabled = false;
//是否打印调用者ip
private Boolean printIp = false;
//是否打印调用者url
private Boolean printUrl = false
}
public class LogAspect {
private static final Log LOGGER = LogFactory.getLog(LogAspect.class);
private LogProperties logProperties;
"@annotation(com.zl.annotation.PrintLog)") (
public void Log(){}
"Log()") (
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
String methodName = method.getName();
//打印调用url
if (Boolean.TRUE.equals(logProperties.getPrintUrl())){
LOGGER.info("URL:" + request.getRequestURL().toString());
}
//打印ip
if (Boolean.TRUE.equals(logProperties.getPrintIp())) {
LOGGER.info("IP :" + request.getRemoteAddr());
}
//打印方法
LOGGER.info("method :" + methodName);
//打印参数
LOGGER.info("parameter :" + Arrays.toString(joinPoint.getArgs()));
Object result = joinPoint.proceed();
//打印返回结果
LOGGER.info("return :" + JSON.toJSONString(result));
return result;
}
}
//表示在application配置文件中必须配置log.switch.enabled = true才启动自动配置
public class LogAutoConfigure {
//Advice.class是aop切面中关键的切面方法类(@Before,@After等)
//程序中有Advice.class类说明需要使用切面功能,这时才加载自定义的切面类
public LogAspect webLogAspect(LogProperties logProperties){
return new LogAspect(logProperties);
}
}
META-INF/spring.factories
\ =
com.zl.autoConfigure.LogAutoConfigure
maven install
<dependency>
<groupId>com.zl.demo</groupId>
<artifactId>LogStarter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
log:
switch:
enabled: true //启用打印日志功能
printIp: true //打印请求ip
printUrl: true //打印请求url
public class HelloWorldController {
public String helleWorld(String test){
return "hello world!";
}
}
com.zl.aspect.LogAspect : URL:http://localhost:8080/test/hello
com.zl.aspect.LogAspect : IP :0:0:0:0:0:0:0:1
com.zl.aspect.LogAspect : method :helleWorld
com.zl.aspect.LogAspect : parameter :[test]
com.zl.aspect.LogAspect : return :"hello world!"
-end-