We will then create a small application called “SpringGreetings” which is a little bit like Twitter I suppose. The goal is to have several users entering short greetings which appear in a list on the screen.
Building the SpringGreetings Application
After setting up the basic development environment, we will do a quick jumpstart and use maven to generate a simple java web application which we will then import into Eclipse. We will then deploy it to the tomcat application server, and see our first web application inside a web page that says “hello world.” Then we will set up basic logging with log4j.
We will then add Spring MVC to the application we have created so far and create a basic form to enter a greeting. After that we will continue to explore form, data models, and domain objects in Spring MVC and ways to encapsulate information. We will add some color to the page and see how to keep that separate from the domain logic.
Moving on to the persistence layers, we will set up a simple hypersonic database (HSQLDB) that is a great tool for learning purposes. We will then integrate Hibernate and add DAO and Service layers. We will persist the greetings and add transactions. We will use annotations wherever we can.
Security comes next, where we will allow users to login. We will then see how to restrict certain pages and methods of the application. The methods used here are very basic and now appropriate for the enterprise world so we will discuss alternatives such as LDAP etc.
We will then discuss dependency injection with Spring and have a look at the dependencies in the application. We will talk about ways to refactor the application and create interfaces where appropriate. We will see how to configure the dependencies via Spring XML config or Spring Java config.
And finally, we will write some unit tests for the different components of the application. We will use mockito in these tests. In traditional Test Driven Development (TDD) however, the test cases are written first and the code is written and refactored after. Since this blog is not about the TDD process but more about creating a flexible, maintainable, and testable design, I decided to include the unit tests at the end instead of the beginning. But when you go to build your first application, do consider the test-first approach to TDD after you have understood the basic concepts of web application development.
Why I chose these frameworks
This blog is also not about why I chose some tools and frameworks over others. But I will explain that briefly now. I chose Eclipse IDE because I feel it is currently the most widely used and it is the IDE that I am most comfortable with. I chose tomcat in this example over other application servers such as jetty or glassfish or jboss because I feel it is good for learning purposes and has been used and documented extensively by other developers. I chose maven over ant because maven seems much more widely accepted these days than ant and most of the sample projects I have found around the internet use maven. And, I also like Maven for a number of reasons.
I chose Spring MVC 3 with annotations for the web framework because, afterall, I was using Spring for the rest of the application and I think that Spring MVC is great. It so easy to test and deal with when used properly. But there are many great web frameworks out there. I chose Hibernate with annotations for the persistence framework because it is not only simple to use, but also seems to be the most widely used and documented framework today, integrates well with Spring, and makes testing a breeze. I used HSQLDB for this example but you could easily use mysql or any other database. HSQLDB is the easiest to use for education and is often used in other tutorials as well.
And finally, for TDD I used mockito over other mocking frameworks such as easymock and jmock because I found it was the most logical and requires the least amount of code. It also seems to be the favorite among developers at the moment.
Bottom line: less code, more fun. I tried not to create any unnecessary classes or xml configuration code. Every framework, tool, class, and method, and file represent a design choice that has undergone a significant amount of consideration.