6 changed files with 161 additions and 80 deletions
@ -0,0 +1,23 @@
@@ -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 { |
||||
} |
||||
@ -0,0 +1,43 @@
@@ -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[]
|
||||
@ -0,0 +1,22 @@
@@ -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 |
||||
@ -0,0 +1,40 @@
@@ -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[] |
||||
@ -0,0 +1,29 @@
@@ -0,0 +1,29 @@
|
||||
<!-- tag::snippet[] --> |
||||
<web-app> |
||||
|
||||
<listener> |
||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> |
||||
</listener> |
||||
|
||||
<context-param> |
||||
<param-name>contextConfigLocation</param-name> |
||||
<param-value>/WEB-INF/app-context.xml</param-value> |
||||
</context-param> |
||||
|
||||
<servlet> |
||||
<servlet-name>app</servlet-name> |
||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> |
||||
<init-param> |
||||
<param-name>contextConfigLocation</param-name> |
||||
<param-value></param-value> |
||||
</init-param> |
||||
<load-on-startup>1</load-on-startup> |
||||
</servlet> |
||||
|
||||
<servlet-mapping> |
||||
<servlet-name>app</servlet-name> |
||||
<url-pattern>/app/*</url-pattern> |
||||
</servlet-mapping> |
||||
|
||||
</web-app> |
||||
<!-- end::snippet[] --> |
||||
Loading…
Reference in new issue