img
Afterward, we declare the bean with the ID "provider" and the corresponding implementation
class. When Spring sees this bean definition during ApplicationContext initialization, it will instantiate
the class and store it with the specified ID.
Then the "renderer" bean is declared, with the corresponding implementation class. Remember
that this bean depends on the MessageProvider interface for getting the message to render. To inform
Spring about the DI requirement, we use the p namespace attribute. The tag attribute
p:messageProvider-ref="provider" tells Spring that the bean's property, messageProvider, should be
injected with another bean. The bean to be injected into the property should reference a bean with the
ID "provider". When Spring sees this definition, it will instantiate the class, look up the bean's property
named messageProvider, and inject it with the bean instance with the ID "provider".
As you can see, upon the initialization of Spring's ApplicationContext, the main() method now just
obtains the MessageRenderer bean by using its type-safe getBean() method (passing in the ID and the
expected return type, which is the MessageRenderer interface) and calls render(); Spring has created the
MessageProvider implementation and injected it into the MessageRenderer implementation. Notice that
we didn't have to make any changes to the classes that are being wired together using Spring. In fact,
these classes have no reference to Spring whatsoever and are completely oblivious to its existence.
However, this isn't always the case. Your classes can implement Spring-specified interfaces to interact in
a variety of ways with the DI container.
Now let's see the Spring DI­powered "Hello World!" application in action. In STS, just right-click the
class HelloWorldSpringDI and choose Run As and then Java Application. The execution result will be
displayed on the Console tab (see Figure 2-7).
Figure 2-7. Execution output of HelloWorldSpringDI
Summary
In this chapter, we presented you with all the background information you need to get up and running
with Spring. We showed you how to obtain both the Spring release distribution and the current
development version directly from GitHub. We described how Spring is packaged and the
dependencies you need for each of Spring's features. Using this information, you can make informed
decisions about which of the Spring JAR files your application needs and which dependencies you need
to distribute with your application. Spring's documentation, sample applications, and test suite provide
Spring users with an ideal base from which to start their Spring development, so we took some time to
investigate what is available in the Spring distribution. Finally, we presented an example of how, using
Spring DI, it is possible to make the traditional "Hello World!" a loosely coupled, extendable message-
rendering application.
The important thing to realize is that we only scratched the surface of Spring DI in this chapter,
and we barely made a dent in Spring as a whole. In the next chapter, we take an in-depth look at the
sample application that we will be building, paying particular attention to how we can use Spring to
solve common design issues and how we have made our application simpler and more manageable
using Spring.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home