我尝试学习SpEL,但是我的脚本之一不起作用。
我的脚本是:

changePages(#{T(StoryContent).pages}, (x, y) -> x.replaceAll(y, x.substring(0, 1) + '...' + x.substring(x.length() - 1, x.length())))


Java代码:

     public void validate(Story story, List<StoryRule> rules) throws NoSuchMethodException, SecurityException {
            StoryContent storyContent = storyContentRepository.findOne(story.getContentId());
            story.setStatus(SchedulerStatus.VALIDATION);
            storyRepository.save(story);
            ExpressionParser parser = new SpelExpressionParser();
            StandardEvaluationContext inventorContext = new StandardEvaluationContext(storyContent);
            inventorContext.registerFunction("changePages", ValidationByRuleServiceImpl.class
                    .getDeclaredMethod("changePages", new Class[] { List.class, BiFunction.class }));
            try {
                rules.stream().map(StoryRule::getScript)
                        .forEach(script -> parser.parseExpression(script).getValue(inventorContext));
                if (!storyContent.getTitle().equals(story.getName()))
                    story.setName(storyContent.getTitle());
                story.setStatus(SchedulerStatus.PUBLISHED);
                storyContentRepository.save(storyContent);
            } catch (Exception e) {
                LOG.error(e.getMessage());
                story.setStatus(SchedulerStatus.ERROR);
            } finally {
                storyRepository.save(story);
            }
    }
    private void changePages(List<String> pages, BiFunction<String, String, String> function) {
        pages.forEach(System.out::println);
    }


错误是:

Expression [changePages(T(StoryContent).pages, (x, y) -> x.replaceAll(y, x.substring(0, 1) + '...' + x.substring(x.length() - 1, x.length())))] @37: EL1043E: Unexpected token. Expected 'rparen())' but was 'comma(,)'


我从数据库获得SpEL脚本列表。

我不知道这是什么错误,我的脚本还是我的Java代码?

最佳答案

SpEL不是Java,而是另一种语言。该首字母缩写词代表Spring Expression Language。

它不了解Java8 lambda,因此无法解析(x, y) -> ...

但是,您可以将lambda注册为SpEL函数,请参见my answer here

10-06 14:01