xml零配置之AbstractAnnotationConfigDispatcherServletInitializer

本文介绍了如何在Servlet 3.0环境中使用AbstractAnnotationConfigDispatcherServletInitializer接口进行XML免配置,实现Spring MVC应用的初始化。通过实现该接口,可以注册配置有注解的类,替代传统的web.xml配置。

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

1.实现AbstractAnnotationConfigDispatcherServletInitializer接口,在servlet3.0中web.xml启动时会检测该接口实现类

package org.springframework.source.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.*;

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

//指定开始被servlet处理的url,配置从/开始
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
	
//配置root上下文,如Jpa数据源等等的配置
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] {ApplicationConfig.class, JpaConfig.class, SecurityConfig.class};
    }
	
//配置dispatcher servlet,如果在root config指定了该转发规则就可以忽略
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] {WebMvcConfig.class};
    }
//配置servlet过滤器
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        DelegatingFilterProxy securityFilterChain = new DelegatingFilterProxy("springSecurityFilterChain");
        return new Filter[] {characterEncodingFilter, securityFilterChain};
    }
//当registerDispatcherServlet完成时自定义registration
    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        registration.setInitParameter("defaultHtmlEscape", "true");
        registration.setInitParameter("spring.profiles.active", "default");
    }
}
官方doc

Base class for WebApplicationInitializer implementations that register a DispatcherServlet configured with annotated classes

很清楚吧,我们要他来register a DispatcherServlet,他会在web启动时被系统扫秒到并配置我们的servlet转发规则及web的上下文

或者你可以使用已经包含该接口的WebApplicationInitializer

Interface to be implemented in Servlet 3.0+ environments in order to configure the ServletContext programmatically -- as opposed to (or possibly in conjunction with) the traditional web.xml-based approach.
Implementations of this SPI will be detected automatically by SpringServletContainerInitializer, which itself is bootstrapped automatically by any Servlet 3.0 container. See its Javadoc for details on this bootstrapping mechanism.
2.实现该接口时的最重要的两个
dispatcher servlet
RootConfigClasses
3.两个就不多说了
4.现在看看web.xml的配置,很简单吧
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	<!-- Map all errors to Spring MVC handler method. See CustomErrorController.generalError() -->
	<error-page>
    	<location>/generalError</location>
	</error-page>
</web-app>





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值