@SessionAttributes stores the session object specified by the following attributes in the Model for each method of the annotated Controller.
The process is implemented in SessionAttributesHandler#retrieveAttributes(WebRequest), but sometimes it is not reflected in the Model because it only looks at knownAttributeNames.
The object specified in types is not initially defined in knownAttributeNames, but is added to knownAttributeNames when DI by a controller method.
Instances of SessionAttributesHandler are cached, so information added to knownAttributeNames is preserved, but it is only valid within the same VM since it is not stored in the session.
Therefore, if different methods of the same Controller are executed on different servers, the expected behavior will not occur because the knownAttributeNames state will be different.
e.g.) If handle1 and handle2 below are executed on different servers, Pet is not stored in handle2's model.
@Controller
@SessionAttributes(types = { Pet.class })
public class EditPetForm {
// ...
@PostMapping("...")
public String handle1(Pet pet, BindingResult errors) {
}
@PostMapping("...")
public String handle2(Model model) {
}
}
Defining the above in names instead of types will result in the expected behavior on different servers.
I do not think this behavior is what is expected.
Note that this problem does not occur when SessionAttributesHandler is the same on all servers.
The problem is easily missed because it occurs immediately after a server restart when the SessionAttributesHandler cache is different.
@SessionAttributes stores the session object specified by the following attributes in the Model for each method of the annotated Controller.
The process is implemented in SessionAttributesHandler#retrieveAttributes(WebRequest), but sometimes it is not reflected in the Model because it only looks at
knownAttributeNames.The object specified in
typesis not initially defined inknownAttributeNames, but is added toknownAttributeNameswhen DI by a controller method.Instances of
SessionAttributesHandlerare cached, so information added toknownAttributeNamesis preserved, but it is only valid within the same VM since it is not stored in the session.Therefore, if different methods of the same Controller are executed on different servers, the expected behavior will not occur because the
knownAttributeNamesstate will be different.e.g.) If handle1 and handle2 below are executed on different servers, Pet is not stored in handle2's model.
Defining the above in
namesinstead oftypeswill result in the expected behavior on different servers.I do not think this behavior is what is expected.
Note that this problem does not occur when
SessionAttributesHandleris the same on all servers.The problem is easily missed because it occurs immediately after a server restart when the
SessionAttributesHandlercache is different.