SpringBoot之自定义简单的注解和AOP

1.引入依赖

<!-- AOP依赖-->
<dependency>
	<groupId>org.aspectj</groupId>
	<artifactId>aspectjweaver</artifactId>
	<version>1.9.8</version>
</dependency>

2.自定义一个注解

package com.example.springbootdemo3.annotation;

import java.lang.annotation.*;

@Target({ElementType.METHOD}) // 指定该注解可以加在方法上
@Retention(RetentionPolicy.RUNTIME) // 指定该注解在运行时保留,可以通过反射获取
@Documented // 表明该注解被包含在javadoc中
public @interface MyLog {

    /**
     * 名称
     */
    String name() default "";
}

3.自定义AOP

package com.example.springbootdemo3.aop;

import com.example.springbootdemo3.annotation.MyLog;
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.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * description
 * 自定义ControllerAOP
 *
 * @author PC 2025/02/24 20:37
 */
@Component
@Aspect
public class MyControllerAop {
    // 定义使用MyLog注解的方法为切入点
    @Pointcut("@annotation(com.example.springbootdemo3.annotation.MyLog)")
    private void pointCut() {
    }

    @Around("pointCut()") // 注解表示该方法是一个环绕通知(around advice),它会在指定的切入点(pointcut)执行前后进行增强处理。
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取倍增强调的类和方法信息
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        // 获取被增强的方法对象
        Method method = methodSignature.getMethod();
        // 获取被增强的方法上的注解
        if (method != null) {
            MyLog myLog = method.getAnnotation(MyLog.class);
            System.out.println("MyLog name:" + myLog.name());
        }
        // 方法名
        String name = method.getName();
        System.out.println("方法名:" + name);

        // 获取Request对象
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        // 访问url
        String url = request.getRequestURL().toString();
        System.out.println("访问url:" + url);
        // 请求方式
        String method1 = request.getMethod();
        System.out.println("请求方式:" + method1);
        // 请求参数
        String queryString = request.getQueryString();
        System.out.println("请求参数:" + queryString);
        // 请求IP
        System.out.println("请求IP:" + getClientIpAddress(request));
        Object proceed = joinPoint.proceed();
        System.out.println("返回值:" + proceed);
        return proceed;
    }

    /**
     * 方法:获取真实IP地址
     * 从常见的IP所在的请头中来获取
     *
     * @param request
     * @return
     */
    private String getClientIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}

3.写个测试Controller试一下

package com.example.springbootdemo3.config;

import com.example.springbootdemo3.annotation.MyLog;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * description
 *
 * @author PC 2025/02/24 21:29
 */
@RestController
@RequestMapping("/aop-test")
public class AopTestController {
    @PostMapping("/test")
    @MyLog(name = "aop测试")
    public String aopTest(@RequestBody Map map) {
        System.out.println(map);
        return "success";
    }
}

4.结束

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值