From 64d95d7f3ebd7083fabc04819122183fd75c45eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Mon, 19 Jan 2026 17:10:15 +0100 Subject: [PATCH] Extract code snippets from mvc-servlet.adoc Closes gh-36175 --- .../ROOT/pages/web/webmvc/mvc-servlet.adoc | 84 +------------------ .../docs/web/webmvc/mvcservlet/AppConfig.java | 23 +++++ .../MyWebApplicationInitializer.java | 43 ++++++++++ .../docs/web/webmvc/mvcservlet/AppConfig.kt | 22 +++++ .../mvcservlet/MyWebApplicationInitializer.kt | 40 +++++++++ .../MyWebApplicationInitializer.xml | 29 +++++++ 6 files changed, 161 insertions(+), 80 deletions(-) create mode 100644 framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/AppConfig.java create mode 100644 framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/MyWebApplicationInitializer.java create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/AppConfig.kt create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/MyWebApplicationInitializer.kt create mode 100644 framework-docs/src/main/resources/org/springframework/docs/web/webmvc/mvcservlet/MyWebApplicationInitializer.xml diff --git a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-servlet.adoc b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-servlet.adoc index 34d9fd30c4f..1374c3dd338 100644 --- a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-servlet.adoc +++ b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-servlet.adoc @@ -14,55 +14,12 @@ In turn, the `DispatcherServlet` uses Spring configuration to discover the delegate components it needs for request mapping, view resolution, exception handling, xref:web/webmvc/mvc-servlet/special-bean-types.adoc[and more]. -The following example of the Java configuration registers and initializes +The following example shows the programmatic registration and initialization of the `DispatcherServlet`, which is auto-detected by the Servlet container -(see xref:web/webmvc/mvc-servlet/container-config.adoc[Servlet Config]): +(see xref:web/webmvc/mvc-servlet/container-config.adoc[Servlet Config]), and the +equivalent `web.xml`: -[tabs] -====== -Java:: -+ -[source,java,indent=0,subs="verbatim,quotes"] ----- - public class MyWebApplicationInitializer implements WebApplicationInitializer { - - @Override - public void onStartup(ServletContext servletContext) { - - // Load Spring web application configuration - AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); - context.register(AppConfig.class); - - // Create and register the DispatcherServlet - DispatcherServlet servlet = new DispatcherServlet(context); - ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet); - registration.setLoadOnStartup(1); - registration.addMapping("/app/*"); - } - } ----- - -Kotlin:: -+ -[source,kotlin,indent=0,subs="verbatim,quotes"] ----- - class MyWebApplicationInitializer : WebApplicationInitializer { - - override fun onStartup(servletContext: ServletContext) { - - // Load Spring web application configuration - val context = AnnotationConfigWebApplicationContext() - context.register(AppConfig::class.java) - - // Create and register the DispatcherServlet - val servlet = DispatcherServlet(context) - val registration = servletContext.addServlet("app", servlet) - registration.setLoadOnStartup(1) - registration.addMapping("/app/*") - } - } ----- -====== +include-code::./MyWebApplicationInitializer[tag=snippet,indent=0] NOTE: In addition to using the ServletContext API directly, you can also extend `AbstractAnnotationConfigDispatcherServletInitializer` and override specific methods @@ -73,39 +30,6 @@ alternative to `AnnotationConfigWebApplicationContext`. See the {spring-framework-api}/web/context/support/GenericWebApplicationContext.html[`GenericWebApplicationContext`] javadoc for details. -The following example of `web.xml` configuration registers and initializes the `DispatcherServlet`: - -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - org.springframework.web.context.ContextLoaderListener - - - - contextConfigLocation - /WEB-INF/app-context.xml - - - - app - org.springframework.web.servlet.DispatcherServlet - - contextConfigLocation - - - 1 - - - - app - /app/* - - - ----- - NOTE: Spring Boot follows a different initialization sequence. Rather than hooking into the lifecycle of the Servlet container, Spring Boot uses Spring configuration to bootstrap itself and the embedded Servlet container. `Filter` and `Servlet` declarations diff --git a/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/AppConfig.java b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/AppConfig.java new file mode 100644 index 00000000000..bd16e7ec218 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/AppConfig.java @@ -0,0 +1,23 @@ +/* + * Copyright 2002-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.web.webmvc.mvcservlet; + +import org.springframework.context.annotation.Configuration; + +@Configuration +public class AppConfig { +} diff --git a/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/MyWebApplicationInitializer.java b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/MyWebApplicationInitializer.java new file mode 100644 index 00000000000..ceb168b45a8 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/MyWebApplicationInitializer.java @@ -0,0 +1,43 @@ +/* + * Copyright 2002-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.web.webmvc.mvcservlet; + +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletRegistration; + +import org.springframework.web.WebApplicationInitializer; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.servlet.DispatcherServlet; + +// tag::snippet[] +public class MyWebApplicationInitializer implements WebApplicationInitializer { + + @Override + public void onStartup(ServletContext servletContext) { + + // Load Spring web application configuration + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); + context.register(AppConfig.class); + + // Create and register the DispatcherServlet + DispatcherServlet servlet = new DispatcherServlet(context); + ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet); + registration.setLoadOnStartup(1); + registration.addMapping("/app/*"); + } +} +// end::snippet[] diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/AppConfig.kt b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/AppConfig.kt new file mode 100644 index 00000000000..36ea83dd985 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/AppConfig.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2002-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.web.webmvc.mvcservlet + +import org.springframework.context.annotation.Configuration + +@Configuration +class AppConfig diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/MyWebApplicationInitializer.kt b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/MyWebApplicationInitializer.kt new file mode 100644 index 00000000000..96c584a0c8e --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/MyWebApplicationInitializer.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2002-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.web.webmvc.mvcservlet + +import jakarta.servlet.ServletContext +import org.springframework.web.WebApplicationInitializer +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext +import org.springframework.web.servlet.DispatcherServlet + +// tag::snippet[] +class MyWebApplicationInitializer : WebApplicationInitializer { + + override fun onStartup(servletContext: ServletContext) { + + // Load Spring web application configuration + val context = AnnotationConfigWebApplicationContext() + context.register(AppConfig::class.java) + + // Create and register the DispatcherServlet + val servlet = DispatcherServlet(context) + val registration = servletContext.addServlet("app", servlet) + registration.setLoadOnStartup(1) + registration.addMapping("/app/*") + } +} +// end::snippet[] diff --git a/framework-docs/src/main/resources/org/springframework/docs/web/webmvc/mvcservlet/MyWebApplicationInitializer.xml b/framework-docs/src/main/resources/org/springframework/docs/web/webmvc/mvcservlet/MyWebApplicationInitializer.xml new file mode 100644 index 00000000000..47ea2fd3e3a --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/web/webmvc/mvcservlet/MyWebApplicationInitializer.xml @@ -0,0 +1,29 @@ + + + + + org.springframework.web.context.ContextLoaderListener + + + + contextConfigLocation + /WEB-INF/app-context.xml + + + + app + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + + + 1 + + + + app + /app/* + + + +