Announcement Announcement Module
No announcement yet.
Customizing @ModelAttribute annotated method invocation order Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Customizing @ModelAttribute annotated method invocation order

    Hey all,

    I am structuring my controllers with a few @RequestMapping's and a number of @ModelAttribute annotated methods, which in some cases rely upon each other. The problem I have is that the @ModelAttribute method invocation order is pretty much undefined, and relies on reflection only (i.e., I can't use Ordered to sort them to my liking). So, if I have something like this:

        public String xxx() {
            return "xxx";
        public String yyy(@ModelAttribute("xxx") String xxx) {
            return xxx+"yyy";
        public String render(@ModelAttribute("xxx") String xxx) {
            return "rendered: " + xxx;
    my xxx() method ***may*** never get called.

    I am using @EnableWebMvc java configuration, so all the magic of param binding happens in RequestMappingHandlerAdapter, which creates a ModelFactory, which in turn picks out @ModelAttribute annotated methods. Now, I would gladly override the behavior by sorting the ModelAttribute methods depending on relation on each other's arguments, which would be as simple as sorting ModelFactory.attributeMethods list, but I find it impossible because all the methods are either final, private, or protected final (?), and I can't provide a subclass of ModelFactory, since it's hardcoded (and I'm reluctant to use aspectj to override this type of functionality).
    The current implementation provides very limited configuration options, which comes as a surprise to me, since I've always considered spring to be the cleanest architecture around.

    Could anyone point me out to the right direction here, because I don't see any options other than a) copy pasting most of the RequestMappingHandlerAdapter code and customizing it in a private implementation (which would mean additional maintenance-related nightmares), b) using aspectj to override this functionality (not the cleanest option either).

    I would be thankful for any feedback on this topic, and would gladly contribute a patch to resolve this issue.

    Andrius Juozapaitis