Search code examples
bean-io

Does BeanIO require I provide a setter


It appears BeanIO requires I provide setters for my fields. Is this necessary? I created a BeanIO writer, I intend only to write files, and I have getters for each field - why would I need setters?


Solution

  • If your beans are only meant to be serialized (i.e. used by a BeanWriter), you can declare your stream as mode="write". This will cause BeanIO to look for getters but not setters.

    The reference guide states that:

    By default, a stream mapping can be used for both reading input streams and writing output streams, called readwrite mode. Setting mode to read orwrite instead, respectively restricts usage to a BeanReader or a BeanWriter only, but relaxes some validations on the mapping configuration.

    When mode is set to read, a bean class does not require getter methods.

    When mode is set to write, a bean class may be abstract or an interface, and does not require setter methods.