Injecting Dependencies with Spring
You're almost through with the setup for Spring.
It's time to download it and put it into action. In
this lab, you'll replace the
RentABikeAssembler object with Spring.
When I started using Spring instead of J2EE, it changed my life. I got more productive. My code got easier for me to write, and easier for my customers to understand. Since it was simpler, it often executed faster. It was also infinitely easier to refactor. In fact, I wrote a book about the value of lightweight frameworks like Spring, called Better, Faster, Lighter Java (O'Reilly).
How do I do that?
You'll first have to download Spring. Go get it at http://www.springframework.org. That will point you to sourceforge, where you'll get the best version for your platform. We used Version 1.1. You will need to add a new folder to your project, war\WEB-INF\lib, and put the Spring libraries there (everything in the \dist folder of your Spring distribution).
Moving a well-designed plain-ordinary-Java-object (POJO) application to Spring is straightforward. It only takes three steps:
Refactor your code to take advantage of dependency injection. Model objects are beans, and services are aspects. Usually, you'll only have beans.
Remove the code that instantiates the objects and sets dependencies.
Build a configuration file describing your beans and aspects.
Access your code through Spring.
Since our individual parts are already built to take advantage of dependency injection, moving to Spring is an ...