当前位置:首页 > Web开发 > 正文

项目实战之干失Web.xml

2024-03-31 Web开发

在java Web开发过程我们早已习惯了Web.xml的繁杂的配置,我们从出产低下的”jsp+Servlet”时期进入到SSH在步入到较为先进的SSM时期,仍然没有逃脱Web.xml.令人惊喜的是Servlet3.0 带来了全新的配置方法允许我们可以使用编程的方法实现接口去进行ServletContext配置.正如Spring官方文档里说的,这和基于(或者可能是结合)传统的web.xml要领恰恰相反。我感受应该是一个令人惊喜的转变.而后在SpringBoot给我们带来了AutoConfiguration,极大的解放了出产力.

今天我要说的不是使用SpringBoo干失Web.xml,而是在SSM框架中干失Web.xml,请听我细细道来.

可恨的Web.xml <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/dispatcher-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>

上面就是我们 在使用SSM框架开发所要配置的Web.xml文件,是不是挺繁琐的.

现如今,YAML,Properties,JSON等文件以简洁和可读性更高的长处不禁让我们趋之若鹜,宁愿拜倒在他们的石榴裙下.固然,我并不是说XML文件应该被裁减,它在有些处所有着不成替代的感化;而是我感受配置文件就应该简洁,让人一眼就知道一段代码是干啥的,不需要解读标签的含义就能够全部理解.说了这么多那我们该如何替代呢?请往下看.

WebApplicationInitializer简介

检察源码得知,WebApplicationInitializer就是一个接口,其布局很简单,就包罗一个要领onStartup(),布局如下:

public interface WebApplicationInitializer { /** * Configure the given {@link ServletContext} with any servlets, filters, listeners * context-params and attributes necessary for initializing this web application. See * examples {@linkplain WebApplicationInitializer above}. * @param servletContext the {@code ServletContext} to initialize * @throws ServletException if any call against the given {@code ServletContext} * throws a {@code ServletException} */ void onStartup(ServletContext servletContext) throws ServletException; }

所有的初始化类都实现了此接口,直接或间接实现此接口的类有:AbstractContextLoaderInitializer和AbstractReactiveWebInitializer这两个抽象类,AbstractReactiveWebInitializer感化是为了便利WebApplicationInitializer 将一个ClassLoaderListener 注册到Servlet 上下文中.而AbstractReactiveWebInitializer的感化是为响应式Web 措施供给一个初始化基类.由此可见,我们使用JavaConfig模式不只可以配置传统的Web应用,也可以很好的配置响应式Web应用.下面让我们先来看看该如何操作javaConfig和Web.xml进行.下面是对应DispatcherServlet注册逻辑, WebApplicationInitializer实现如下:

public class MyWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { XmlWebApplicationContext appContext = new XmlWebApplicationContext(); appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml"); ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(appContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } }

如果对付上述的配置有其他的考虑,也可以担任org.springframework.web.servlet.support.AbstractDispatcherServletInitializer,此类的父类就是我们上面提到的AbstractContextLoaderInitializer并在其根本长进行了扩展.担任此类Spring将会自动为你完成配置.

我们看到这种方法更简单,也更简洁.不用担忧措置惩罚惩罚初始化参数,只是普通的JavaBean气势派头的属性和结构函数参数。

在官方文档中提到,Spring的大大都组件已经进行了更新优化,用来撑持上述气势派头的注册事情.当你在使用Spring或者源码的时候,你会诧异的发明DispatcherServlet , FrameworkServlet , ContextLoaderListener和DelegatingFilterProxy此刻都撑持使用结构函数进行参数注册.此刻如果我们,们使用Servlet3.0,我们完全的干失Web.xml,用代码将ServletContext API的进行实现和扩展,以配置init-params, context-params 等须要的参数.

既然有了这么多的撑持,我们又该如何完美且不掉优雅的干失Web.xml呢?请看代码:

public class MyWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { // Create the 'root' Spring application context AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(AppConfig.class); // Manage the lifecycle of the root application context container.addListener(new ContextLoaderListener(rootContext)); // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(DispatcherConfig.class); // Register and map the dispatcher servlet ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } }

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32938.html