zbb20181205 springboot_aop

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cwag.pss</groupId>
    <artifactId>springboot_aop</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_aop Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>springboot_aop</finalName>
    </build>
</project>
 

 

application.yml

spring:
    aop:
        auto: true
        proxy-target-class: true

LogAop.java

package com.ipss.trip.app.luggageapp.config;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
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.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
* 通过Aop拦截 统一打印日志
*/
@Component
@Aspect
@Slf4j
public class LogAop {

@Pointcut("execution(public * com.zbb.app.controller..*.*(..))")
public void apiLogAop() {
log.info("apiLogAop");
}

@Around("apiLogAop()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
Enumeration<String> headerNames = request.getHeaderNames();
Map<String, Object> headerMap = new HashMap<>(10);
do {
String header = headerNames.nextElement();
headerMap.put(header, request.getHeader(header));
} while (headerNames.hasMoreElements());

String token = request.getParameter("token");
String runum = request.getParameter("runum");
if (StringUtil.isEmpty(token)) {
token = request.getHeader("token");
}
if (StringUtil.isEmpty(runum)) {
runum = request.getHeader("runum");
}
long start = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
log.info("***************************start " + sdf.format(start) + " *************************************************");
String method = request.getMethod();
String uri = request.getRequestURI();
log.info("流水号:{}:请求:地址:{}:请求类型:{}:参数:{}:", runum, uri, method, JSONUtil.toJsonStr(request.getParameterMap()));
Object result = pjp.proceed();
long end = System.currentTimeMillis();
log.info("流水号:{}:响应:地址:{}:请求类型:{}:响应耗时:{}ms:响应结果:{}:", runum, uri, method, (end - start), JSONUtil.toJsonStr(result));
return result;
}
}

HelloController.java

package com.zbb.app.controller;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
 
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    @ResponseBody
    public String hello(@RequestParam String name) {
        return "Hello " + name;
    }
 
}

 

Application.java

package com.zbb.app;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
 
/**
 * 
 */
@SpringBootApplication
@EnableScheduling
public class Application {
 
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
 

 

posted @ 2018-12-05 10:49  DaryllD  阅读(143)  评论(0编辑  收藏  举报