【问题标题】:use function to provide values for spring spel使用函数为弹簧拼写提供值
【发布时间】:2021-03-10 17:06:32
【问题描述】:

我使用的spring评估语言如下:

Map<String, Object> bigMap = loader.loadBigMap();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariables(bigMap);

我的问题是 bigMap 中包含的大部分数据都没有使用。 相反,我宁愿通过将函数传递给评估上下文来延迟加载我需要的内容,例如:

Function<String, Object> lazyloader = name -> loader.loadForName(name);
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariables(lazyloader);

但我找不到如何做到这一点。有什么建议吗?

【问题讨论】:

    标签: spring spring-el


    【解决方案1】:

    你不能用 lambda 来做 - SpEL 函数必须是静态方法。

    public void registerFunction(String name, Method method)
    
    public class MyUtils {
    
        public static Object lazyLoader(String key) {
            ...
        }
    
    }
    

    使用Class.getDeclaredMethod(...) 获取对该方法的引用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-17
      • 2021-12-27
      • 2019-09-19
      • 2012-08-12
      • 2019-05-03
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      相关资源
      最近更新 更多