【Spring】SpringMVC配置文件

SpringMVC中一般会引入三个配置文件applicationContext.xml、dispatcher-servlet.xml(SpringMVC-servlet.xml)、web.xml

1.dispatcher-servlet.xml(SpringMVC-servlet.xml)

DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,默认使用servlet context。加载包含Web组件的bean,如控制器、视图解析器以及处理器映射。

mvc:annotation-driven

当配置了mvc:annotation-driven/后,Spring就知道了我们启用注解驱动。然后Spring通过context:component-scan/标签的配置,会自动为我们将扫描到的@Component,@Controller,@Service,@Repository等注解标记的组件注册到工厂中,来处理我们的请求。配置如果没有

context:component-scan

    <!-- 扫描controller -->
    <context:component-scan base-package="**.*controller"/>
2.applicationContext.xml

使用application context,使用SpringMVC可以不配置applicationContext.xml,若使用需要在web.xml中添加Listener。加载应用中的其他bean,通常是驱动应用后端的中间层和数据层组件。

    <!-- 默认的spring配置文件是在WEB-INF下的applicationContext.xml -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:applicationContext*.xml
        </param-value>
    </context-param>
3.web.xml
    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <!-- 默认的spring配置文件是在WEB-INF下的applicationContext.xml -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:applicationContext*.xml
        </param-value>
    </context-param>

    <!-- 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>classpath*:springMVC-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

原文地址:https://www.cnblogs.com/grape1211/p/9575164.html

时间: 2024-08-29 13:31:39

【Spring】SpringMVC配置文件的相关文章

spring+springMVC+配置文件propertites

web.xml: <listener>         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>     </listener>  <context-param>         <param-name>contextConfigLocation</param-name>         &l

spring(一)--spring/springmvc/spring+hibernate(mybatis)配置文件

这篇文章用来总结一下spring,springmvc,spring+mybatis,spring+hibernate的配置文件 1.web.xml 要使用spring,必须在web.xml中定义分发器等信息,基本的配置信息如下: <?xml version="1.0" encoding= "UTF-8"?> <web-app version= "3.0" xmlns="http://java.sun.com/xml/n

Spring+SpringMVC+MyBatis深入学习及搭建(三)——MyBatis全局配置文件解析

转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6874672.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发 MyBatis的全局配置文件SqlMapConfig.xml,配置内容和顺序如下: properties(属性) setting(全局配置参数) typeAliases(类名别名) typeHandlers(类名处理器) objectFactory(对

Spring+SpringMVC +MyBatis整合配置文件案例66666

Spring+SpringMVC +MyBatis整合配置文件案例 标签: springspringmvcmybatismvcjava 2017-04-13 19:12 228人阅读 评论(1) 收藏 举报 分类: java_javaSE(2) 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] Spring+SpringMVC +MyBatis整合配置文件案例 针对spring/SpringMVC/MyBatis三个框架的整合有很多的方式,经过最近的学习我来总结一下其配置文

SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释

SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释 2016-04-14 23:40 13030人阅读 评论(2) 收藏 举报 分类: SSM(7) 这几天一直在整合SSM框架,虽然网上有很多已经整合好的,但是对于里面的配置文件并没有进行过多的说明,很多人知其然不知其所以然,经过几天的搜索和整理,今天总算对其中的XML配置文件有了一定的了解,所以拿出来一起分享一下,希望有不足的地方大家批评指正~~~ 首先   这篇文章暂时只对框架中所要用到的配置文件进行解

Spring+SpringMVC重复加载配置文件问题

sping+springmvc的框架中,IOC容器的加载过程 http://my.oschina.net/xianggao/blog/596476 基本上是先加载ContextLoaderListener,然后生成一个ioc容器. 然后再实例化DispatchServlet时候在加载对应的配置文件,再次生成Controller相关的IOC容器 关于上面两个容器关系: ContextLoaderListener中创建ApplicationContext主要用于整个Web应用程序需要共享的一些组件,

spring+springmvc+mybatis配置文件

最近在学习SSM,自己上手了一个crm项目,这两天对底层配置文件做了个总结. sqlmapconfig.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd&qu

基于Spring+SpringMVC+Mybatis的Web系统搭建

主要的后端架构:Spring+SpringMVC+Mybatis+Shiro+Maven  IDE:IntelliJ IDEA 15.0.2 jdk:1.8.0_66 系统完整源码 https://github.com/Wellat/Factor 系统目录结构 跑起来效果 搭建步骤 1.用Idea创建maven项目 2.配置pom.xml文件,添加依赖 1 <?xml version="1.0" encoding="UTF-8"?> 2 <proj

SSM框架 (Spring+SpringMVC+MyBatis)

SSM框架--详细整合教程(Spring+SpringMVC+MyBatis) springspringmvcmybatis整合教程ssm整合 1.基本概念  1.1.Spring          Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来.它是为了解决企业应用开发的复杂性而创建的.Spri

SSM框架整合(Spring+SpringMVC+MyBatis)

[SSM的系统架构] [整合概述] 第一步: MyBatis和Spring整合,通过Spring管理mapper接口. 使用mapper的扫描器自动扫描mapper接口在Spring中进行注册. 第二步: 通过Spring管理Service接口. 使用配置方式将Service接口配置在Spring配置文件中. 实现事务控制. 第三步: 由于SpringMVC是Spring的模块,无需整合这两个. [工程截图] [数据库的items] [ 表结构 ] [ 表内数据 ] [1.整合dao]将Myba