From 58612d0fb87459feaf8c10ea4cc7234bea14d150 Mon Sep 17 00:00:00 2001 From: Alessio Stalla Date: Fri, 4 May 2018 21:30:53 +0200 Subject: [PATCH] BAEL-82 (#4109) * BAEL-82 revised example code * Fix the build --- spring-all/pom.xml | 1 + ...BasedApplicationAndServletInitializer.java | 28 ++++++++ ...nnotationsBasedApplicationInitializer.java | 16 +++++ .../config/ApplicationInitializer.java | 24 +++---- .../src/main/webapp/WEB-INF/greeting.xml | 11 +++ .../src/main/webapp/WEB-INF/mvc-servlet.xml | 6 -- .../webapp/WEB-INF/rootApplicationContext.xml | 4 +- spring-all/src/main/webapp/WEB-INF/web.xml | 72 ++++++++++++------- 8 files changed, 115 insertions(+), 47 deletions(-) create mode 100644 spring-all/src/main/java/com/baeldung/contexts/config/AnnotationsBasedApplicationAndServletInitializer.java create mode 100644 spring-all/src/main/java/com/baeldung/contexts/config/AnnotationsBasedApplicationInitializer.java create mode 100644 spring-all/src/main/webapp/WEB-INF/greeting.xml delete mode 100644 spring-all/src/main/webapp/WEB-INF/mvc-servlet.xml diff --git a/spring-all/pom.xml b/spring-all/pom.xml index ac66ecdad8..f9ced963e7 100644 --- a/spring-all/pom.xml +++ b/spring-all/pom.xml @@ -170,6 +170,7 @@ org.springframework.boot spring-boot-starter-thymeleaf + ${org.springframework.version} 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 new file mode 100644 index 0000000000..8ec35515a3 --- /dev/null +++ b/spring-all/src/main/java/com/baeldung/contexts/config/AnnotationsBasedApplicationAndServletInitializer.java @@ -0,0 +1,28 @@ +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; + +public class AnnotationsBasedApplicationAndServletInitializer extends AbstractDispatcherServletInitializer { + + @Override + protected WebApplicationContext createRootApplicationContext() { + AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); + rootContext.register(RootApplicationConfig.class); + return rootContext; + } + + @Override + protected WebApplicationContext createServletApplicationContext() { + AnnotationConfigWebApplicationContext secureWebAppContext = new AnnotationConfigWebApplicationContext(); + secureWebAppContext.register(SecureWebAppConfig.class); + return secureWebAppContext; + } + + @Override + protected String[] getServletMappings() { + return new String[] { "/s/api/*" }; + } +} diff --git a/spring-all/src/main/java/com/baeldung/contexts/config/AnnotationsBasedApplicationInitializer.java b/spring-all/src/main/java/com/baeldung/contexts/config/AnnotationsBasedApplicationInitializer.java new file mode 100644 index 0000000000..0d2674d4f3 --- /dev/null +++ b/spring-all/src/main/java/com/baeldung/contexts/config/AnnotationsBasedApplicationInitializer.java @@ -0,0 +1,16 @@ +package com.baeldung.contexts.config; + +import org.springframework.web.context.AbstractContextLoaderInitializer; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; + +public class AnnotationsBasedApplicationInitializer extends AbstractContextLoaderInitializer { + + @Override + protected WebApplicationContext createRootApplicationContext() { + AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); + rootContext.register(RootApplicationConfig.class); + return rootContext; + } + +} \ No newline at end of file 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 c3ff90cf39..babad90598 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 @@ -5,31 +5,31 @@ import javax.servlet.ServletException; import javax.servlet.ServletRegistration; import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.context.support.XmlWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; -@Configuration public class ApplicationInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { - AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); - rootContext.register(RootApplicationConfig.class); - servletContext.addListener(new ContextLoaderListener(rootContext)); + //XML Context + //XmlWebApplicationContext rootContext = new XmlWebApplicationContext(); + //rootContext.setConfigLocations("/WEB-INF/rootApplicationContext.xml"); + //Annotations Context + //AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); + //rootContext.register(RootApplicationConfig.class); + //Registration + //servletContext.addListener(new ContextLoaderListener(rootContext)); - AnnotationConfigWebApplicationContext normalWebAppContext = new AnnotationConfigWebApplicationContext(); - normalWebAppContext.register(NormalWebAppConfig.class); + 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/*"); - - AnnotationConfigWebApplicationContext secureWebAppContext = new AnnotationConfigWebApplicationContext(); - secureWebAppContext.register(SecureWebAppConfig.class); - ServletRegistration.Dynamic secure = servletContext.addServlet("secure-webapp", new DispatcherServlet(secureWebAppContext)); - secure.setLoadOnStartup(1); - secure.addMapping("/s/api/*"); } } diff --git a/spring-all/src/main/webapp/WEB-INF/greeting.xml b/spring-all/src/main/webapp/WEB-INF/greeting.xml new file mode 100644 index 0000000000..3f0ae83455 --- /dev/null +++ b/spring-all/src/main/webapp/WEB-INF/greeting.xml @@ -0,0 +1,11 @@ + + + + + + + + \ No newline at end of file diff --git a/spring-all/src/main/webapp/WEB-INF/mvc-servlet.xml b/spring-all/src/main/webapp/WEB-INF/mvc-servlet.xml deleted file mode 100644 index 4ba9642448..0000000000 --- a/spring-all/src/main/webapp/WEB-INF/mvc-servlet.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/spring-all/src/main/webapp/WEB-INF/rootApplicationContext.xml b/spring-all/src/main/webapp/WEB-INF/rootApplicationContext.xml index cd79c64e79..af03661ebc 100644 --- a/spring-all/src/main/webapp/WEB-INF/rootApplicationContext.xml +++ b/spring-all/src/main/webapp/WEB-INF/rootApplicationContext.xml @@ -10,7 +10,5 @@ - - - + \ No newline at end of file diff --git a/spring-all/src/main/webapp/WEB-INF/web.xml b/spring-all/src/main/webapp/WEB-INF/web.xml index 2050f28f81..55c2ccd62a 100644 --- a/spring-all/src/main/webapp/WEB-INF/web.xml +++ b/spring-all/src/main/webapp/WEB-INF/web.xml @@ -3,37 +3,54 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> + + + - + + + + + - + - + + - test-mvc + normal-webapp-annotations org.springframework.web.servlet.DispatcherServlet + + contextClass + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + contextConfigLocation - /WEB-INF/test-mvc.xml + com.baeldung.contexts.config.NormalWebAppConfig 1 - - test-mvc - /test/* + normal-webapp-annotations + /api-ann/*