今天有人问我web.xml的配置,早些年看过的配置就用了一次,后来又都用了Java Config,把web.xml都忘了个干净,略是尴尬,因此再开个专题《知其所以然》来细究一些java杂七杂八的细节问题。
今天先来谈谈web.xml的前身今世。
首先明确一个概念,web.xml不是必须的用xml形式存在的,一样可以使用Java Config完成.
当时被人唬住了。web.xml 不是在项目中必须出现的,可以使用JavaConfig 替代。!!在朝花夕拾中之Spring WebMvc 中提到继承了 AbstractAnnotationConfigDispatcherServletInitializer 的WebMVCInitializer的类,就是web.xml的java config 形式。
web.xml 主要作用是通知 Servlet Containers (Tomcat) 从哪些类加载 & Url映射 & filter是哪些 & 等等。在servlet 3.0之后,@WebServlet, @WebListener 这些annotations 可以做这类的标记工作了,Spring 也早就在框架里集成了这些功能
下面放出博主实现的web.xml的Java Config 版本。
WebMVCInitializer
public class WebMVCInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{
Application.class, MyBatisConfig.class
};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{};
}
@Override
protected String[] getServletMappings() {
return new String[]{
"/"
};
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
servletContext.addListener(new SessionListener());
}
}
父类AbstractAnnotationConfigDispatcherServletInitializer
public abstract class AbstractAnnotationConfigDispatcherServletInitializer
extends AbstractDispatcherServletInitializer {
/**
* {@inheritDoc}
* <p>This implementation creates an {@link AnnotationConfigWebApplicationContext},
* providing it the annotated classes returned by {@link #getRootConfigClasses()}.
* Returns {@code null} if {@link #getRootConfigClasses()} returns {@code null}.
*/
@Override
protected WebApplicationContext createRootApplicationContext() {
Class<?>[] configClasses = getRootConfigClasses();
if (!ObjectUtils.isEmpty(configClasses)) {
AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
rootAppContext.register(configClasses);
return rootAppContext;
}
else {
return null;
}
}
/**
* {@inheritDoc}
* <p>This implementation creates an {@link AnnotationConfigWebApplicationContext},
* providing it the annotated classes returned by {@link #getServletConfigClasses()}.
*/
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext servletAppContext = new AnnotationConfigWebApplicationContext();
Class<?>[] configClasses = getServletConfigClasses();
if (!ObjectUtils.isEmpty(configClasses)) {
servletAppContext.register(configClasses);
}
return servletAppContext;
}
/**
* Specify {@link org.springframework.context.annotation.Configuration @Configuration}
* and/or {@link org.springframework.stereotype.Component @Component} classes to be
* provided to the {@linkplain #createRootApplicationContext() root application context}.
* @return the configuration classes for the root application context, or {@code null}
* if creation and registration of a root context is not desired
*/
protected abstract Class<?>[] getRootConfigClasses();
/**
* Specify {@link org.springframework.context.annotation.Configuration @Configuration}
* and/or {@link org.springframework.stereotype.Component @Component} classes to be
* provided to the {@linkplain #createServletApplicationContext() dispatcher servlet
* application context}.
* @return the configuration classes for the dispatcher servlet application context or
* {@code null} if all configuration is specified through root config classes.
*/
protected abstract Class<?>[] getServletConfigClasses();
}
博主一直都觉得注释写的真好,加上函数名,基本上作什么用,改怎么用都解释的很清楚了。
最后插一句,xml配置不一定一直好于Java Config。