Spring MVC配置文件

在Spring MVC项目中,配置文件是至关重要的,它们帮助Spring框架正确地初始化并管理应用。通常,在web.xml中配置Spring的DispatcherServlet,并在applicationContext.xml和spring-mvc.xml中配置Spring的Bean定义和MVC相关设置。

  1. web.xml配置

web.xml是Web应用的部署描述符,用于配置Servlet容器启动时需要加载的Servlet、监听器等。对于Spring MVC应用,通常需要在这里配置DispatcherServlet。

‌示例配置:‌

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee

http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

version="4.0">

<display-name>Spring MVC Application</display-name>

<!-- Spring DispatcherServlet Configuration -->

<servlet>

<servlet-name>dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/spring-mvc-config.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<!-- Spring Listener -->

<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>

</web-app>

  1. applicationContext.xml配置

applicationContext.xml通常包含Spring的全局配置,例如数据源、事务管理器、业务层Bean等。这个文件通常不包含MVC特定的配置。

‌示例配置:‌

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- DataSource configuration -->

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver"/>

<property name="url" value="jdbc:mysql://localhost:3306/yourdatabase"/>

<property name="username" value="root"/>

<property name="password" value="password"/>

</bean>

<!-- Transaction manager -->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"/>

</bean>

<!-- Other beans -->

</beans>

  1. spring-mvc.xml配置(或使用Java Config)

对于MVC特定的配置,可以在spring-mvc.xml中定义,或者使用Java配置类(推荐方式)。spring-mvc.xml应该包含视图解析器、控制器、拦截器等MVC相关的配置。

‌XML配置示例:‌

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 扫描 Controller 层 -->

<context:component-scan base-package="com.example.controller" />

<!-- 启用注解驱动(@RequestMapping, @ResponseBody 等) -->

<mvc:annotation-driven />

<!-- 静态资源处理:避免 DispatcherServlet 拦截 /static/ 下的资源 -->

<mvc:resources mapping="/static/**" location="/static/" />

<!-- 视图解析器:将逻辑视图名解析为 JSP 文件 -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/views/"/>

<property name="suffix" value=".jsp"/>

</bean>

<!-- 文件上传配置(如需) -->

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<property name="maxUploadSize" value="5242880"/> <!-- 5MB -->

</bean>

</beans>

相关推荐
callJJ2 小时前
SpringBoot 自动配置原理详解——从“约定优于配置“到源码全程追踪
java·spring boot·后端·spring
小江的记录本2 小时前
【分布式】分布式一致性协议:2PC/3PC、Paxos、Raft、ZAB 核心原理、区别(2026必考Raft)
java·前端·分布式·后端·安全·面试·系统架构
北风toto2 小时前
RestTemplate 的入门使用,直接给上作者的项目Demo
java
疯狂打码的少年2 小时前
JDK 7、8、13 和 20区别深度了解
java·开发语言
钝挫力PROGRAMER2 小时前
Java中如何优雅管理接口的多个实现
java·设计模式
迷藏4942 小时前
# 发散创新:基于Python的自动特征工程实战与深度优化在机器学习
java·开发语言·python·机器学习
星晨雪海2 小时前
查询区域列表并统计点位数量
java
Seven972 小时前
用300行代码手写一个mini版的Tomcat
java
隐退山林2 小时前
JavaEE进阶:SpirngMVC入门(2)
java·java-ee