SpringBoot SpEL支持方法参数解析

想在IDEA中支持方法提示, 需要下载SpEL Assistant插件, 用法在这里

这里仅说AOP时支持方法参数

DemoAop.java:

java 复制代码
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DemoAop {
    /**
     * 支持SpEL表达式
     */
    String value();
}

MyXXXAspect.java:

java 复制代码
@EnableAspectJAutoProxy
@Aspect
@Slf4j
public class CustomMetadataAspect implements BeanFactoryAware {

    private final ExpressionParser parser = new SpelExpressionParser();
    private final ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
    @Setter
    private BeanFactory beanFactory;

    @Before("@annotation(demoAop)")
    public void atBefore(JoinPoint joinPoint, DemoAop demoAop) {
		String result = demoAop.value();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method targetMethod = signature.getMethod();
        Object[] args = joinPoint.getArgs();
        // noinspection DataFlowIssue
        MethodBasedEvaluationContext context =
            new MethodBasedEvaluationContext(null, targetMethod, args, pnd);
        context.setBeanResolver(new BeanFactoryResolver(beanFactory));
        // 可添加额外加参数, 如after切点添加result返回值: context.setVariable("额外参数", 参数值);
        result = parser.parseExpression(result).getValue(context, String.class);
        log.info("result is: {}", result);
    }

其实需要的代码行数特别少, 不需要花里胡哨的写法;

另外我一般会写上rootObject(MethodBasedEvaluationContext的第一个参数), 方便调用一些内容, 比如SpringSecurity中的@PreAuthorize中的hasRole()/hasAnyRole()/hasPermission()等等方法, 都是通过rootObject来实现的

示例我的一个RootObject:

java 复制代码
@Data
@AllArgsConstructor
@SuppressWarnings({"unused"})
public class RobinMetadataRootObject {
    private final Method method;

    private final Object[] args;

    private final Object target;

    private final Class<?> targetClass;

    public String getMethodName() {
        return this.method.getName();
    }
}
java 复制代码
// 解析时
public void atBefore(JoinPoint joinPoint, DemoAop demoAop) {
    ...
    // 初始化
    RobinMetadataRootObject rootObject = new RobinMetadataRootObject(targetMethod, extractArgs(targetMethod, args),
            joinPoint.getTarget(), joinPoint.getTarget().getClass());
    // 赋值
    MethodBasedEvaluationContext context =
            new MethodBasedEvaluationContext(rootObject, targetMethod, args, parameterNameDiscoverer);
    ...
}

可以方便的取方法和函数名称

Spring中的一些其他rootObject, 可供参考

Cache: CacheExpressionRootObject

Security: SecurityExpressionRoot

一些常见SpEL用法, 这里仅做示例, rootObject以, 不做详细解释:

java 复制代码
public String test(String id, Integer[] idList) {
    return id;
}
  • "#id" -- 直接取值
  • "1 > 2 && 3 == 4 && 5 > 6"关系运算符, 支持> / < / >= / <= / !=, 其中null小于任何值
  • 运算符支持纯字母等效替代: lt ('<')、gt ('>')、le ('<=')、ge ('>=')、eq ('==')、ne ('!= ')、div ('/')、mod ('%')、not ('!') 不区分大小写
  • "#id.startsWith('a')" -- 调用一些方法
  • "#id?.startsWith('b')" -- 空安全
  • "getMethodName() + getMethod() + getArgs()[0] + #args[0]" -- root上的字段/方法
  • "#p0+#a0" -- 以pa前缀 + 参数序号来取值
  • "@taskExecutor.activeCount" -- 使用@符号 + bean名称调用其他bean
  • "1 + 2 - 3 * 4 / 5 - -6 % 7" -- 加减乘除, 正负数, 取余
  • "'abc' + true + null + 123" -- 各种字面量
  • "{1, 2, 3, 4, 5}.?[ #this > 3 ]" -- 内联列表以及过滤(返回值为[4,5])
  • "{1, 2, 3, 4, 5}.^[ #this > 1 ]" -- 查找第一个符合条件的值(结果为2)
  • "{1, 2, 3, 4, 5}.&[ #this > 1 ]" -- 查找最后一个符合条件的值(结果为5)
  • "#username ?: 'abcd'" -- 默认值 如果username为 null, 则赋值为"abcd"
  • "new String(234)" -- 创建对象
  • "true ? 1 : 2" -- 三元表达式
相关推荐
Lxinccode2 小时前
Java查询数据库表信息导出Word-获取数据库实现[1]:KingbaseES
java·数据库·word·获取数据库信息·获取kingbasees信息
元亓亓亓2 小时前
Java后端开发day36--源码解析:HashMap
java·开发语言·数据结构
sd21315122 小时前
RabbitMQ 复习总结
java·rabbitmq
他҈姓҈林҈3 小时前
使用 Spring Boot 进行开发
spring boot
码银5 小时前
Java 集合:泛型、Set 集合及其实现类详解
java·开发语言
东阳马生架构5 小时前
Nacos简介—4.Nacos架构和原理
java
Java&Develop5 小时前
onloyoffice历史版本功能实现,版本恢复功能,编辑器功能实现 springboot+vue2
前端·spring boot·编辑器
一只叫煤球的猫6 小时前
你真的会用 return 吗?—— 11个值得借鉴的 return 写法
java·后端·代码规范
颇有几分姿色6 小时前
Spring Boot 读取配置文件的几种方式
java·spring boot·后端
爱编程的鱼6 小时前
C# 枚举(Enum)声明与使用详解
java·windows·c#