代码之家  ›  专栏  ›  技术社区  ›  Amir Pashazadeh

SpEL中SimpleEvaluationContext.forReadOnlyDataBinding()的混乱行为

  •  1
  • Amir Pashazadeh  · 技术社区  · 6 年前

    我正在阅读Spring5.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表达式不应该是只读的,并且不更改属性值吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Gary Russell    6 年前

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

    i、 e.不允许更换 booleanList 但是没有什么可以阻止现有数组的内容发生变化。