Spring web framework 4 was release last December and I have been very excited about its ability to build completely XML free applications. I always like most part of the Spring framework, especially its powerful and flexible ability to glue together various modules. However I am not a fan of the XML configuration ways used by the old Spring.
Recently I had an opportunity to write a simple app demo with DI/AOP/MVC so I decided to take the XML free approach. The application can be found from GitHub and some highlights are listed below.
XML Free Features
- Spring-Boot is used to provide a standalone web application with embedded Tomcat server. The PuzzleApplication is annotated with @EnableAutoConfiguration and calls SpringApplication.run() in its main method to take advantage of the automation and magic provided by Spring-Boot.
- The bean containers used for dependency injection is not using XML anymore thanks to the @ComponentScan in the PuzzleSolvingWebMvcConfig class, which instructs Spring to automatically scan the classes for injectable classes annotated with @Component @Repository @Controller @Service or @Aspect. Other injectable beans are created in the classes annotated with @Configuration.
- The @EnableWebMvc in the same PuzzleSolvingWebMvcConfig class eliminates the needs for XML to config the view revolvers.
- JSR303 Java validation is used for server side bean validation (especially the class level cross-field validation was done via ConstraintValidator and @Constraint). The Hibernate-validator is injected into the validation through LocalValidatorFactoryBean. Because the injection of LocalValidatorFactoryBean is explicitly declared in a @Configuration class, so no XML is needed.
- AOP is demonstrated without XML by using AspectJ annotations for producing logs. Two examplar join-points are defined by using customized point cut annotation and “execution”. And @Before, @Around, @AfterReturning and @AfterThrowing advices are demonstrated without XML.
Some other highlights (and potential improvements) not related to the XML topic are:
- The view implementation uses Spring MVC with the old JSP/JSTL as the view layer. I am about to provide a Thymeleaf based implementation soon since I like its clean HTML semantics. And I believe HTML5 can be seamlessly integrated into both JSP and Thymeleaf in Spring 4.
- As usual, JQuery is used to implement the AJAX communication with server in the JSP file. The Spring form data in the AJAX payload is serialized in JSON format.
- The controller class demonstrates three type of request/response handlings. The first is returning a ModelAndView, the second is returning a @ResponseBody and the third is using @RequestMapping(“**”) to handle illegal request urls.
It is also worthwhile to mention that most of the classes are unit tested. Especially an integration test is provided based on Selenium at the Maven integration-test phase.
How to Run
- Java 7+ and Maven are required
- Download or checkout source code from GitHub
- Execute “mvn clean package” to compile, unit test and package. Or you can invoke “mvn clean verify” to include integration test
- Firefox browser and its Selenium IDE plugin are required for integration tests
- Once the war file is built, you can run the program with “java -jar puzzle-1.0.0.war”
- Go to http://localhost:9024/start to see the page
For developers, using Spring Tool Suite (STS) from eclipse is straight forward.