Spring 3 有缺陷吗?

发布于 2024-12-05 13:28:33 字数 2305 浏览 0 评论 0 原文

我有一个 Spring 2.5.x 应用程序,我正在将其迁移到 Spring 3,但遇到了一个小问题。

我有一个像这样的处理程序映射:

<bean id="handlerMappings1" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="interceptors">
        <list>
            <ref bean="interceptor1" />
            <ref bean="interceptor2" />
            ....
            <ref bean="interceptorN" />
        </list>
    </property>
    <property name="urlMap">
        <map>
            <entry key="/url1.html" value-ref="controller1" />
            <entry key="/url2.html" value-ref="controller2" />
            ....
            <entry key="/url100.html" value-ref="controller100" />
        </map>
    </property>
</bean>

还有另一个像这样的处理程序映射:

   <bean id="handlerMappings2" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
            <map>
                <entry key="/urlA.html" value-ref="controllerA" />
                <entry key="/urlB.html" value-ref="controllerB" />
                ....
                <entry key="/urlN.html" value-ref="controllerN" />
            </map>
        </property>
    </bean>

我正在慢慢地将两个 @RequestMapping 注释替换为 (其中基本上注册一个 DefaultAnnotationHandlerMapping)。

在 Spring 3 中,我看到了 < ;mvc:interceptors> 标签可用于向某些 URL 添加拦截器,但您只能指定一个拦截器,至少我是这样看到的 来自架构

据我所知,我必须为每个拦截器注册其中一个,这将重复我的所有 URL 多次,因为我有拦截器(而且我什至不知道它们将按什么顺序运行)。

另一方面,我无法在 DefaultAnnotationHandlerMapping 上添加迭代器,因为它们将为所有用 @RequestMapping 注解的控制器运行,而我不希望这样。

那么如何在 Spring 3 中为某些 URL 指定拦截器,而不重复 URL 和 基于 @RequestMapping 注释保留 URL 到控制器映射?

I have a Spring 2.5.x application which I'm migrating to Spring 3 and just bumped into a little problem.

I have an handler mapping like so:

<bean id="handlerMappings1" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="interceptors">
        <list>
            <ref bean="interceptor1" />
            <ref bean="interceptor2" />
            ....
            <ref bean="interceptorN" />
        </list>
    </property>
    <property name="urlMap">
        <map>
            <entry key="/url1.html" value-ref="controller1" />
            <entry key="/url2.html" value-ref="controller2" />
            ....
            <entry key="/url100.html" value-ref="controller100" />
        </map>
    </property>
</bean>

and another one like this:

   <bean id="handlerMappings2" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
            <map>
                <entry key="/urlA.html" value-ref="controllerA" />
                <entry key="/urlB.html" value-ref="controllerB" />
                ....
                <entry key="/urlN.html" value-ref="controllerN" />
            </map>
        </property>
    </bean>

I'm slowly replacing both with @RequestMapping annotations with a <context:component-scan> (which basically registers a DefaultAnnotationHandlerMapping).

In Spring 3 I saw the <mvc:interceptors> tag which can be used to add interceptors to certain URLs but you can specify only one interceptor, at least that's what I see from the schema.

From what I can figure, I have to register one of these for each interceptor which will duplicate all my URLs for as many times as I have interceptors (and I don't even know in what order they will run).

On the other hand I can't add the iterceptors on the DefaultAnnotationHandlerMapping because they will run for all my controllers annotated with @RequestMapping and I don't want that.

So how can I specify interceptors is Spring 3 for some URLs, without repeating the URL's and
keeping the URL to controller mapping based on the @RequestMapping annotation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喵星人汪星人 2024-12-12 13:28:33

您可以查看 springplugins 项目。消息来源已经有几年了,但这个想法仍然存在。

创建者的博客上有一个演示:Spring Framework 基于注解的控制器拦截器配置。请确保您还阅读了该博文的评论。

You could have a look at the SelectedAnnotationHandlerMapping and the IgnoreSelectedAnnotationHandlerMapping classes from the springplugins project. The sources are some years old but the idea still stands.

There is a presentation on the creator's blog here: Spring Framework Annotation-based Controller Interceptor Configuration. Make sure you also read the comments to the blog post.

晒暮凉 2024-12-12 13:28:33

一种选择是创建一个自定义拦截器,它可以委托给一组注入的拦截器。

One option would be to create a custom interceptor which can delegate to a collection of injected interceptors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文