基于 xml 和注解的配置
引入 Spring,SpringMVC 相关依赖 (通过 Maven 或 Gradle 完成)
在 web.xml 配置监听器和 ServletContext 上下文参数 (即 Spring IoC 容器初始化文件), 当 ServletContext 容器建立时初始化 Spring IoC 容器 (即初始化根上下文)
配置 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_3_1.xsd"
- version="3.1">
- <!-- 1. 配置监听器 -->
- <listener>
- <listener-class>
- org.springframework.Web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <!-- 指定 Spring Bean 的配置文件所在目录. 默认配置在 WEB-INF 目录下 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
- <!-- 配置 DispatcherServlet, 默认配置在 WEB-INF 目录下 -->
- <servlet>
- <servlet-name>dispatcherConfig</servlet-name>
- <servlet-class>org.springframework.Web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>dispatcherConfig</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </Web-App>
在配置 DispatcherServlet 时, 容器默认读取 Web-INF 目录下的 - servlet.xml 文件.
<servlet-name>-servlet.xml 一般需要配置 3 点:
启动注解
配置组件扫描
声明视图解析器
- <?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:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- <!-- 启用注解 -->
- <context:annotation-config />
- <!-- 配置组件扫描 -->
- <context:component-scan base-package="com.weixia.web"></context:component-scan>
- <!-- 配置视图解析器 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
- </beans>
基于纯注解配置
1. 继承 AbstractAnnotationConfigDispatcherServletInitializer 类
在 Servlet 3.0 环境中, 容器会在类路径中查找实现 javax.servlet.ServletContainerInitializer 接口的类, 如果能发现的话, 就会用它来配置 Servlet 容器.
Spring 提供了这个接口的实现, 名为 SpringServletContainerInitializer , 这个类反过来又会查找实现 了 WebApplicationInitializer 的类并将配置的任务交给它们来完成. Spring 3.2 引入了一个 WebApplicationInitializer 的基础实现类 AbstractAnnotationConfigDispatcherServletInitializer
- // 示例代码出自《Spring 4 实战》
- package spittr.config;
- import org.springframework.Web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
- public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
- @Override
- protected String[] getServletMappings() {
- return new String[] {"/"};
- }
- @Override
- protected Class<?>[] getRootConfigClasses() {
- return new Class<?>[] {RootConfig.class};
- }
- @Override
- protected Class<?>[] getServletConfigClasses() {
- return new Class<?>[] {WebConfig.class};
- }
- }
getServletMappings 方法描述前端 DispatcherServlet 的请求路径
getRootConfigClasses 方法用于配置 Spring IoC 容器
getServletConfigClasses 方法用于初始化 DispatcherServlet 上下文
WebConfig 是基于 java 类的配置文件
附: 基于 IDEA 的 SpringMVC + Maven 项目结构配置
搭建 maven 工程
添加 Web 框架
3, 将应用打包, 等待部署到服务器上
4, 将应用部署到服务器上 (以 Tomcat 为例)
来源: http://www.bubuko.com/infodetail-3352093.html