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 分钟前
Java设计模式精讲---01工厂方法模式
java·设计模式·工厂方法模式
cherry--3 分钟前
集合(开发重点)
java·开发语言
半桶水专家9 分钟前
父子组件通信详解
开发语言·前端·javascript
寻星探路16 分钟前
测试开发话题10---自动化测试常用函数(2)
java·前端·python
鸢尾掠地平22 分钟前
Python中常用内置函数上【含代码理解】
开发语言·python
api_1800790546035 分钟前
请求、认证与响应数据解析:1688 商品 API 接口深度探秘
java·大数据·开发语言·mysql·数据挖掘
陈果然DeepVersion35 分钟前
Java大厂面试真题:Spring Boot+Kafka+AI智能客服场景全流程解析(十二)
java·spring boot·ai·kafka·面试题·向量数据库·rag
yours_Gabriel36 分钟前
【设计模式】UML和设计原则
java·设计模式·uml
唐古乌梁海37 分钟前
【Java】JVM 内存区域划分
java·开发语言·jvm
低调小一41 分钟前
Android Gradle 的 compileOptions 与 Kotlin jvmTarget 全面理解(含案例)
android·开发语言·kotlin