SpringMVC的配置2种(本质上还是一样的,实现的接口不同)

第一种SpringInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer

看第一种配置

java 复制代码
package com.xxx.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpringInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {


    /**
     * //加载spring容器配置
     * 加载根配置类
     * @return
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{RootConfig.class};
    }

    /**
     * 加载springmvc配置类(web容器配置类)
     * @return
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};
    }

    /**
     * 设置哪些请求归属springMVC处理
     * 配置SpringMVC的映射路径(访问路径)
     * 配置SpringMVC拦截哪些请求(哪些请求应该交给DispatcherServlet来处理)
     * @return
     * return new String[]{"/"}; 拦截所有请求,但不拦截jsp页面 (相当于自动进入SpringMVC里面)
     * return new String[]{"/*"}; 表示拦截所有请求 (也相当于自动进入SpringMVC里面)
     * return new String[0];     html和jsp两个页面都可以访问到
     */

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/*"};
    }
}

WebConfig代码

java 复制代码
package com.xxx.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * SpringMVC配置类(管理的是配置类)
 *  -管理控制器(Servlet)
 *  @EnableWebMvc:开启SpringMVC注解,开启后可以使用SpringMVC提供的相关注解
 */
@Configuration
@ComponentScan("com.xxx.controller")
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
    //放行静态资源
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}

第二种ServletContainersInitConfig extends AbstractDispatcherServletInitializer

第二种还没配根(Spring)容器

java 复制代码
package com.xxx.config;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

//4.定义一个servlet容器启动的配置类,在里面加载spring的配置
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {

    //加载springMVC容器配置
    @Override
    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(SpringMvcConfig.class);
        return ctx;
    }

    //设置哪些请求归属springMVC处理
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    //加载spring容器配置
    @Override
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }

}

SpringMvcConfig代码

java 复制代码
package com.xxx.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//3.创建springmvc的配置文件,加载controller对应的bean
@Configuration
@ComponentScan("com.xxx.controller")
public class SpringMvcConfig {
}
相关推荐
民乐团扒谱机19 分钟前
脉冲在克尔效应下的频谱展宽仿真:原理与 MATLAB 实现
开发语言·matlab·光电·非线性光学·克尔效应
yuan1999723 分钟前
基于扩展卡尔曼滤波的电池荷电状态估算的MATLAB实现
开发语言·matlab
Tony Bai25 分钟前
Go GUI 开发的“绝境”与“破局”:2025 年现状与展望
开发语言·后端·golang
豆浆whisky25 分钟前
Go分布式追踪实战:从理论到OpenTelemetry集成|Go语言进阶(15)
开发语言·分布式·golang
2401_8604947025 分钟前
Rust语言高级技巧 - RefCell 是另外一个提供了内部可变性的类型,Cell 类型没办法制造出直接指向内部数据的指针,为什么RefCell可以呢?
开发语言·rust·制造
Tony Bai26 分钟前
【Go模块构建与依赖管理】08 深入 Go Module Proxy 协议
开发语言·后端·golang
浪裡遊26 分钟前
Next.js路由系统
开发语言·前端·javascript·react.js·node.js·js
程序员-小李27 分钟前
基于 Python + OpenCV 的人脸识别系统开发实战
开发语言·python·opencv
QX_hao28 分钟前
【Go】--文件和目录的操作
开发语言·c++·golang
卡提西亚29 分钟前
C++笔记-20-对象特性
开发语言·c++·笔记