spring mvc 使用注意事项

来源:互联网 发布:知荣制衣 联系方式 编辑:程序博客网 时间:2024/04/30 04:44

配置

context-param & ContextLoaderListener

全局参数,优先加载
- 当controller Autowired dao/service,不会出现问题
- 如果不配置,spring的ContextLoader会到 /WEB-INF/下找applicationContext.xml

xx-servlet & contextConfigLocation

加载 servlet 使用
- 使用 mvc:annotation-driven 它会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter这两个bean,如果想添加拦截器,则需要手动配置这2个bean。

配置示例:

xx-servlet .xml

<context:component-scan base-package="xx.controller" /><mvc:annotation-driven/>其他,例如消息转换器,视图解析器

context.xml

<context:component-scan base-package="com.suning.aimp.*">    <!-- 在dispatcherServlet-servlet.xml中扫描controller,此处不扫描,否则导致controller创建2次 -->    <context:exclude-filter type="annotation"    expression="org.springframework.stereotype.Controller" /></context:component-scan>