What practice is right to store attribute names?

Viewed 37

I'm writing a web application using Spring and I wonder how should I store names of attributes that are sent back with the request:

  1. Hardcoding names in controllers code (probably the worst one, but still a variant)
model.addAttribute("orders", orders.orElse(new ArrayList<>()));
  1. Storing names in some class as a constant
model.addAttribute(Attributes.Orders, orders.orElse(new ArrayList<>()));
  1. Storing names in the properties file, that are going to be collected by a class, annotated with @ConfigurationProperties and injected into the controller (I don't think I'll need to change the values, so maybe this variant is a little bit excessive?)
@Component
@ConfigurationProperties(prefix = "attributes")
@Data
public class ApplicationAttributes {
    private String orders;
}
model.addAttribute(appAttributes.getOrders(), orders.orElse(new ArrayList<>()));

What is the right way to do it? Looking forward to your reply! :)

0 Answers
Related