在Spring 3-1中,如何将bean引用指定为参数解析器?

时间:2012-12-13 21:28:40

标签: spring spring-mvc

这是关于Spring配置的语法问题(请参阅spring-beans-3.1.xsd和spring-mvc-3.1.xsd)。

我有一个特定的bean定义(id =“SecurityRequestParametersArgumentResolver”),我想将其注册为自定义参数解析器。 xml片段是:

<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>

...我看过的所有例子看起来都像这样,因此是在默认的无参数构造函数之后

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

重复这个问题,为了将bean引用用作自定义参数解析器,Spring语法应该是什么?

谢谢!

1 个答案:

答案 0 :(得分:1)

至少可以轻松解决构造函数参数问题

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

但是我不知道在<mvc:argument-resolvers>中使用引用而不是bean的方法。但是根据你想要做的事情,有几个工作场所。

  • 使用Bean Factory
  • 创建一个实现HandlerMethodArgumentResolver的代理类,并将每次调用转发给另一个实例,然后就像普通bean一样拥有自己的SecurityRequestParametersArgumentResolver并在<mvc:argument-resolvers>中注册代理部分。

代理:

class HandlerMethodArgumentResolverProxy
       implements HandlerMethodArgumentResolver{
    HandlerMethodArgumentResolver delegate;

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

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

    //delegate for resolveArgument        
}

配置:

<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>