Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I specify a bean reference as an argument-resolver, in Spring 3-1?

This is a syntax question about a Spring configuration (refer to spring-beans-3.1.xsd and spring-mvc-3.1.xsd).

I have a specific bean definition (id="SecurityRequestParametersArgumentResolver") that I want to register as a custom argument resolver. The xml fragment is:

<bean id="SecurityRequestParametersArgumentResolver"
    class="xxx.security.web.SecurityRequestParametersArgumentResolver">
    <property name="credentialsManager" ref="CredentialsManager" />
    <property name="tokenService" ref="TokenService" />
</bean>

... AND I would like to use a bean reference.  The following three lines don't obey the xsd-grammar ( what should be the correct tag declaration here? )
<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean ref="SecurityRequestParametersArgumentResolver"/>
    </mvc:argument-resolvers>
</mvc:annotation-driven>

... All of the examples I have seen look like THIS, and thus are going after the default no-argument constructor

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean class="class="xxx.security.web.SecurityRequestParametersArgumentResolver"/>
    </mvc:argument-resolvers>
</mvc:annotation-driven>

Repeating the question, what should the Spring syntax be in order to use a bean reference as a custom argument resolver?

Thanks!

like image 218
Mark Avatar asked Dec 13 '12 21:12

Mark


1 Answers

At least the constructor argument problem can be solved easily

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean class="class="xxx.security.web.SecurityRequestParametersArgumentResolver">
           <constructor-arg value="123"/>
        </bean>
    </mvc:argument-resolvers>
</mvc:annotation-driven>

But I do not know any way to use a reference instead of a bean within <mvc:argument-resolvers>. But depending on what you want to do, there are several workarrounds.

  • Use a Bean Factory
  • Create a proxy class that implements the HandlerMethodArgumentResolver and forward every call to an other instance, then you can have your own SecurityRequestParametersArgumentResolver like a normal bean and register the Proxy in the <mvc:argument-resolvers> section.

Proxy:

class HandlerMethodArgumentResolverProxy
       implements HandlerMethodArgumentResolver{
    HandlerMethodArgumentResolver delegate;

    publicHandlerMethodArgumentResolverProxy
                   (HandlerMethodArgumentResolver delegate){
        this.delegate=delegate;
    }

    public boolean supportsParameter(MethodParameter parameter) {
        this.delegate.supportsParameter(parameter);
    }

    //delegate for resolveArgument        
}

config:

<bean id="securityRequestParametersArgumentResolver"
    class="xxx.security.web.SecurityRequestParametersArgumentResolver">
    ...
</bean>

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean class "HandlerMethodArgumentResolverProxy">
             <constructor-arg 
                 ref="securityRequestParametersArgumentResolver"/>
        </bean>
    </mvc:argument-resolvers>
</mvc:annotation-driven>  
like image 138
Ralph Avatar answered Sep 23 '22 06:09

Ralph