Containerless Spring MVC
Many of the new JVM-based web frameworks are ditching containers and WAR files and instead using a WAR-less / Containerless approach. But that doesn’t mean you have to ditch your favorite Java web framework. A while back I posted about going containerless with Tapestry. Now lets do the same with Spring MVC. You can grab the full source code from GitHub.
First we need a build that defines the dependencies. Here is the build.gradle file for my Gradle build:
apply plugin:'java' apply plugin:'application' version = '0.0.1-SNAPSHOT' mainClassName = "com.jamesward.Webapp" applicationName = "webapp" repositories { mavenCentral() } dependencies { compile 'org.springframework:spring-webmvc:3.1.2.RELEASE' compile 'cglib:cglib:2.2.2' compile 'org.eclipse.jetty:jetty-webapp:8.1.5.v20120716' } |
There isn’t much to this build except a few dependencies: Spring MVC, CGLib, and Jetty.
The src/main/resources/assets/index.html file just contains simple HTML:
<!doctype html> <html> <body> hello, world </body> </html> |
The src/main/java/com/jamesward/WebConfig.java file uses Spring annotations to configure Spring MVC:
package com.jamesward; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.*; @Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations("classpath:/assets/"); } @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("redirect:index.html"); } } |
Finally, a simple “static void main” Java class is used to start Jetty. The src/main/java/com/jamesward/Webapp.java file just sets up the HTTP listener and starts it:
package com.jamesward; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; public class Webapp { public static void main(String[] args) throws Exception { final AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext(); applicationContext.register(WebConfig.class); final ServletHolder servletHolder = new ServletHolder(new DispatcherServlet(applicationContext)); final ServletContextHandler context = new ServletContextHandler(); context.setContextPath("/"); context.addServlet(servletHolder, "/*"); String webPort = System.getenv("PORT"); if (webPort == null || webPort.isEmpty()) { webPort = "8080"; } final Server server = new Server(Integer.valueOf(webPort)); server.setHandler(context); server.start(); server.join(); } } |
That’s it! To build and run this project locally you can simple run:
./gradlew run |
(Note: Run “gradlew.bat” on Windows.)
So simple it’s hard to believe it works. :) Let me know if you have any questions.
About James Ward
James Ward (www.jamesward.com) works for Typesafe where he teaches developers the Typesafe Stack (Play Framework, Scala, and Akka) . James frequently presents at conferences around the world such as JavaOne, Devoxx, and many other Java get-togethers. Along with Bruce Eckel, James co-authored First Steps in Flex. He has also published numerous screencasts, blogs, and technical articles. Starting with Pascal and Assembly in the 80′s, James found his passion for writing code. Beginning in the 90′s he began doing web development with HTML, Perl/CGI, then Java. After building a Flex and Java based customer service portal in 2004 for Pillar Data Systems he became a Technical Evangelist for Flex at Adobe. In 2011 James became a Principal Developer Evangelist at Salesforce.com where he taught developers how to deploy apps on the cloud with Heroku. James Tweets as @_JamesWard and posts code at github.com/jamesward.
More About James »Northern Virginia Software Symposium
November 1 - 3, 2013
Reston, VA
Current Topics on the NFJS Tour
- Core Java, JEE
- Dynamic Languages: Groovy, JRuby, Scala, Clojure
- RESTful Web Apps
- Frameworks: Hibernate, Grails, Spring, JSF, GWT, more
- Agility
- Test Driven Design
- Security
- Ajax, Flex, RIA
Why Attend the NFJS Tour?
- » Cutting-Edge Technologies
- » Agile Practices
- » Peer Exchange
Current Topics:
- Languages on the JVM: Scala, Groovy, Clojure
- Enterprise Java
- Core Java, Java 7
- Agility
- Testing: Geb, Spock, Easyb
- REST
- NoSQL: MongoDB, Cassandra
- Hadoop
- Spring 3
- Automation Tools: Git, Hudson, Sonar
- HTML5, Ajax, jQuery, Usability
- Mobile Applications - iPhone and Android
- More...
NFJS, the Magazine
May Issue Now AvailableOn the road to learning
by Raju GandhiRefactoring to Modularity
by Kirk KnoernschildRESTful Groovy
by Kenneth KousenGetting Started with D3.js
by Brian Sletten