Spring Boot AOP Demo

Posted dzcici

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot AOP Demo相关的知识,希望对你有一定的参考价值。

什么是AOP?

AOP面向切面,切面将那些与业务无关,却被业务模块共同调用的逻辑提取并封装起来,减少了系统中的重复代码,降低了模块间的耦合度,同时提高了系统的可维护性。

实现策略JAVA SE动态代理

CGLib

相关注解

@Aspect(方面)

@Pointcut(切入点)

@Before(之前)

@After(之后)

pom.xml

<!--引用AOP-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
AspectTest.class
/**
 * @author 张东明
 * @TODO: 2019/3/4
 * @remark AOP切面类 日志记录
 */
@Aspect
@Component
public class AspectTest {

    private final static Logger logger = LoggerFactory.getLogger(AspectTest.class);

    @Autowired
    private GeLogVisitServiceImpl geLogVisitService;

    @Pointcut("execution(public * com.nf147.platform.web..*.*(..))")
    public void controllerMethod() {

    }

    @Before("controllerMethod()")
    public void LogRequestInfo(JoinPoint joinPoint) throws JsonProcessingException {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        /**
         * 获取Session
         * */
        GeEnterprise user = (GeEnterprise) request.getSession().getAttribute("user");

        int id = 0;
        if (user != null) {
            id = user.getId();
        }
        String remoteAddr = request.getRemoteAddr();
        String requestURI = request.getRequestURI();

        if (remoteAddr != null && requestURI != null) {
            GeLogVisit geLogVisit = null;
            if (id > 0) {
                geLogVisit = new GeLogVisit(id, remoteAddr, requestURI, new Date(), 1);
            } else {
                geLogVisit = new GeLogVisit(remoteAddr, requestURI, new Date(), 1);
            }
            System.out.println("日志记录信息:" + geLogVisit.toString());
            int insert = geLogVisitService.insert(geLogVisit);
            System.out.println(insert);
        }

        StringBuffer requestLog = new StringBuffer();
        requestLog.append("请求信息:")
                .append("URL = {" + requestURI + "},	")
                .append("HTTP_METHOD = {" + request.getMethod() + "},	")
                .append("IP = {" + remoteAddr + "},	")
                .append("CLASS_METHOD = {" + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName() + "},	");

        if (joinPoint.getArgs().length == 0) {
            requestLog.append("ARGS = {} ");
        } else {
            requestLog.append("ARGS = " + new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL)
                    .writeValueAsString(joinPoint.getArgs()[0]) + "");
        }
        System.out.println(requestLog.toString());
    }

    /**
     * @remark 调用之后回归返回结果
     * // TODO: 2019/3/4
     */
    @AfterReturning(returning = "resultVO", pointcut = "controllerMethod()")
    public void logResultVOInfo(JSONResponse resultVO) throws Exception {
        System.out.println("请求结果:" + resultVO.getCode() + "	" + resultVO.getData());
    }

}

 

以上是关于Spring Boot AOP Demo的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot使用AOP实现REST接口简易灵活的安全认证

Spring Boot AOP之对请求的参数入参与返回结果进行拦截处理

Spring Boot Aop

spring aop demo

Spring Boot 功能代码:基于注解+Spring AOP 记录业务数据修改前后记录。

springboot aop使用介绍