Spring 3 is great at automatically resolving standard arguments into a controller request method. For example, a primitive Spring controller might look like this ...
In this case, Spring knows the HttpServletRequest argument represents the incoming Servlet request, and the Principal argument is the object representing the authenticated user (in the event that you're using Spring Security to manage authentication in your web-application). On method invocation, Spring automatically resolves these arguments for you. Neat!
However, the repetition becomes obvious where in every controller, you need to fetch the same MyObject from the session, over and over again. Instead of repeating that line of code in every method of every controller that needs access to MyObject, what if you could tell Spring how to resolve MyObject automatically on invocation?
@Controller
@RequestMapping(value="/somepath")
public class MyController {
@RequestMapping(method={RequestMethod.GET, RequestMethod.HEAD})
public ModelAndView someMethod(final HttpServletRequest request,
final Principal principal) {
// Extract some special object needed to process the request from
// the session -- this object is bound to the session elsewhere on
// a successful authentication.
final MyObject obj = (MyObject)request.getSession().getAttribute("myobjkey");
// Do actual work.
/* ... */
return new ModelAndView("someview");
}
}
In this case, Spring knows the HttpServletRequest argument represents the incoming Servlet request, and the Principal argument is the object representing the authenticated user (in the event that you're using Spring Security to manage authentication in your web-application). On method invocation, Spring automatically resolves these arguments for you. Neat!
However, the repetition becomes obvious where in every controller, you need to fetch the same MyObject from the session, over and over again. Instead of repeating that line of code in every method of every controller that needs access to MyObject, what if you could tell Spring how to resolve MyObject automatically on invocation?

