diff --git a/spring-all/src/main/java/com/baeldung/contexts/config/AnnotationsBasedApplicationAndServletInitializer.java b/spring-all/src/main/java/com/baeldung/contexts/config/AnnotationsBasedApplicationAndServletInitializer.java
index 8ec35515a3..4df1e2e73b 100644
--- a/spring-all/src/main/java/com/baeldung/contexts/config/AnnotationsBasedApplicationAndServletInitializer.java
+++ b/spring-all/src/main/java/com/baeldung/contexts/config/AnnotationsBasedApplicationAndServletInitializer.java
@@ -1,6 +1,5 @@
package com.baeldung.contexts.config;
-import org.springframework.web.context.AbstractContextLoaderInitializer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
@@ -9,20 +8,29 @@ public class AnnotationsBasedApplicationAndServletInitializer extends AbstractDi
@Override
protected WebApplicationContext createRootApplicationContext() {
- AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
- rootContext.register(RootApplicationConfig.class);
- return rootContext;
+ //If this is not the only class declaring a root context, we return null because it would clash
+ //with other classes, as there can only be a single root context.
+
+ //AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
+ //rootContext.register(RootApplicationConfig.class);
+ //return rootContext;
+ return null;
}
@Override
protected WebApplicationContext createServletApplicationContext() {
- AnnotationConfigWebApplicationContext secureWebAppContext = new AnnotationConfigWebApplicationContext();
- secureWebAppContext.register(SecureWebAppConfig.class);
- return secureWebAppContext;
+ AnnotationConfigWebApplicationContext normalWebAppContext = new AnnotationConfigWebApplicationContext();
+ normalWebAppContext.register(NormalWebAppConfig.class);
+ return normalWebAppContext;
}
@Override
protected String[] getServletMappings() {
- return new String[] { "/s/api/*" };
+ return new String[] { "/api/*" };
+ }
+
+ @Override
+ protected String getServletName() {
+ return "normal-dispatcher";
}
}
diff --git a/spring-all/src/main/java/com/baeldung/contexts/config/ApplicationInitializer.java b/spring-all/src/main/java/com/baeldung/contexts/config/ApplicationInitializer.java
index babad90598..09e0742394 100644
--- a/spring-all/src/main/java/com/baeldung/contexts/config/ApplicationInitializer.java
+++ b/spring-all/src/main/java/com/baeldung/contexts/config/ApplicationInitializer.java
@@ -16,7 +16,11 @@ public class ApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
- //XML Context
+ //Here, we can define a root context and register servlets, among other things.
+ //However, since we've later defined other classes to do the same and they would clash,
+ //we leave this commented out.
+
+ //Root XML Context
//XmlWebApplicationContext rootContext = new XmlWebApplicationContext();
//rootContext.setConfigLocations("/WEB-INF/rootApplicationContext.xml");
//Annotations Context
@@ -24,12 +28,13 @@ public class ApplicationInitializer implements WebApplicationInitializer {
//rootContext.register(RootApplicationConfig.class);
//Registration
//servletContext.addListener(new ContextLoaderListener(rootContext));
-
- XmlWebApplicationContext normalWebAppContext = new XmlWebApplicationContext();
- normalWebAppContext.setConfigLocation("/WEB-INF/normal-webapp-servlet.xml");
- ServletRegistration.Dynamic normal = servletContext.addServlet("normal-webapp", new DispatcherServlet(normalWebAppContext));
- normal.setLoadOnStartup(1);
- normal.addMapping("/api/*");
+
+ //Dispatcher Servlet
+ //XmlWebApplicationContext normalWebAppContext = new XmlWebApplicationContext();
+ //normalWebAppContext.setConfigLocation("/WEB-INF/normal-webapp-servlet.xml");
+ //ServletRegistration.Dynamic normal = servletContext.addServlet("normal-webapp", new DispatcherServlet(normalWebAppContext));
+ //normal.setLoadOnStartup(1);
+ //normal.addMapping("/api/*");
}
}
diff --git a/spring-all/src/main/java/com/baeldung/contexts/config/SecureAnnotationsBasedApplicationAndServletInitializer.java b/spring-all/src/main/java/com/baeldung/contexts/config/SecureAnnotationsBasedApplicationAndServletInitializer.java
new file mode 100644
index 0000000000..89ce0153f5
--- /dev/null
+++ b/spring-all/src/main/java/com/baeldung/contexts/config/SecureAnnotationsBasedApplicationAndServletInitializer.java
@@ -0,0 +1,32 @@
+package com.baeldung.contexts.config;
+
+import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
+import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
+
+public class SecureAnnotationsBasedApplicationAndServletInitializer extends AbstractDispatcherServletInitializer {
+
+ @Override
+ protected WebApplicationContext createRootApplicationContext() {
+ return null;
+ }
+
+ @Override
+ protected WebApplicationContext createServletApplicationContext() {
+ AnnotationConfigWebApplicationContext secureWebAppContext = new AnnotationConfigWebApplicationContext();
+ secureWebAppContext.register(SecureWebAppConfig.class);
+ return secureWebAppContext;
+ }
+
+ @Override
+ protected String[] getServletMappings() {
+ return new String[] { "/s/api/*" };
+ }
+
+
+ @Override
+ protected String getServletName() {
+ return "secure-dispatcher";
+ }
+
+}
diff --git a/spring-all/src/main/java/com/baeldung/contexts/normal/HelloWorldController.java b/spring-all/src/main/java/com/baeldung/contexts/normal/HelloWorldController.java
index dd70ce8e97..fbe3dfb398 100644
--- a/spring-all/src/main/java/com/baeldung/contexts/normal/HelloWorldController.java
+++ b/spring-all/src/main/java/com/baeldung/contexts/normal/HelloWorldController.java
@@ -35,7 +35,7 @@ public class HelloWorldController {
@RequestMapping(path = "/welcome")
public ModelAndView helloWorld() {
processContext();
- String message = "