diff --git a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-servlet/container-config.adoc b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-servlet/container-config.adoc index 2ae4827d533..ae6f4eb34e7 100644 --- a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-servlet/container-config.adoc +++ b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-servlet/container-config.adoc @@ -5,48 +5,7 @@ In a Servlet environment, you have the option of configuring the Servlet contain programmatically as an alternative or in combination with a `web.xml` file. The following example registers a `DispatcherServlet`: -[tabs] -====== -Java:: -+ -[source,java,indent=0,subs="verbatim,quotes"] ----- - 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("/"); - } - } ----- - -Kotlin:: -+ -[source,kotlin,indent=0,subs="verbatim,quotes"] ----- - import org.springframework.web.WebApplicationInitializer - - class MyWebApplicationInitializer : WebApplicationInitializer { - - override fun onStartup(container: ServletContext) { - val appContext = XmlWebApplicationContext() - appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml") - - val registration = container.addServlet("dispatcher", DispatcherServlet(appContext)) - registration.setLoadOnStartup(1) - registration.addMapping("/") - } - } ----- -====== - +include-code::./MyWebApplicationInitializer[tag=snippet,indent=0] `WebApplicationInitializer` is an interface provided by Spring MVC that ensures your implementation is detected and automatically used to initialize any Servlet 3 container. @@ -55,144 +14,21 @@ An abstract base class implementation of `WebApplicationInitializer` named `DispatcherServlet` by overriding methods to specify the servlet mapping and the location of the `DispatcherServlet` configuration. -This is recommended for applications that use Java-based Spring configuration, as the +This is recommended for applications that use programmatic Spring configuration, as the following example shows: -[tabs] -====== -Java:: -+ -[source,java,indent=0,subs="verbatim,quotes"] ----- - 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[] { "/" }; - } - } ----- - -Kotlin:: -+ -[source,kotlin,indent=0,subs="verbatim,quotes"] ----- - class MyWebAppInitializer : AbstractAnnotationConfigDispatcherServletInitializer() { - - override fun getRootConfigClasses(): Array>? { - return null - } - - override fun getServletConfigClasses(): Array>? { - return arrayOf(MyWebConfig::class.java) - } - - override fun getServletMappings(): Array { - return arrayOf("/") - } - } ----- -====== +include-code::./MyWebAppInitializer[tag=snippet,indent=0] If you use XML-based Spring configuration, you should extend directly from `AbstractDispatcherServletInitializer`, as the following example shows: -[tabs] -====== -Java:: -+ -[source,java,indent=0,subs="verbatim,quotes"] ----- - 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[] { "/" }; - } - } ----- - -Kotlin:: -+ -[source,kotlin,indent=0,subs="verbatim,quotes"] ----- - class MyWebAppInitializer : AbstractDispatcherServletInitializer() { - - override fun createRootApplicationContext(): WebApplicationContext? { - return null - } - - override fun createServletApplicationContext(): WebApplicationContext { - return XmlWebApplicationContext().apply { - setConfigLocation("/WEB-INF/spring/dispatcher-config.xml") - } - } - - override fun getServletMappings(): Array { - return arrayOf("/") - } - } ----- -====== +include-code::./MyXmlDispatcherServletInitializer[tag=snippet,indent=0] `AbstractDispatcherServletInitializer` also provides a convenient way to add `Filter` instances and have them be automatically mapped to the `DispatcherServlet`, as the following example shows: -[tabs] -====== -Java:: -+ -[source,java,indent=0,subs="verbatim,quotes"] ----- - public class MyWebAppInitializer extends AbstractDispatcherServletInitializer { - - // ... - - @Override - protected Filter[] getServletFilters() { - return new Filter[] { - new HiddenHttpMethodFilter(), new CharacterEncodingFilter() }; - } - } ----- - -Kotlin:: -+ -[source,kotlin,indent=0,subs="verbatim,quotes"] ----- - class MyWebAppInitializer : AbstractDispatcherServletInitializer() { - - // ... - - override fun getServletFilters(): Array { - return arrayOf(HiddenHttpMethodFilter(), CharacterEncodingFilter()) - } - } ----- -====== +include-code::./MyFilterDispatcherServletInitializer[tag=snippet,indent=0] Each filter is added with a default name based on its concrete type and automatically mapped to the `DispatcherServlet`. diff --git a/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyFilterDispatcherServletInitializer.java b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyFilterDispatcherServletInitializer.java new file mode 100644 index 00000000000..2c52f38ad86 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyFilterDispatcherServletInitializer.java @@ -0,0 +1,53 @@ +/* + * 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.mvccontainerconfig; + +import jakarta.servlet.Filter; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.AbstractDispatcherServletInitializer; +import org.springframework.web.filter.CharacterEncodingFilter; +import org.springframework.web.filter.HiddenHttpMethodFilter; + +// tag::snippet[] +public class MyFilterDispatcherServletInitializer extends AbstractDispatcherServletInitializer { + + // ... + + @Override + protected Filter[] getServletFilters() { + return new Filter[] { + new HiddenHttpMethodFilter(), new CharacterEncodingFilter() }; + } + + // @fold:on + @Override + protected WebApplicationContext createServletApplicationContext() { + return null; + } + + @Override + protected String[] getServletMappings() { + return new String[] { "/" }; + } + + @Override + protected WebApplicationContext createRootApplicationContext() { + return null; + } + // @fold:off +} +// end::snippet[] diff --git a/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyWebAppInitializer.java b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyWebAppInitializer.java new file mode 100644 index 00000000000..b6a5a21b83d --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyWebAppInitializer.java @@ -0,0 +1,41 @@ +/* + * 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.mvccontainerconfig; + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +// tag::snippet[] +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[] { "/" }; + } +} +// end::snippet[] + +class MyWebConfig {} diff --git a/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyWebApplicationInitializer.java b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyWebApplicationInitializer.java new file mode 100644 index 00000000000..3ee45306bcb --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyWebApplicationInitializer.java @@ -0,0 +1,38 @@ +/* + * 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.mvccontainerconfig; + +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletRegistration; +import org.springframework.web.WebApplicationInitializer; +import org.springframework.web.context.support.XmlWebApplicationContext; +import org.springframework.web.servlet.DispatcherServlet; + +// tag::snippet[] +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("/"); + } +} +// end::snippet[] diff --git a/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyXmlDispatcherServletInitializer.java b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyXmlDispatcherServletInitializer.java new file mode 100644 index 00000000000..86a216682e5 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyXmlDispatcherServletInitializer.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.mvccontainerconfig; + +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.AbstractDispatcherServletInitializer; +import org.springframework.web.context.support.XmlWebApplicationContext; + +// tag::snippet[] +public class MyXmlDispatcherServletInitializer 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[] { "/" }; + } +} +// end::snippet[] diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyFilterDispatcherServletInitializer.kt b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyFilterDispatcherServletInitializer.kt new file mode 100644 index 00000000000..22042a211a7 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyFilterDispatcherServletInitializer.kt @@ -0,0 +1,48 @@ +/* + * 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.mvccontainerconfig + +import jakarta.servlet.Filter +import org.springframework.web.context.WebApplicationContext +import org.springframework.web.context.support.AbstractDispatcherServletInitializer +import org.springframework.web.filter.CharacterEncodingFilter +import org.springframework.web.filter.HiddenHttpMethodFilter + +// tag::snippet[] +class MyFilterDispatcherServletInitializer : AbstractDispatcherServletInitializer() { + + // ... + + override fun getServletFilters(): Array { + return arrayOf(HiddenHttpMethodFilter(), CharacterEncodingFilter()) + } + + // @fold:on + override fun createServletApplicationContext(): WebApplicationContext { + TODO("Not yet implemented") + } + + override fun getServletMappings(): Array { + return arrayOf("/") + } + + override fun createRootApplicationContext(): WebApplicationContext? { + return null + } + // @fold:off +} +// end::snippet[] diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyWebAppInitializer.kt b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyWebAppInitializer.kt new file mode 100644 index 00000000000..e3f0b935c71 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyWebAppInitializer.kt @@ -0,0 +1,38 @@ +/* + * 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.mvccontainerconfig + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer + +// tag::snippet[] +class MyWebAppInitializer : AbstractAnnotationConfigDispatcherServletInitializer() { + + override fun getRootConfigClasses(): Array>? { + return null + } + + override fun getServletConfigClasses(): Array>? { + return arrayOf(MyWebConfig::class.java) + } + + override fun getServletMappings(): Array { + return arrayOf("/") + } +} +// end::snippet[] + +class MyWebConfig diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyWebApplicationInitializer.kt b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyWebApplicationInitializer.kt new file mode 100644 index 00000000000..22939cc58bf --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyWebApplicationInitializer.kt @@ -0,0 +1,36 @@ +/* + * 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.mvccontainerconfig + +import jakarta.servlet.ServletContext +import org.springframework.web.WebApplicationInitializer +import org.springframework.web.context.support.XmlWebApplicationContext +import org.springframework.web.servlet.DispatcherServlet + +// tag::snippet[] +class MyWebApplicationInitializer : WebApplicationInitializer { + + override fun onStartup(container: ServletContext) { + val appContext = XmlWebApplicationContext() + appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml") + + val registration = container.addServlet("dispatcher", DispatcherServlet(appContext)) + registration.setLoadOnStartup(1) + registration.addMapping("/") + } +} +// end::snippet[] diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyXmlDispatcherServletInitializer.kt b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyXmlDispatcherServletInitializer.kt new file mode 100644 index 00000000000..a63e1aadfac --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyXmlDispatcherServletInitializer.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.mvccontainerconfig + +import org.springframework.web.context.WebApplicationContext +import org.springframework.web.context.support.AbstractDispatcherServletInitializer +import org.springframework.web.context.support.XmlWebApplicationContext + +// tag::snippet[] +class MyXmlDispatcherServletInitializer : AbstractDispatcherServletInitializer() { + + override fun createRootApplicationContext(): WebApplicationContext? { + return null + } + + override fun createServletApplicationContext(): WebApplicationContext { + return XmlWebApplicationContext().apply { + setConfigLocation("/WEB-INF/spring/dispatcher-config.xml") + } + } + + override fun getServletMappings(): Array { + return arrayOf("/") + } +} +// end::snippet[]