Monday, May 22, 2017

Spring MVC's ArgumentResolver

Recently a colleague introduced to me the concept of an ArgumentResolver.

In theory, with an ArgumentResolver, one can inject anything into a Spring MVC Controller. They are usually injected as arguments (aka parameters) in the Controller's methods.

So first in the XML configuration we do the below:

            <bean class="com.mycompany.ArgumentResolver"></bean>

Then we implement the class itself:

import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;

public class ArgumentResolver implements HandlerMethodArgumentResolver {
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterType().equals(MyClass.class);

    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

        // do what you need to return the Argument

No comments:

Post a Comment