SpringMVC4零配置--web.xml
servlet3.0+規範後,允許servlet,filter,listener不必宣告在web.xml
中,而是以硬編碼的方式存在,實現容器的零配置。
ServletContainerInitializer:啟動容器時負責載入相關配置
package javax.servlet;
import java.util.Set;
public interface ServletContainerInitializer {
public void onStartup(Set<Class<?>> c, ServletContext ctx)
throws ServletException;
}
容器啟動時會自動掃描當前服務中ServletContainerInitializer
的實現類,並呼叫其onStartup
方法,其引數Set<Class<?>> c
,可通過在實現類上宣告註解javax.servlet.annotation.HandlesTypes(xxx.class)
註解自動注入,@HandlesTypes
會自動掃描專案中所有的xxx.class
的實現類,並將其全部注入Set
。
Spring為其提供了一個實現類:
SpringServletContainerInitializer
package org.springframework.web;
import java.lang.reflect.Modifier;
import java.util.LinkedList;
import java.util.List;
import java.util.ServiceLoader;
import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {
@Override
public void onStartup(Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)
throws ServletException {
List<WebApplicationInitializer> initializers = new LinkedList<WebApplicationInitializer>();
if (webAppInitializerClasses != null) {
for (Class<?> waiClass : webAppInitializerClasses) {
// Be defensive: Some servlet containers provide us with invalid classes,
// no matter what @HandlesTypes says...
if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) &&
WebApplicationInitializer.class.isAssignableFrom(waiClass)) {
try {
initializers.add((WebApplicationInitializer) waiClass.newInstance());
}
catch (Throwable ex) {
throw new ServletException("Failed to instantiate WebApplicationInitializer class", ex);
}
}
}
}
if (initializers.isEmpty()) {
servletContext.log("No Spring WebApplicationInitializer types detected on classpath");
return;
}
AnnotationAwareOrderComparator.sort(initializers);
servletContext.log("Spring WebApplicationInitializers detected on classpath: " + initializers);
for (WebApplicationInitializer initializer : initializers) {
initializer.onStartup(servletContext);
}
}
}
從中可以看出,WebApplicationInitializer
才是我們需要關心的介面,我們只需要將相應的servlet,filter,listener等硬編碼到該介面的實現類中即可。比如:
xml配置:
<!-- Log4jConfigListener -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:config/properties/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- OpenSessionInViewFilter -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- DemoServlet -->
<servlet>
<servlet-name>demoServlet</servlet-name>
<servlet-class>web.function.servlet.DemoServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>demoServlet</servlet-name>
<url-pattern>/demo_servlet</url-pattern>
</servlet-mapping>
編碼配置:
@Order(1)
public class CommonInitializer implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
//Log4jConfigListener
servletContext.setInitParameter("log4jConfigLocation", "classpath:config/properties/log4j.properties");
servletContext.addListener(Log4jConfigListener.class);
//OpenSessionInViewFilter
OpenSessionInViewFilter hibernateSessionInViewFilter = new OpenSessionInViewFilter();
FilterRegistration.Dynamic filterRegistration = servletContext.addFilter(
"hibernateFilter", hibernateSessionInViewFilter);
filterRegistration.addMappingForUrlPatterns(
EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE), false, "/");
//DemoServlet
DemoServlet demoServlet = new DemoServlet();
ServletRegistration.Dynamic dynamic = servletContext.addServlet(
"demoServlet", demoServlet);
dynamic.setLoadOnStartup(2);
dynamic.addMapping("/demo_servlet");
}
}
Spring為我們提供了一些WebApplicationInitializer
的抽象類,我們只需要繼承並按需修改即可,比如:
1)****org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer : SpringSecurity相關配置
xml配置:
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
編碼配置:
@Order(2)
public class WebAppSecurityInitializer extends AbstractSecurityWebApplicationInitializer
{
//servletContext.addListener("org.springframework.security.web.session.HttpSessionEventPublisher");
//session監聽器
@Override
protected boolean enableHttpSessionEventPublisher() {
return true;
}
}
2)org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer:MVC相關配置,比如載入spring配置檔案,宣告****DispatcherServlet等等,參看下面的對比:
xml配置:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/context/applicationContext-AppConfig.xml,
classpath:config/context/applicationContext-SpringSecurityConfig.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>webmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/context/applicationContext-MvcConfig.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
編碼方式:
@Order(3)
//spring DispatcherServlet的配置,其它servlet和監聽器等需要額外宣告,用@Order註解設定啟動順序
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
/*
* DispatcherServlet的對映路徑
*/
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
/*
* 應用上下文,除web部分
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected Class[] getRootConfigClasses() {
//載入配置檔案類,這裡與上面的xml配置是對應的,需要使用@Configuration註解進行標註,稍後介紹
return new Class[] {AppConfig.class, SpringSecurityConfig.class};
}
/*
* web上下文
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected Class[] getServletConfigClasses() {
return new Class[] {MvcConfig.class};
}
/*
* 註冊過濾器,對映路徑與DispatcherServlet一致,路徑不一致的過濾器需要註冊到另外的WebApplicationInitializer中
*/
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
return new Filter[] {characterEncodingFilter};
}
}
相關文章
- SpringMVC基於程式碼的配置方式(零配置,無web.xml)SpringMVCWebXML
- web.xml相關配置WebXML
- websphere中web.xml配置WebXML
- web.xml配置詳解7WebXML
- web.xml配置詳解6WebXML
- web.xml配置詳解5WebXML
- web.xml配置詳解4WebXML
- web.xml配置詳解3WebXML
- web.xml配置詳解2WebXML
- web.xml中的shiroFilter配置WebXMLFilter
- 在web.xml中配置過濾器WebXML過濾器
- web.xml檔案配置的說明WebXML
- springMVC---配置檔案解析(web.xml)SpringMVCWebXML
- web.xml 檔案 配置載入順序WebXML
- 在web.xml檔案中配置Servlet時,主要配置哪些資訊?WebXMLServlet
- 使用struts2框架,web.xml怎麼配置框架WebXML
- Servlet開發技術,建立,以及Servlet的配置,web.xml的配置ServletWebXML
- 如何用程式碼修改struts中的web.xml配置WebXML
- web.xml作用WebXML
- 零配置部署ReactReact
- spring cloud分散式微服務-springboot省去web.xml配置Cloud分散式微服務Spring BootWebXML
- JavaWeb開發之建立Servlet無法自動更新web.xml配置JavaWebServletXML
- web.xml的作用WebXML
- web.xml之一WebXML
- 關於web.xmlWebXML
- web.xml詳解WebXML
- web.xml元素介紹WebXML
- Struts2在web.xml中配置為“/*”和“*.action,*.jsp”的差別WebXMLJS
- web.xml配置引數context-param和init-param的區別WebXMLContext
- maven專案增加web.xmlMavenWebXML
- Struts2 web.xml(七)WebXML
- web.xml檔案的作用WebXML
- 【原創】Web.xml簡介WebXML
- 從零開始--webpack 4 配置Web
- 從零開始配置 MySQL MMMMySql
- 在web.xml中配置載入applicationContext.xml中總說找不到類?WebXMLAPPContext
- 不同版本的 web.xml 頭資訊WebXML
- web.xml中的servlet相關WebXMLServlet