今天有人問我web.xml的配置,早些年看過的配置就用了一次,后來又都用了Java Config,把web.xml都忘了個(gè)干凈,略是尷尬,因此再開個(gè)專題《知其所以然》來細(xì)究一些java雜七雜八的細(xì)節(jié)問題。
今天先來談?wù)剋eb.xml的前身今世。
首先明確一個(gè)概念,web.xml不是必須的用xml形式存在的,一樣可以使用Java Config完成.
當(dāng)時(shí)被人唬住了。web.xml 不是在項(xiàng)目中必須出現(xiàn)的,可以使用JavaConfig 替代。??!在朝花夕拾中之Spring WebMvc 中提到繼承了 AbstractAnnotationConfigDispatcherServletInitializer 的WebMVCInitializer的類,就是web.xml的java config 形式。
web.xml 主要作用是通知 Servlet Containers (Tomcat) 從哪些類加載 & Url映射 & filter是哪些 & 等等。在servlet 3.0之后,@WebServlet, @WebListener 這些annotations 可以做這類的標(biāo)記工作了,Spring 也早就在框架里集成了這些功能
下面放出博主實(shí)現(xiàn)的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();
}
博主一直都覺得注釋寫的真好,加上函數(shù)名,基本上作什么用,改怎么用都解釋的很清楚了。
最后插一句,xml配置不一定一直好于Java Config。