<!DOCTYPE web-appPUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name>
/** Interface to be implemented in Servlet 3.0+ environments in order to configure the {@link ServletContext} programmatically as opposed to (or possibly in conjunction with) the traditional {@code web.xml}-based approach. 译: 在servlet3.0版本以上,通过实现接口以编程的方式进行配置ServletContext,这与传统的web.xml的形式近乎相反(不用web.xml文件,同样可以创建一个web应用) Implementations of this SPI will be detected automatically by {@link SpringServletContainerInitializer}, which itself is bootstrapped automatically by any Servlet 3.0 container. 译: 实现了这个SPI,将会被SpringServletContainerInitializer自动识别,并自动启动。 See {@linkplain SpringServletContainerInitializer its Javadoc} for details on this bootstrapping mechanism. 译: 可以通过查看SpringServletContainerInitializer文档去了解自启动机制的详情。 <h2>Example</h2> 译:示例 <h3>The traditional, XML-based approach</h3> 译:传统基于Xml的方式 Most Spring users building a web application will need to register Spring's {@code DispatcherServlet}. For reference, in WEB-INF/web.xml, this would typically be done as,follows: 译:大部分Spring用户在构建一个web应用程序都需要去注册一个Spring的前端控制器(DispatchServlet),而其典型的就是在WEB-INF下的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> 以上配置就是使用传统的xml配置文件的方式,向servlet容器中注册一个DispatchServlet。 The code-based approach with {@code WebApplicationInitializer} * Here is the equivalent {@code DispatcherServlet} registration logic, * {@code WebApplicationInitializer}-style: 译: 通过使用WebApplicationInitializer的基于代码的方式,其与xml是一个相同的注册逻辑。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("/"); * } * } * * As an alternative to the above, you can also extend from {@link org.springframework.web.servlet.support.AbstractDispatcherServletInitializer}. 译:你也可以通过集成自AbstractDispatcherServletInitializer来实现,以上二者任选其一 * As you can see, thanks to Servlet 3.0's new {@link ServletContext#addServlet} method we're actually registering an instance of the {@code DispatcherServlet}, and this means that the {@code DispatcherServlet} can now be treated like any other object receiving constructor injection of its application context in this case. 译:如你所见,正是由于servlet3.0 的新方法(ServletContext#addServlet)出现,我们可以通过它注册一个DispatchServlet的实例了。这也意味着DispatchServlet可以像其他对象那 样,通过构造注入的方式接受一个Application Context This style is both simpler and more concise. There is no concern for dealing with init-params, etc, just normal JavaBean-style properties and constructor arguments. You are free to create and work with your Spring application contexts as necessary before injecting them into the {@code DispatcherServlet}. 译:这种方式既简单又简洁。我们不需要如何去处理初始化参数,等,仅仅需要处理像普通JavaBean那样的属性和构造方法参数。在你必须将他们注入到DispatchServlet中之前,你可以很自由的构建和处理你的spring应用程序。 Most major Spring Web components have been updated to support this style of registration. You'll find that {@code DispatcherServlet}, {@code FrameworkServlet},{@code ContextLoaderListener} and {@code DelegatingFilterProxy} all now support constructor arguments. Even if a component (e.g. non-Spring, other third party) has not been specifically updated for use within {@code WebApplicationInitializers}, they still may be used in any case. The Servlet 3.0 {@code ServletContext} API allows for setting init-params, context-params, etc programmatically. 译:大部分Spring web 组件都进行了更新,以至于能够支持这个注册方式,你可以发现像 DispatcherServlet、FrameworkServlet、ContextLoaderListener和DelegatingFilterProxy ,现在全部都支持构造参数注入。甚至是一些非spring,其他第三方组织的组件在WebApplicationInitializers中使用的没有支持的也会更新,以至于一直可以使用。Servlet3.0 API 也可以通过编程的方式 去设置 初始化参数、容器参数等 A 100% code-based approach to configuration In the example above, {@code WEB-INF/web.xml} was successfully replaced with code in * the form of a {@code WebApplicationInitializer}, but the actual * {@code dispatcher-config.xml} Spring configuration remained XML-based. * {@code WebApplicationInitializer} is a perfect fit for use with Spring's code-based * {@code@Configuration} classes. See @{@link org.springframework.context.annotation.Configuration Configuration} Javadoc for complete details, but the following example demonstrates refactoring to use Spring's {@link org.springframework.web.context.support.AnnotationConfigWebApplicationContext AnnotationConfigWebApplicationContext} in lieu of {@code XmlWebApplicationContext}, and user-defined {@code@Configuration} classes {@code AppConfig} and {@code DispatcherConfig} instead of Spring XML files. 译 :上面是一个完全由代码的配置方式的案例,web.xml文件已经被一个WebApplicationInitializer 成功取代。当然,实际上 Spring的配置任然保持着基于xml方式,WebApplicationInitializer 是一个更加适合 Spring代码的 配置类。可以查看Configuration的文档,了解完整的详情。但是下面这个例子是使用Spring的 AnnotationConfigWebApplicationContext 来代替XmlWebApplicationContext进行重构的。并且用户通过@Configuration,AppConfig,DispatcherConfig来代替xml配置文件。 This example also goes a bit beyond those above to demonstrate typical configuration of the 'root' application context and registration of the {@code ContextLoaderListener}: 译:这个例子比上面的例子更进一步,使用root application context 来重构的典型配置,并且注册ContextLoaderListener * public class MyWebAppInitializer implements WebApplicationInitializer { * @Override * public void onStartup(ServletContext container) { * // Create the 'root' Spring application context 创建root Spring的应用上下文 * AnnotationConfigWebApplicationContext rootContext = * new AnnotationConfigWebApplicationContext(); * rootContext.register(AppConfig.class); * // Manage the lifecycle of the root application context 管理root 应用上下文的生命周期 * container.addListener(new ContextLoaderListener(rootContext)); * // Create the dispatcher servlet's Spring application context // 创建了Servlet的前端控制器的Spring应用上下文 * 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("/"); * } * } * * As an alternative to the above, you can also extend from {@link org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer}. 你也可以通过集成自AbstractAnnotationConfigDispatcherServletInitializer,以上二者任选其一。 Remember that {@code WebApplicationInitializer} implementations are detected automatically -- so you are free to package them within your application as you see fit. 记住,WebApplicationInitializer 的实现类 都是自动识别的。所以,你可以自由的将他们打包到你认为合适的应用程序中。 Ordering {@code WebApplicationInitializer} execution {@code WebApplicationInitializer} implementations may optionally be annotated at the * class level with Spring's @{@link org.springframework.core.annotation.Order Order} * annotation or may implement Spring's {@link org.springframework.core.Ordered Ordered} * interface. If so, the initializers will be ordered prior to invocation. This provides * a mechanism for users to ensure the order in which servlet container initialization * occurs. Use of this feature is expected to be rare, as typical applications will likely * centralize all container initialization within a single {@code WebApplicationInitializer}. Caveats :说明、警告 web.xml versioning web.xml版本 {@code WEB-INF/web.xml} and {@code WebApplicationInitializer} use are not mutually exclusive; for example, web.xml can register one servlet, and a {@code WebApplicationInitializer} can register another. An initializer can even modify registrations performed in {@code web.xml} through methods such as {@link ServletContext#getServletRegistration(String)}. 译:web.xml与WebApplicationInitializer不会相互排斥,例如,web.xml可注册一个Servlet,而WebApplicationInitializer可以注册另一个,后者甚至可以修改在xml中注册的。 However, if {@code WEB-INF/web.xml} is present in the application, its {@code version} attribute must be set to "3.0" or greater, otherwise {@code ServletContainerInitializer} bootstrapping will be ignored by the servlet container 译:然而,如果web.xml存在于应用中,那么它的版本必须在3.0或以上,否则ServletContainerInitializer 自己化启动将会被Servlet容器忽略。 * <h3>Mapping to '/' under Tomcat</h3>、 在tomcat下映射/ * <p>Apache Tomcat maps its internal {@code DefaultServlet} to "/", and on Tomcat versions <= 7.0.14, this servlet mapping cannot be overridden programmatically 7.0.15 fixes this issue. Overriding the "/" servlet mapping has also been tested successfully under GlassFish 3.1 译:在tomcat7.0.14及以下,tomcat使用其自身的DefaultServlet去映射/,这个Servlet映射将不能被编程方式重写在7.0.15版本。在GlassFish 3.1下, 成功测试出可以重写/这个Servlet映射器。 */
publicinterfaceWebApplicationInitializer{
/** * Configure the given {@link ServletContext} with any servlets, filters, listeners context-params and attributes necessary for initializing this web application. 为你这个web容器配置任何必要的 filter,listener,servlet,及其参数和属性 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} */ voidonStartup(ServletContext servletContext)throws ServletException;