spring与servlet3.0无web.xml文件时的配置方式


    这里只做简要的记录,更详细的使用方式,更详细的资料参见spring参考文档(21.15 Code-based Servlet container initialization),以后会慢慢补充。


110857_0kK6_1011578.png

21.15 Code-based Servlet container initialization

In a Servlet 3.0+ environment, you have the option of configuring the Servlet container programmatically as an alternative or in combination with a web.xml file. Below is an example of registering a DispatcherServlet:

import org.springframework.web.WebApplicationInitializer; 
public class MyWebApplicationInitializer implements WebApplicationInitializer {     
    @Override
    public void onStartup(ServletContext container) {
        XmlWebApplicationContext appContext = new XmlWebApplicationContext();
        appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");

        ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(appContext));
        registration.setLoadOnStartup(1);
        registration.addMapping("/");
    }

}

WebApplicationInitializer is an interface provided by Spring MVC that ensures your implementation is detected and automatically used to initialize any Servlet 3 container. An abstract base class implementation of WebApplicationInitializer named AbstractDispatcherServletInitializer makes it even easier to register the DispatcherServlet by simply overriding methods to specify the servlet mapping and the location of the DispatcherServlet configuration:

public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {    
     @Override
    protected Class<?>[] getRootConfigClasses() {        return null;
    }    @Override
    protected Class<?>[] getServletConfigClasses() {        return new Class[] { MyWebConfig.class };
    }    @Override
    protected String[] getServletMappings() {        return new String[] { "/" };
    }

}

The above example is for an application that uses Java-based Spring configuration. If using XML-based Spring configuration, extend directly from AbstractDispatcherServletInitializer:

public class MyWebAppInitializer extends AbstractDispatcherServletInitializer { 
    @Override
    protected WebApplicationContext createRootApplicationContext() { 
        return null;
    } 
    @Override
    protected WebApplicationContext createServletApplicationContext() {
 
        XmlWebApplicationContext cxt = new XmlWebApplicationContext();
        cxt.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml"); 
        return cxt;
    } 
    @Override
    protected String[] getServletMappings() { 
        return new String[] { "/" };
    }

}

AbstractDispatcherServletInitializer also provides a convenient way to add Filter instances and have them automatically mapped to the DispatcherServlet:

public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {    // ...    @Override
    protected Filter[] getServletFilters() { 
        return new Filter[] { new HiddenHttpMethodFilter(), new CharacterEncodingFilter() };
    }

}

Each filter is added with a default name based on its concrete type and automatically mapped to the DispatcherServlet.

The isAsyncSupported protected method of AbstractDispatcherServletInitializer provides a single place to enable async support on the DispatcherServlet and all filters mapped to it. By default this flag is set to true.

Finally, if you need to further customize the DispatcherServlet itself, you can override the createDispatcherServlet method.

转载于:https://my.oschina.net/sskxyz/blog/601719

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值