注解中的spel 表达式中取# 取方法参数,idea 是如何识别到的

上图是我自实现的注解,并且对注解的 value 参数添加了 idea 提供的 @Language(“SpEL”) 注解,所以它能识别到是spel 表达式。但是参数 #loginUser 以及 #opt 却无法识别到。


这个是SpringSecurity 提供的 注解,它能识别到spel 表达式,同时还能识别到方法参数,通过鼠标还能点击跳转。

如何才能做到像SpringSecurity 那样,让idea 识别到spel 表达式中的参数,并且点击能跳转到参数具体的位置。

IDEA中自定义java注解SpEL高丽跳转

安装插件spel assistant

项目src/main/resources创建spel-extension.json配置

method.parameters设置为true,feilds.roo设置对应参数

{
  "com.example.demo.ext.Auth@curr": {
    "prefix": "",
    "suffix": "",
    "method": {
      "result": false,
      "resultName": "result",
      "parameters": true,
      "parametersPrefix": [
        "p",
        "a"
      ]
    },
    "fields": {
      "root": "com.example.demo.domain.User"
    }
  }
}

自定义注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Auth {
    String curr();
}

使用示例

@RestController
public class TestController {

    @Auth(curr = "#user.username")
    public String test(@RequestBody User user) {
        return "ok";
    }
}

支持提示和跳转