六、基于Java配置的DispatcherServlet

本文详细介绍了如何使用Java配置启动Spring MVC的DispatcherServlet。内容包括使用WebApplicationInitializer初始化容器,启用MVC Java配置和XML配置,以及配置视图解析器。通过Java配置,可以更方便地管理和控制Spring MVC的行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DispatcherServlet是Spring MVC的核心。在这里请求会第一次接触到框架,它要负责将请求路由到其他的组件之中。按照传统的方式,
像DispatcherServlet这样的Servlet会配置在web.xml文件中,这个文件会放到应用的WAR包里面。当然,这是配置DispatcherServlet的方法之一。
但是,借助于Servlet 3规范和Spring 3.1的功能增强,这种方式已经不是唯一的方案了。

使用代码实例化容器

WebApplicationInitializer是Spring MVC提供的一个接口,用于确保你的实现探测并且自动初始化Servlet 3容器。
WebApplicationInitializer有一个抽象基类实现AbstractDispatcherServletInitializer,实现该抽象类非常简单,继承它,实现servlet映射方法和DispatcherServlet配置的定位方法即可

package com.lf.web.config;

import com.lf.web.WebConfig;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * 此配置相当于web.xml此种方法用于指定配置类
 * Created by LF on 2017/5/4.
 */
public class MyJavaConfigDispatcherServlet extends AbstractAnnotationConfigDispatcherServletInitializer {


    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};//指定配置类
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};//请求映射
    }
}

若是使用基于xml的spring配置, 则是继承AbstractDispatcherServletInitializer


package com.lf.web.config;

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

/**
 * Created by LF on 2017/5/5.
 */


public class MyXmlConfigDispatcherServlet extends AbstractDispatcherServletInitializer {
    @Override
    protected WebApplicationContext createServletApplicationContext() {

        return null;
    }

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

    @Override
    protected WebApplicationContext createRootApplicationContext() {
        XmlWebApplicationContext context = new XmlWebApplicationContext();
        context.setConfigLocations("classpath:application.xml");
        return context;
    }
}

启用Spring MVC

基于Java配置和基于XML配置都提供了相似的默认配置,用于覆盖DispatcherServlet默认实现。 之所以这么做,是为了减少重复工作,因为多数应用都使用相同的配置,也是为了提供更高层级的抽象配置,降低学习曲线,使用户仅需要很少的关于配置的背景知识就可以使用Spring MVC了。
MVC基于java的配置比基于XML的配置要更容易些,也有更细粒度的控制。

开启MVC Java配置

@Configuration 注解的类上使用注解@EnableWebMvc即可开启MVC Java配置

开启MVC XML配置

若是使用XML达到同样的效果,得在DispatchServlet上下文中使用mvc:annotation-driven元素(如果没有DispatchServlet 环境则是在root context根环境中)


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven />

</beans>

这可以运行起来,它的确能够启用Spring MVC,但还有不少问题要解
决:
- 没有配置视图解析器。如果这样的话,Spring默认会使
用BeanNameView-Resolver,这个视图解析器会查找ID与视
图名称匹配的bean,并且查找的bean要实现View接口,它以这样
的方式来解析视图
- 没有启用组件扫描。这样的结果就是,Spring只能找到显式声明
在配置类中的控制器。
- 这样配置的话,DispatcherServlet会映射为应用的默认
Servlet,所以它会处理所有的请求,包括对静态资源的请求,如
图片和样式表.

配置视图解析器

package com.lf.web;

import com.lf.web.config.MyHttpMessage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.List;

/**
 * Created by LF on 2017/5/4.
 */
@Configuration
@EnableWebMvc
@ComponentScan//组件扫描
public class WebConfig extends WebMvcConfigurerAdapter {


    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        super.configureViewResolvers(registry);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值