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 {
}
相关推荐
松涛和鸣2 小时前
从零开始理解 C 语言函数指针与回调机制
linux·c语言·开发语言·嵌入式硬件·排序算法
j***78882 小时前
clickhouse-介绍、安装、数据类型、sql
java
2***B4492 小时前
Rust在系统编程中的内存安全
开发语言·后端·rust
U***e632 小时前
Rust错误处理最佳实践
开发语言·后端·rust
习习.y2 小时前
python笔记梳理以及一些题目整理
开发语言·笔记·python
q***47182 小时前
Spring中的IOC详解
java·后端·spring
qq_386218993 小时前
Gemini生成的自动搜索和下载论文的python脚本
开发语言·python
vx_vxbs663 小时前
【SSM电影网站】(免费领源码+演示录像)|可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案
java·spring boot·python·mysql·小程序·php·idea
SunnyDays10113 小时前
如何使用 Java 删除 Word 文档中的水印
java·删除word文档水印
o***Z4484 小时前
JavaScript在Node.js中的内存管理
开发语言·javascript·node.js