return renderer;
}
public MessageProvider getMessageProvider() {
return provider;
}
}
The implementation here is trivial and naïve, the error handling is simplistic, and the name of the
configuration file is hard-coded, but we already have a substantial amount of code. The configuration
file for this class is quite simple:
renderer.class=com.apress.prospring3.ch2.StandardOutMessageRenderer
provider.class=com.apress.prospring3.ch2.HelloWorldMessageProvider
Make a simple modification to the main() method (as shown in Listing 2-9), and we are in business.
Listing 2-9. Using MessageSupportFactory
package com.apress.prospring3.ch2;
public class HelloWorldDecoupledWithFactory {
public static void main(String[] args) {
MessageRenderer mr = MessageSupportFactory.getInstance().getMessageRenderer();
MessageProvider mp = MessageSupportFactory.getInstance().getMessageProvider();
mr.setMessageProvider(mp);
mr.render();
}
}
Before we move on to see how we can introduce Spring into this application, let's quickly recap
what we have done. Starting with the simple "Hello World!" application, we defined two additional
requirements that the application must fulfill. The first was that changing the message should be simple,
and the second was that changing the rendering mechanism should also be simple. To meet these
requirements, we introduced two interfaces: MessageProvider and MessageRenderer. The
MessageRenderer interface depends on an implementation of the MessageProvider interface to be able to
retrieve a message to render. Finally, we added a simple factory class to retrieve the names of the
implementation classes and instantiate them as applicable.
Create Spring Project in STS
Before we discuss how to refactor the "Hello World!" application with Spring, we'll show you how to use
STS for Spring-based application development. STS can save you a lot of time when creating Spring
projects of different types (e.g., simple Spring DI applications, Web applications, JPA applications, etc.),
and STS will create a project with all required dependencies declared for you. So, let's download and
install STS first and create the project for refactoring the "Hello World!" application.
First, download STS from the SpringSource web site (www.springsource.com/downloads/sts),
select your development environment, and install it. At the time of writing, the version is 2.8.1. This is
also the version we used for developing all the example code presented in this topic, as well as the
sample application.
Next, after installation, start STS, and create a new Spring template project. For template selection,
choose Simple Spring Utility Project (see Figure 2-5).
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home