您的位置:首页 > 房产 > 家装 > 安阳区号12345_中国人民解放军战略支援部队信息工程大学_凡客建站_四川省人民政府官网

安阳区号12345_中国人民解放军战略支援部队信息工程大学_凡客建站_四川省人民政府官网

2025/2/26 2:25:50 来源:https://blog.csdn.net/qq_45923849/article/details/145838915  浏览:    关键词:安阳区号12345_中国人民解放军战略支援部队信息工程大学_凡客建站_四川省人民政府官网
安阳区号12345_中国人民解放军战略支援部队信息工程大学_凡客建站_四川省人民政府官网

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();// 访问urlString 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);// 请求IPSystem.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.结束

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com