声明一个注解
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Order(Ordered.HIGHEST_PRECEDENCE)
public @interface RequestLimit {
}
配置一个切面
@Aspect
@Component
public class RequestLimitAspect {
@Resource
private JdCloudRedisManager jdRedisManager;
@Resource
private JwStoreConfigDao jwStoreConfigDao;
@Before(value="execution (* com.jw.store.controller.JwStoreController.*(..)) && @annotation(limit)")
public void limitRequest(JoinPoint joinPoint, RequestLimit limit) throws Exception{
if(getSwitch()) {
Object[] args = joinPoint.getArgs();
if(args==null || args.length == 0) {
return;
}
HttpServletRequest request = null;
for(int i=0; i<args.length; i++) {
if(args[i] instanceof HttpServletRequest) {
request = (HttpServletRequest) args[i];
break;
}
}
if (request == null) {
return;
}
String clientIp = IpUtil.getIpAddr(request);
String method_name =joinPoint.getSignature().getName();
if (method_name != null && !method_name.equals("")) {
executeLimitMethod(method_name, clientIp);
}
}
}
配置aop动态代理
<!-- aop动态代理 -->
<aop:aspectj-autoproxy proxy-target-class="false"/>
但死活没有运行aop, 通过各种查找,原来是动态代理配置需要配置到spring mvc xml文件中
原文地址:https://www.cnblogs.com/chenge-0401/p/8617525.html