Spring 4 Without XML

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

  1. 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.
  2. 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.
  3. The @EnableWebMvc in the same PuzzleSolvingWebMvcConfig class eliminates the needs for XML to config the view revolvers.
  4. 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.
  5. 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.

Other Notes

Some other highlights (and potential improvements)  not related to the XML topic are:

  1. 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.
  2. The UI user input validation is done by using JQuery-validator plugin. However this could be avoided if using HTML5 elements that are meant to reduce the javascript usage.
  3. 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.
  4. 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

  1. Java 7+ and Maven are required
  2. Download or checkout source code from GitHub
  3. Execute “mvn clean package” to compile, unit test and package. Or you can invoke  “mvn clean verify” to include integration test
  4. Firefox browser and its Selenium IDE plugin are required for integration tests
  5. Once the war file is built, you can run the program with “java -jar puzzle-1.0.0.war”
  6. Go to http://localhost:9024/start to see the page

For developers, using Spring Tool Suite (STS) from eclipse is straight forward.

Advertisements
This entry was posted in Java, Programming and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s