From 3c3432620806ba3ecbe8ef6a3736276bc0e45151 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Sat, 1 Jun 2013 14:43:02 +0100 Subject: [PATCH] [bs-141] Add Bootstrap WebApplicationInitializer got traditional wars [#50806851] [bs-141] First class escape hatch from jar to war for web applications --- pom.xml | 4 ++ .../properties/ServerProperties.java | 4 ++ .../web/BootstrapServletInitializer.java | 55 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 spring-bootstrap/src/main/java/org/springframework/bootstrap/web/BootstrapServletInitializer.java diff --git a/pom.xml b/pom.xml index 150c6732a61..df6813dbc46 100644 --- a/pom.xml +++ b/pom.xml @@ -219,6 +219,10 @@ maven-exec-plugin 1.2.1 + + maven-war-plugin + 2.3 + diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/properties/ServerProperties.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/properties/ServerProperties.java index 5efa8fd58e7..5a26e7c2f4e 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/properties/ServerProperties.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/properties/ServerProperties.java @@ -68,6 +68,10 @@ public class ServerProperties { this.address = address; } + public void setLoader(String value) { + // no op + } + public static class Tomcat { private String accessLogPattern; diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/web/BootstrapServletInitializer.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/web/BootstrapServletInitializer.java new file mode 100644 index 00000000000..07313856105 --- /dev/null +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/web/BootstrapServletInitializer.java @@ -0,0 +1,55 @@ +package org.springframework.bootstrap.web; + +import javax.servlet.Filter; + +import org.springframework.util.ClassUtils; +import org.springframework.web.WebApplicationInitializer; +import org.springframework.web.filter.DelegatingFilterProxy; +import org.springframework.web.servlet.FrameworkServlet; +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +/** + *

+ * A handy opinionated {@link WebApplicationInitializer} for applications that only have + * one Spring servlet, and no more than a single filter (which itself is only enabled when + * Spring Security is detected). If your application is more complicated consider using + * one of the other WebApplicationInitializers. + *

+ * + *

+ * Note that a WebApplicationInitializer is only needed if you are building a war file and + * deploying it. If you prefer to run an embedded container (we do) then you won't need + * this at all. + *

+ * + * @author Dave Syer + * + */ +public abstract class BootstrapServletInitializer extends + AbstractAnnotationConfigDispatcherServletInitializer { + + @Override + protected Class[] getRootConfigClasses() { + return null; + } + + @Override + protected String[] getServletMappings() { + return new String[] { "/" }; + } + + @Override + protected Filter[] getServletFilters() { + if (ClassUtils.isPresent( + "org.springframework.security.config.annotation.web.EnableWebSecurity", + null)) { + DelegatingFilterProxy filter = new DelegatingFilterProxy( + "springSecurityFilterChain"); + filter.setContextAttribute(FrameworkServlet.SERVLET_CONTEXT_PREFIX + + "dispatcher"); + return new Filter[] { filter }; + } + return new Filter[0]; + } + +}