Expert Spring MVC and Web Flow

(Dana P.) #1
The preceding example can be roughly translated into the following Java code:

CashRegister cashRegister = new CashRegisterImpl();
PriceMatrix priceMatrixBean = new PriceMatrixImpl();
cashRegister.setPriceMatrix(priceMatrixBean);

Following the tenets of zero impact of the framework on your code, you usually won’t
interact with the ApplicationContextdirectly. To keep your system lightweight, avoid interact-
ing directly with the ApplicationContext. We’ll cover suggested application design later,
showing how to use the ApplicationContextwithout embedding it into your object model.
The ApplicationContexthas a lot of features and is very powerful. If you are not already
comfortable with this object, we recommend you review the capabilities of the BeanFactory
and the ApplicationContextusing Pro Spring. It covers the capabilities, features, and configu-
ration of these core Spring objects in great detail. We will also use the XML file format
tremendously, so we encourage you to become familiar with it.

The Return of the POJO


We’ve covered Dependency Injection and the Spring ApplicationContext, the two most central
concepts in the Spring Framework. They are but tools, created only to enable applications
(yes, even web applications) to be written entirely with plain old Java objects (POJOs). You can
now develop web applications that are object oriented and without a trace of the framework
in your core business logic. The Spring Framework and, by extension, Spring MVC go out of
their way to get out of your way. This applies to the whole range of applications you might
write, from simple web applications to large enterprise web applications.
One of the most important topics this book covers is that your web applications can be built
using strong OO principles. You will find yourself focusing on the business logic embedded in
your object model, instead of inside framework-specific code. This opens up the door to all of
the design patterns, principles, and practices for effective object-oriented development. We will
show you how to build out the domain object model first, and then the framework will enhance
it and expose it as a web application.
The POJO has come full circle and regained its place at the top of the food chain. You
can now take advantage of all those concepts you learned about when studying OOP, such as
inheritance, encapsulation, and polymorphism. The Spring Framework is all about transpar-
ently serving and enhancing your business domain objects. You no longer have to sacrifice a
design principle just to fit into the framework.
For example, instead of subclassing a framework abstract class (and destroying your
one chance at inheritance) you are free to subclass whatever class makes sense. The Spring
Framework is considered lightweight because it doesn’t weigh down your application code by
imposing intrusive restrictions. Your POJOs remain POJOs and your domain object model
remains independent of any framework or environment.
We will take advantage of Spring’s acceptance of POJOs throughout our coverage of Spring
MVC. Developing systems with POJOs allows us to concentrate on business logic and solid OO
design principles.

18 CHAPTER 2 ■SPRING FUNDAMENTALS

Free download pdf