Building Spring 2 Enterprise Applications
Interface21, Bram Smeets, Seth Ladd (2007)
This book is an excellent nuts and bolts introduction to the Spring 2.0 framework. It covers the core (DI/IoC) container, aspect oriented programming, data access, transactions, and Spring MVC. At work I introduced this as part of our bi-monthly book club topic. If you don’t know what DI/IoC is, buy this book used for $1 on amazon and read it.
Chapters 1 and 2 introduce the concept of Dependency Injection (DI) and Inversion of Control (IoC). Through a series of easy to follow examples, the book explains the short comings of the ‘new()’ keyword in Java. You’ll never need to use ‘new()’ again if you use a DI container such as Spring. It is a neat idea and makes unit testing and refactoring a breeze, provided your XML configuration files are well organized.
Chapters 3 and 4 cover Aspect Oriented Programming and Spring’s implementation Spring AOP. I found this to be a clearly written example of how AOP works and what Spring AOP does for you. In a nutshell, AOP allows wrappers (called advice) to be placed around object methods, such that the wrapper code runs before, after, or before and after a method on an existing object is called. These wrappers address cross-cutting concerns, such that objects may become more focused and less intertwined in an architecture. Whether it is a new object model or the maintenance of an existing domain model, AOP can come in very handy in terms of adding logging, auditing, security, or monitoring.
To use Spring AOP, the target object must be instantianed by the Spring factory. At that point what the caller is handed at runtime is a proxy object that wraps the target object, based on the AOP configuration. Wrapping is also called method interception. This technique can be used to manage life cycles of external resources (like database connections or network sockets), handle transaction logic, impose security, logging, etc. It can also be used filter or convert exceptions that are thrown by the wrapped method. Some very clean code can come from this methodology, perhaps at the expense of some overhead.
The book concludes with a chapter on testing, and it was a little thin. The first half covers unit testing with JUnit. The integration testing section of the testing chapter introduces using mock objects (EasyMock) to facilitate unit testing how objects interact. I would have liked to see some more real world examples of how to wire up these powerful tools together in a larger unit test suite. With Spring, more and more critical details about the software are ending up in XML configuration files. This makes me a little nervous because there is no compiler to check for accuracy. Unless a full scale unit test suite is created, the likelihood of runtime errors increases with the use of Spring.
Update 1/3/2009 – with Spring 3.0 now available, this book is dated. If you are new to Spring or DI/IoC, chapters 1-4 are a great overview of the basics. Spring 3.0 adds some auto-magic features like @Autowired. It pays to understand what is going on under the hood. This book shows how everything was done by hand in version 2.0 – the old days of 2007.