0

我正在使用基于 xml 的 spring 配置。我在 spring 文档中发现默认情况下启用了某些 bean。这是什么意思?以及如何解决该 bean 以更改其属性之一。我明白,如果我定义一个 bean,我可以在它的定义中配置它。但是现有的“默认启用”bean呢?

编辑:来自 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver javadoc:

@Deprecated public class AnnotationMethodHandlerExceptionResolver
extends AbstractHandlerExceptionResolver Implementation of the
org.springframework.web.servlet.HandlerExceptionResolver interface
that handles exceptions through the ExceptionHandler annotation. This
exception resolver is enabled by default in the
org.springframework.web.servlet.DispatcherServlet.
4

1 回答 1

1

这意味着如果您没有指定任何其他bean,只需DispatcherServlet在应用程序配置中指定一个 servlet 就会注册一个bean 。AnnotationMethodHandlerExceptionResolverHandlerExceptionResolver

当您运行 Spring MVC webapp 时,您会注意到DispatcherServlet.properties类路径中有一个文件。此属性文件除其他外定义此属性

org.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver,\
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,\
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver

它描述了HandlerExceptionResolverDispatcherServlet's List<HandlerExceptionResolver>. 这是在DispatcherServlet#getDefaultStrategies()方法中完成的。

请注意,仅当您用于初始化的上下文DispatcherServlet未声明任何HandlerExceptionResolverbean 时才会发生这种情况。典型的应用程序将使用元素或注释注册不同HandlerExceptionResolver的 bean 。这也是该类被弃用的原因。<mvc:annotation-driven>@EnableWebMvc

于 2013-11-12T15:43:12.630 回答