开发者社区 问答 正文

springmvc为什么还配置spring

在配置时发现,其实只要配置SrpingMVC的部分就可以完成所需要的功能,而网上或书中,都还配置了Spring这一部分。

如下,其实只要spring-servlet配置好了就可以了,上面的<listener>可以不写。。。也可能是我只是单纯练习的原因,有些地方没用到。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- spring的启动配置 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <!-- spring的启动配置结束 -->

    <!-- springMvc的配置 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    <!-- springMvc的配置结束 -->

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

其中,spring-servlet.xml和applicationContext.xml的内容其实是差不多的,spring-servlet.xml比applicationContext.xml多些内容。
因此我认为applicationContext.xml中的内容其实可以配置到spring-servlet.xml中去,只要多分几个xml配置文件不

展开
收起
a123456678 2016-03-16 11:40:37 2584 分享 版权
2 条回答
写回答
取消 提交回答
  • 你MVC的controller说白了也是bean还是要依赖 spring管理的

    2019-07-17 19:03:45
    赞同 展开评论
  • 那个监听器最大的作用就是把applicationContext放到application作用域,便于获取,如果所有功能都靠注入不需要手工获取context的话,也不一定非要用。另外一个特殊的作用就是他产生的context是dispatchServlet的上下文有父子关系,如果应用大到有多个dispatchServlet的时候,公用部分就应该提到监听器这边来定义

    2019-07-17 19:03:45
    赞同 展开评论