• MVEL自定义函数重复掉用报错:duplicate function


    MVEL自定义表达式重复掉用报错:duplicate function

    原因:第一次调用时会将function缓存,再次调用时提示函数重复

    方案:先将funciton放入缓存,再次调用时直接调用声明缓存中的函数

    • 自定义函数名,不要与参数名相同

    示例代码:

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class MvelTest {
            
        @Test
        public void evalTest(){
            //表达式
            String roundup = "def roundup(num) { roundnum = null; if(num != null){ int roundnum = Math.round(num); } return roundnum; }";
            VariableResolverFactory functionFactory = new MapVariableResolverFactory();
            MVEL.eval(roundup, functionFactory);
    
            VariableResolverFactory resolverFactory = new MapVariableResolverFactory();
            resolverFactory.setNextFactory(functionFactory);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("A", new BigDecimal(2.33));
            map.put("B", new BigDecimal(4));
            String exp = "roundup(A*B)";
            System.out.println(MVEL.eval(exp, map, resolverFactory));
    
            System.out.println("------------");
    
            VariableResolverFactory resolverFactory2 = new MapVariableResolverFactory();
            resolverFactory2.setNextFactory(functionFactory);
            Map<String, Object> map2 = new HashMap<String, Object>();
            map2.put("A", new BigDecimal(3.33));
            map2.put("B", new BigDecimal(2));
            System.out.println(MVEL.eval(exp, map2, resolverFactory2));
        }
    }
    

    错误代码示例

    向缓存中加入roundup函数

    第一次执行成功

    再次调用再次向缓存中加入roundup函数

    VariableResolverFactory中存在函数抛出异常

  • 相关阅读:
    antd4.x Form组建改变
    react hook 使用注意点
    Dockerfile怎么编写
    在spring boot中3分钟上手阿里巴巴服务熔断系统sentinel
    容器和镜像的导入导出及部署
    设计模式之 ==> 装饰器设计模式
    Jenkins + Gradle + Docker 自动化部署 SpringBoot 项目到远程服务器
    Linux运维常用的40个命令总结
    ceph集群部署
    tcpdump常用命令
  • 原文地址:https://www.cnblogs.com/PoetryAndYou/p/15747970.html
Copyright © 2020-2023  润新知