是否可以使用注释连接 Spring MVC 拦截器? [英] Is it possible to wire a Spring MVC Interceptor using annotations?
问题描述
是否可以使用注释连接 Spring MVC 拦截器,如果可以,有人可以向我提供如何操作的示例吗?
Is it possible to wire a Spring MVC Interceptor using annotations and if so could someone provide me with an example of how to do so?
通过注释连接,我指的是在 XML 配置中尽可能少做.例如,在这个配置文件中,我在 http://www.vaannila.com/spring/spring-interceptors.html;
By wire via annotation I am referring to doing as little in the XML configuration as possible. For example in this configuration file I found at http://www.vaannila.com/spring/spring-interceptors.html;
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:interceptors-ref="loggerInterceptor" />
<bean id="loggerInterceptor" class="com.vaannila.interceptor.LoggerInterceptor" />
你能做到多少配置?我想 @Autowired
将消除在第 2 行中显式声明 bean 的需要,但是否也可以用注释去掉第 1 行?
How little configuration could you get away with there? I imagine an @Autowired
would remove the need to explicitly declare the bean in line 2, but would it be possible to get rid of line 1 with an annotation as well?
推荐答案
据我所知,没有 XML 是没有办法配置 Spring MVC 拦截器的.
As far as I know, there are no ways to configure Spring MVC interceptors without XML at all.
但是,在最新版本的 Spring 3.0.x(不是 Spring 3.0.0!)中,mvc
命名空间有一些简化:
However, there are some simplifications with mvc
namespace in the latest versions of Spring 3.0.x (not Spring 3.0.0!):
<mvc:interceptors>
<bean class="com.vaannila.interceptor.LoggerInterceptor" />
</mvc:interceptors>
另见:
这篇关于是否可以使用注释连接 Spring MVC 拦截器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!