将@ModelAttribute解析为方法参数的切入点表达式 [英] pointcut expression to parse @ModelAttribute as method parameter

查看:57
本文介绍了将@ModelAttribute解析为方法参数的切入点表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个@ModelAttribute(键)在@CONTRONTERADVICE中定义,并且我在多个控制器方法中使用相同的模型属性作为方法参数,因为(键)将在所有控制器中都可用。

我正在控制器类中添加属性(键),如下所示。

@RequestMapping(value = "/", method = RequestMethod.GET)
public String list(final Model model,@ModelAttribute("key") final boolean key) { 
...
...
}

我要截取方法参数为@ModelAttribute("key")的所有控制器方法。

我的方面文件如下所示。

@Component
@Aspect
@EnableAspectJAutoProxy
public class myAspectclass {

@Pointcut("execution(public * *(.., @org.springframework.web.bind.annotation.ModelAttribute(boolean key)))")
          public void methodWithAnnotatedParameter() {}

@Around("methodWithAnnotatedParameter()")
public String blahMethod(ProceedingJoinPoint PJP){

blah blah

....
}

但是我的服务器启动失败,提示

[Tomcat:Launch]原因:java.lang.IlLegalArgumentException:PointCut格式不正确:应在字符位置97处使用‘名称模式’

[Tomcat:启动]执行(PUBLIC**(..,@org.springframework.web.bind.annotation.ModelAttribute(boolean Key),..)

我无法理解这种情况下的错误...我在语法上做错了什么吗?

注意:MyModelAttribute(key)在参数列表中没有任何具体位置

引用this答案:

推荐答案

这里是独立的AspectJ示例(不是Spring应用程序,但方面语法应该相同)。

驱动程序应用程序:

如您所见,有几个带有和不带有@ModelAttribute参数注释的方法,其中一个甚至带有两个带注释的参数(无论是否有意义,但这只是一个示例)。

package de.scrum_master.app;

import java.util.Collection;
import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;

public class Application {
  public String list(Model model, @ModelAttribute("key") boolean key) {
    return "x";
  }

  public String foo(Model model, @ModelAttribute("key") boolean key, @ModelAttribute(name = "key") String text) {
    return "x";
  }

  public String bar(@ModelAttribute(name = "key") int number) {
    return "x";
  }

  public String zot(Model model, @ModelAttribute("XXX") boolean key) {
    return "x";
  }

  public String baz(Model model, boolean key, String text) {
    return "x";
  }

  public String bla(@ModelAttribute("XXX") int number) {
    return "x";
  }

  public static void main(String[] args) {
    Model model = new Model() {
      @Override public Model mergeAttributes(Map<String, ?> arg0) { return null; }
      @Override public boolean containsAttribute(String arg0) { return false; }
      @Override public Map<String, Object> asMap() { return null; }
      @Override public Model addAttribute(String arg0, Object arg1) { return null; }
      @Override public Model addAttribute(Object arg0) { return null; }
      @Override public Model addAllAttributes(Map<String, ?> arg0) { return null; }
      @Override public Model addAllAttributes(Collection<?> arg0) { return null; }
    };
    Application application = new Application();
    application.list(model, true);
    application.foo(model, true, "hey");
    application.bar(11);
    application.zot(model, true);
    application.baz(model, true, "hey");
    application.bla(11);
  }
}

方面:

方面匹配具有至少一个带有@ModelAttribute注释的参数的所有方法执行。如果您只想查找这些方法,而不考虑注释参数值,那么切入点就足够了。但正如您所说,您只想匹配具有value = "key"的注释,我们需要使用反射来查看注释本身并过滤掉不需要的注释。

另一个复杂情况是,根据@ModelAttribute JavaDoc,参数namevalue是彼此的别名,即我们还需要检查这两个参数的值才能完全正确。

package de.scrum_master.aspect;

import java.lang.annotation.Annotation;

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.bind.annotation.ModelAttribute;

@Aspect
@Component
public class ModelAttributeInterceptor {
  @Pointcut("execution(public * *(.., @org.springframework.web.bind.annotation.ModelAttribute (*), ..))")
  public void methodWithAnnotatedParameter() {}

  @Around("methodWithAnnotatedParameter()")
  public String blahMethod(ProceedingJoinPoint thisJoinPoint) throws Throwable {
    MethodSignature methodSignature = (MethodSignature) thisJoinPoint.getSignature();
    Annotation[][] annotationMatrix = methodSignature.getMethod().getParameterAnnotations();
    boolean foundModelAttribute = false;
    for (Annotation[] annotations : annotationMatrix) {
      for (Annotation annotation : annotations) {
        if (!(annotation instanceof ModelAttribute))
          continue;
        ModelAttribute modelAttribute = (ModelAttribute) annotation;
        if ("key".equals(modelAttribute.value()) || "key".equals(modelAttribute.name())) {
          if (!foundModelAttribute) {
            System.out.println(thisJoinPoint);
            foundModelAttribute = true;
          }
          System.out.println("  " + modelAttribute);
        }
      }
    }
    return (String) thisJoinPoint.proceed();
  }
}

控制台日志:

execution(String de.scrum_master.app.Application.list(Model, boolean))
  @org.springframework.web.bind.annotation.ModelAttribute(name=, value=key, binding=true)
execution(String de.scrum_master.app.Application.foo(Model, boolean, String))
  @org.springframework.web.bind.annotation.ModelAttribute(name=, value=key, binding=true)
  @org.springframework.web.bind.annotation.ModelAttribute(name=key, value=, binding=true)
execution(String de.scrum_master.app.Application.bar(int))
  @org.springframework.web.bind.annotation.ModelAttribute(name=key, value=, binding=true)

这篇关于将@ModelAttribute解析为方法参数的切入点表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆