From 1b8a00e6d4fe87a0592808b46a8aa383fbf31f9f Mon Sep 17 00:00:00 2001 From: Alessio Stalla Date: Wed, 9 May 2018 14:17:45 +0200 Subject: [PATCH] BAEL-82 (#4198) * BAEL-82 revised example code * Fix the build * Modified example * Clarified commented out sections of the code. --- ...BasedApplicationAndServletInitializer.java | 24 +++++++---- .../config/ApplicationInitializer.java | 19 +++++---- ...BasedApplicationAndServletInitializer.java | 32 +++++++++++++++ .../contexts/normal/HelloWorldController.java | 2 +- .../secure/HelloWorldSecureController.java | 2 +- .../config/StudentControllerConfig.java | 9 ++-- .../spring/config/MainWebAppInitializer.java | 9 ++-- .../webapp/WEB-INF/normal-webapp-servlet.xml | 11 ++--- .../webapp/WEB-INF/secure-webapp-servlet.xml | 11 ++--- spring-all/src/main/webapp/WEB-INF/web.xml | 4 +- .../src/main/webapp/WEB-INF/web_old.xml | 41 ------------------- 11 files changed, 84 insertions(+), 80 deletions(-) create mode 100644 spring-all/src/main/java/com/baeldung/contexts/config/SecureAnnotationsBasedApplicationAndServletInitializer.java delete mode 100644 spring-all/src/main/webapp/WEB-INF/web_old.xml 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 = "
" + "

" + greeterService.greet() + "

"; + String message = "
" + "

Normal " + greeterService.greet() + "

"; return new ModelAndView("welcome", "message", message); } } diff --git a/spring-all/src/main/java/com/baeldung/contexts/secure/HelloWorldSecureController.java b/spring-all/src/main/java/com/baeldung/contexts/secure/HelloWorldSecureController.java index b46ace91ae..4ebf2d55e0 100644 --- a/spring-all/src/main/java/com/baeldung/contexts/secure/HelloWorldSecureController.java +++ b/spring-all/src/main/java/com/baeldung/contexts/secure/HelloWorldSecureController.java @@ -43,7 +43,7 @@ public class HelloWorldSecureController { @RequestMapping(path = "/welcome") public ModelAndView helloWorld() { processContext(); - String message = "
" + "

" + greeterService.greet() + "

"; + String message = "
" + "

Secure " + greeterService.greet() + "

"; return new ModelAndView("welcome", "message", message); } } diff --git a/spring-all/src/main/java/org/baeldung/controller/config/StudentControllerConfig.java b/spring-all/src/main/java/org/baeldung/controller/config/StudentControllerConfig.java index ec6cf19785..3dc4db53c0 100644 --- a/spring-all/src/main/java/org/baeldung/controller/config/StudentControllerConfig.java +++ b/spring-all/src/main/java/org/baeldung/controller/config/StudentControllerConfig.java @@ -19,10 +19,13 @@ public class StudentControllerConfig implements WebApplicationInitializer { root.setServletContext(sc); - // Manages the lifecycle of the root application context - sc.addListener(new ContextLoaderListener(root)); + //Manages the lifecycle of the root application context. + //Conflicts with other root contexts in the application, so we've manually set the parent below. + //sc.addListener(new ContextLoaderListener(root)); - DispatcherServlet dv = new DispatcherServlet(new GenericWebApplicationContext()); + GenericWebApplicationContext webApplicationContext = new GenericWebApplicationContext(); + webApplicationContext.setParent(root); + DispatcherServlet dv = new DispatcherServlet(webApplicationContext); ServletRegistration.Dynamic appServlet = sc.addServlet("test-mvc", dv); appServlet.setLoadOnStartup(1); diff --git a/spring-all/src/main/java/org/baeldung/spring/config/MainWebAppInitializer.java b/spring-all/src/main/java/org/baeldung/spring/config/MainWebAppInitializer.java index 5ef83b8afd..a857783c60 100644 --- a/spring-all/src/main/java/org/baeldung/spring/config/MainWebAppInitializer.java +++ b/spring-all/src/main/java/org/baeldung/spring/config/MainWebAppInitializer.java @@ -26,11 +26,14 @@ public class MainWebAppInitializer implements WebApplicationInitializer { root.scan("org.baeldung.spring.config"); // root.getEnvironment().setDefaultProfiles("embedded"); - // Manages the lifecycle of the root application context - sc.addListener(new ContextLoaderListener(root)); + //Manages the lifecycle of the root application context. + //Conflicts with other root contexts in the application, so we've manually set the parent below. + //sc.addListener(new ContextLoaderListener(root)); // Handles requests into the application - final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext())); + GenericWebApplicationContext webApplicationContext = new GenericWebApplicationContext(); + webApplicationContext.setParent(root); + final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(webApplicationContext)); appServlet.setLoadOnStartup(1); final Set mappingConflicts = appServlet.addMapping("/"); if (!mappingConflicts.isEmpty()) { diff --git a/spring-all/src/main/webapp/WEB-INF/normal-webapp-servlet.xml b/spring-all/src/main/webapp/WEB-INF/normal-webapp-servlet.xml index d358e2d62b..0a7a0919a8 100644 --- a/spring-all/src/main/webapp/WEB-INF/normal-webapp-servlet.xml +++ b/spring-all/src/main/webapp/WEB-INF/normal-webapp-servlet.xml @@ -1,12 +1,9 @@ diff --git a/spring-all/src/main/webapp/WEB-INF/secure-webapp-servlet.xml b/spring-all/src/main/webapp/WEB-INF/secure-webapp-servlet.xml index 5bca724670..6cdd3971bf 100644 --- a/spring-all/src/main/webapp/WEB-INF/secure-webapp-servlet.xml +++ b/spring-all/src/main/webapp/WEB-INF/secure-webapp-servlet.xml @@ -1,12 +1,9 @@ diff --git a/spring-all/src/main/webapp/WEB-INF/web.xml b/spring-all/src/main/webapp/WEB-INF/web.xml index 55c2ccd62a..42020e75c4 100644 --- a/spring-all/src/main/webapp/WEB-INF/web.xml +++ b/spring-all/src/main/webapp/WEB-INF/web.xml @@ -8,8 +8,8 @@ application initializers. --> - - + + - - contextClass - - org.springframework.web.context.support.AnnotationConfigWebApplicationContext - - - - contextConfigLocation - org.baeldung.spring.web.config - - - - org.springframework.web.context.ContextLoaderListener - - - - - mvc - org.springframework.web.servlet.DispatcherServlet - 1 - - - mvc - / - - - - index.html - - - \ No newline at end of file