Why: An immutable class sets its values once, usually through its constructor. After that, the values cannot be changed.
2. From which Spring version can data binding work with immutable classes?
Why: Starting with Spring 5, data binding works with immutable classes. Before Spring 5, this was not supported.
3. Before Spring 5, what did a bean class need so that request parameters could be bound to it?
Why: Older Spring versions created the object with a no-argument constructor. Then they set each value using setter methods.
4. What happened in versions before Spring 5 when request parameters were bound to a class that had no no-argument constructor?
Why: Older versions could not create the object without a default constructor. They failed with a BeanInstantiationException.
5. How does Spring 5 detect a data class that it can bind using a constructor?
Why: Spring automatically detects data classes that have a single public constructor. It then uses that constructor to create the object.
6. How does Spring decide which request parameter goes to which constructor argument?
Why: Spring resolves each constructor argument by looking for a request parameter with a matching name.
7. What does Spring need in order to know the names of the constructor parameters?
Why: Spring can match request parameters only if it knows the constructor parameter names. They can be kept by the compiler, or given using @ConstructorProperties.
8. Which Java 8+ compiler flag keeps constructor parameter names, so Spring can match them with request parameter names?
Why: With the -parameters compiler flag, parameter names are kept in the compiled class. Spring can then read them at runtime.
9. What is the purpose of the @ConstructorProperties annotation?
Why:@ConstructorProperties is a Java annotation. It tells which property each constructor parameter belongs to, using names.
10. Where do we place the @ConstructorProperties annotation?
Why:@ConstructorProperties is meant to be used on a constructor.
11. When can @ConstructorProperties be left out?
Why: With the -parameters compiler flag, Spring can read the real constructor parameter names. So the annotation is not needed if those names match the request parameter names.
12. A constructor is declared as shown below. Which request parameter names will Spring use for binding?
@ConstructorProperties({"code", "city"})
public Address(String a, String b) { ... }
Why: The names given in @ConstructorProperties are used as request parameter names. They are matched to the constructor parameters in the same order.
13. How are request parameter values given to an immutable bean?
Why: An immutable bean has no setters. Spring passes the request parameter values to its constructor when it creates the object.