【问题标题】:Confusing behavior of SimpleEvaluationContext.forReadOnlyDataBinding() in SpELSpEL 中 SimpleEvaluationContext.forReadOnlyDataBinding() 的令人困惑的行为
【发布时间】:2018-12-08 01:24:13
【问题描述】:

我正在阅读 Spring 5.1.3 参考文档,SpEL Type Conversion 得到以下示例代码:

class Simple {
    public List<Boolean> booleanList = new ArrayList<Boolean>();
}

Simple simple = new Simple();
simple.booleanList.add(true);

EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();

// false is passed in here as a string. SpEL and the conversion service
// correctly recognize that it needs to be a Boolean and convert it
parser.parseExpression("booleanList[0]").setValue(context, simple, "false");

// b is false
Boolean b = simple.booleanList.get(0);

它像提到的文档一样工作,并改变属性的值,但根据 Javadocs forReadOnlyDataBinding()

创建一个 {@code SimpleEvaluationContext} 以通过 {@link DataBindingPropertyAccessor} 对公共属性进行只读访问。

SpEL表达式不应该是只读的,不改变属性值吗?

【问题讨论】:

    标签: spring-el


    【解决方案1】:

    字段的内容是可变的,但字段本身是不可变的。

    即不允许将booleanList 替换为新数组,但没有什么可以防止现有数组的内容发生变异。

    【讨论】:

      猜你喜欢
      • 2011-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多