├── .gitignore ├── README.md ├── pom.xml └── src └── main └── java └── eu └── kielczewski └── example ├── config ├── AppConfig.java └── WebMvcConfig.java ├── controller └── IndexController.java └── initializer └── AppInitializer.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | target 3 | *.iml 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Spring MVC Initializer Example 2 | ============================== 3 | 4 | Basic Spring MVC 4.0.x application using `WebApplicationInitializer` instead of `web.xml`. 5 | 6 | Check out [kielczewski.eu/blog](http://kielczewski.eu/blog)! 7 | 8 | Requirements 9 | ------------ 10 | * [Java Platform (JDK) 7](http://www.oracle.com/technetwork/java/javase/downloads/index.html) 11 | * [Apache Maven 3.x](http://maven.apache.org/) 12 | 13 | Quick start 14 | ----------- 15 | 1. `mvn jetty:run` 16 | 2. Point your browser to [http://localhost:8080/](http://localhost:8080/) -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | eu.kielczewski.example.spring 6 | example-spring-initializer 7 | 1.0-SNAPSHOT 8 | war 9 | 10 | Spring MVC Initializer Example 11 | http://kielczewski.eu 12 | 13 | 14 | 4.0.2.RELEASE 15 | UTF-8 16 | UTF-8 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | org.springframework 25 | spring-webmvc 26 | ${spring.version} 27 | 28 | 29 | 30 | 31 | 32 | javax.servlet 33 | javax.servlet-api 34 | 3.0.1 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | maven-compiler-plugin 46 | 3.1 47 | 48 | 1.7 49 | 1.7 50 | 51 | 52 | 53 | 54 | 55 | 56 | maven-war-plugin 57 | 2.4 58 | 59 | false 60 | 61 | 62 | 63 | 64 | 65 | 66 | org.eclipse.jetty 67 | jetty-maven-plugin 68 | 9.0.6.v20130930 69 | 70 | 71 | 8080 72 | localhost 73 | 74 | 10 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /src/main/java/eu/kielczewski/example/config/AppConfig.java: -------------------------------------------------------------------------------- 1 | package eu.kielczewski.example.config; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | @Configuration 7 | @ComponentScan(basePackages = "eu.kielczewski.example") 8 | class AppConfig { 9 | } -------------------------------------------------------------------------------- /src/main/java/eu/kielczewski/example/config/WebMvcConfig.java: -------------------------------------------------------------------------------- 1 | package eu.kielczewski.example.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.web.servlet.config.annotation.EnableWebMvc; 5 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 6 | 7 | @EnableWebMvc 8 | @Configuration 9 | class WebMvcConfig extends WebMvcConfigurerAdapter { 10 | } -------------------------------------------------------------------------------- /src/main/java/eu/kielczewski/example/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package eu.kielczewski.example.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RequestMethod; 6 | import org.springframework.web.bind.annotation.ResponseBody; 7 | 8 | @Controller 9 | class IndexController { 10 | 11 | @SuppressWarnings("SameReturnValue") 12 | @RequestMapping(value = "/", method = RequestMethod.GET) 13 | @ResponseBody 14 | public String showIndex() { 15 | return "Hello world"; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/eu/kielczewski/example/initializer/AppInitializer.java: -------------------------------------------------------------------------------- 1 | package eu.kielczewski.example.initializer; 2 | 3 | import org.springframework.web.WebApplicationInitializer; 4 | import org.springframework.web.context.ContextLoaderListener; 5 | import org.springframework.web.context.WebApplicationContext; 6 | import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 7 | import org.springframework.web.servlet.DispatcherServlet; 8 | 9 | import javax.servlet.ServletContext; 10 | import javax.servlet.ServletException; 11 | import javax.servlet.ServletRegistration; 12 | 13 | public class AppInitializer implements WebApplicationInitializer { 14 | 15 | private static final String CONFIG_LOCATION = "eu.kielczewski.example.config"; 16 | private static final String MAPPING_URL = "/*"; 17 | 18 | @Override 19 | public void onStartup(ServletContext servletContext) throws ServletException { 20 | WebApplicationContext context = getContext(); 21 | servletContext.addListener(new ContextLoaderListener(context)); 22 | ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context)); 23 | dispatcher.setLoadOnStartup(1); 24 | dispatcher.addMapping(MAPPING_URL); 25 | } 26 | 27 | private AnnotationConfigWebApplicationContext getContext() { 28 | AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); 29 | context.setConfigLocation(CONFIG_LOCATION); 30 | return context; 31 | } 32 | 33 | } --------------------------------------------------------------------------------