|
|
Welcome to the MyEclipse Introduction to Spring tutorial. In this
tutorial we are going to cover some of the basic features of the
Spring framework, such as dependency injection, and demonstrate
using MyEclipse to assist you while developing Spring
applications.
Since Spring is such an expansive set of frameworks, this
tutorial will focus on dependency injection, which is one of the
core capabilities of Spring that permeates all of Spring. To put
it simply, dependency injection is the idea of having a framework
manage and assign object references at the moment the reference
is needed. This removes the need for the developer to call a
property setter or JNDI lookup in order to get a reference to an
object when needed. If the system is clearly defined, using XML
or annotations for example, then the Spring runtime knows when
the dependency is needed and will make sure it is provided
appropriately. By removing "object bookkeeping", the
developer can more easily focus on the business logic of the
application.
This simplified view of the Spring core is what we will be
looking at in this tutorial. In addition, this tutorial also uses
Spring 1.x conventions and doesn't cover some of the new
annotations support in Spring 2.0. We will release a tutorial at
a future time that will cover the expanded functionality in
version 2.0.
The program used in this tutorial is the Knight example from the
Spring
in Action book. The contents of the project were modified
slightly to remove any compilation warnings and to help clarify
the example, but only in small bits.
|
|
|
This tutorial is intended for developers who are somewhat
familiar with either MyEclipse or Eclipse so you are expected to
recognize navigation and understand some of the more common
concepts like "Views" within the IDE. While this
tutorial will also introduce some of the basics of Spring, it is
not intended to replace detailed introductory Spring material
found on the Spring Framework site and in the
Resources section
below.
If either MyEclipse or Spring makes you feel uncomfortable, this
introductory tutorial should provide you with the basics of
both. If you wish to learn more about either MyEclipse or
Spring please have a look at either our product
Documentation for more
material or our
Resources section
respectively.
|
|
This tutorial was created with MyEclipse 5.1 and the bundled
Spring 1.3 libraries. If you are using a another version of
MyEclipse, most of these screens and instructions should still be
very similar. If you are using Spring 2.x, it should be backward
compatible with all the concepts used in this tutorial.
If you are using a newer version of MyEclipse and notice portions
of this tutorial looking different than the screens you are
seeing, please
let us know and we will make sure to
resolve any inconsistencies.
|
|
Introducing Spring is harder than most frameworks because it's
not a single-purpose technology. Spring can be thought of as a
huge framework of best practices for almost every area of Java
software development. Everything from Plain-Old-Java-Object
(POJO) development, to web application development, to enterprise
application development, to persistence layer management and
aspect oriented programming (AOP). Spring supports it all and
does so with some of the most well designed and heavily tested
code in the Java industry.
Because of its size, we need to focus this tutorial on the
simplest application of Spring which is POJO development
utilizing dependency injection. To clarify, dependency injection
is a mechanism by which Spring handles creation and initializaton
of the proper type of child object for a parent object at the
moment the parent object needs the reference to the child. An
example of where this is useful in the context of enterprise Java
programming would be for your web application to instantly have
access to your data-access layer in order to read or write an
object from the database. This is a classic example of allowing
Spring to inject the reference to the data-access layer into a
POJO in the web application in order to load or save an object.
To immediately demystify any all-knowing magic from your minds
about this process, the way this is done in the Spring is that
the object reference relationships are mapped out in XML
configuration files in Spring 1.2 and handled with Java 5
annotations in Spring 2.0.
Using this declarative method, Spring developers can actually
soft-wire their application portions together using these
annotations or XML configuration files such that when the
application runs, Spring creates and instantiates all the object
relationships "on demand". The advantage to
having everything soft-wired is that portions of an application
can quickly and easily be swapped out for alternative
implementations (e.g. testing implementations) by simply changing
around the annotations or XML configuration information and
rerunning the application. There isn't even a need to recompile
the application in some cases. This can be a huge boon to
developers working on large applications that require constant
testing or when delivering a larger application in interations.
Spring provides a very natural way to thinking about these
problems encouraging you to maintain your application in a
modular architecture that supports this plugging and unplugging
capability.
To learn more about Spring, we would encourage you to begin with
the Introduction to Spring tutorial in our
Resources section.
|
|
In this tutorial we are going to utilize a slightly modified (and
simplified) version of the Knight Spring application from the
Spring
in Action book since it is simple and well known.
You can download a ZIP version of this MyEclipse Spring project
for the example
here or
from the link in our
Resources section. You can pull this
project into your MyEclipse workspace by using the
File > Import > Existing Project functionality as
outlined below:
After the project has been imported we can use some of
MyEclipse's advanced software development features to get a
better idea how this Spring application is architected.
|
|
The first thing we will do is create a new UML class diagram of
all the source code to see how the classes are related to one
another. The sample project you just imported already includes
this UMR (UML Model Repository) file in the root of the src/java
folder by the name
Knight.umr. You can double click that file to open up
the diagram, it will look something like this:
|
Figure 2. UML class diagram of the
Knight application
|
This diagram provides us immediate visual relationships between
the knights, quests, exceptions and other classes that are
involved in this application. While this application is quite
small you could just have easily reverse engineered a much larger
application to get an idea of what it looked like. In this
particular case, I simply selected the classes out of the package
explorer, and dropped them onto a new class diagram to get this
result.
Looking at a UML class diagram is nice, but it doesn't tell us
the whole story, especially about a Spring application. For that,
it would be helpful if we could get some sort of help visualizing
the relationships between the different beans in the
application... as luck would have it, MyEclipse can help you do
this too!
First, you will want to open up the Spring Beans view. This view
is a display of all the beans configured in your Spring
application through one or more Spring bean configuration files.
These configuration files don't have a clearly defined naming
convention (like
struts-config.xml,
faces-config.xml, etc.) and an application can contain
any number of these bean configuration files. Because of this,
MyEclipse allows you to configure a list of XML files that
represent your bean configurations. This information can be found
under the project properties under
MyEclipse > Spring as shown below:
Now to open the actual Spring Bean view, you want to go to
Window > Show View > Other... > MyEclipse
Enterprise Workbench > Spring Beans. After the Spring Beans
view is open, you can even visualize the graph of your Spring
beans and how they related as follows:
From the graph of our Spring beans we see that our Knight has a
reference to a minstrel and a
knightTarget which in turn has a reference to a quest
that the Knight will embark on. In this application we are
actually using Aspect Oriented Programming (AOP) in the form of
an interceptor to implement the minstrel.
|
|
Our minstrel is going to "sing" (actually log) about
all the actions our Knight does before our Knight does them by
way of a "before" method interceptor. Simply put, this
type of "before" interceptor is simply a method that
performs an action (like writing a description to the console
about what our Knight is about to do) "before" the real
operation occurs. To accomplish this, you instruct Spring to
weave this interceptor into the behavior of the other class in
your bean definition. For the web developers reading this, you
can immediately see an application of this: handling
transactions.
MyEclipse is going to help you navigate your Spring application
quickly. In this case, let's say we wanted to see exactly how the
minstrel was implemented. We can simply double click on our
minstrel bean in our graph, jump to that definition in our Spring
bean configuration file and then using
CTRL+Left Click we can jump directly to the minstrel
implementation as shown below:
So the minstrel implementation looks pretty simple, all it's
doing is logging the actions of the Knight. To run the Spring
application you will want to run the
com.springinaction.chapter01.knight.KnightApp class and
you will get the following output in your console:
[DEBUG] 10:51:03 KnightApp - Running KnightApp
[DEBUG] 10:51:04 KnightOfTheRoundTable - Brave Bedivere did
embarkOnQuest
[DEBUG] 10:51:04 KnightApp - KnightApp Finished
|
Figure 6. Default minstrel output
to the console
|
You can see the default minstrel implementation outputting the
line "Brave Bedivere did embarkOnQuest". Let's say that
we wanted a different minstrel implementation entirely but didn't
want to recode the entire
MinstrelAdvice class, this is not a problem at all. We
just write a new minstrel implementation then plug it in using
Spring.
|
|
To create our new minstrel we are going to create a copy of the
MinstrelAdvice class and name it
BetterMinstrelAdvice. The class will look like this (
Download):
package com.springinaction.chapter01.knight;
import java.lang.reflect.Method;
import org.apache.log4j.Logger;
import org.springframework.aop.MethodBeforeAdvice;
public class BetterMinstrelAdvice implements
MethodBeforeAdvice
{
public void before(Method method, Object[]
args, Object target)
throws Throwable
{
Knight knight = (Knight)
target;
Logger song =
Logger.getLogger(target.getClass());
song.debug("La la
lee la, our fair knight " + knight.getName() + " has
performed " + method.getName() + " with such
grace!");
}
}
|
|
Figure 7. The implementation of BetterMinstrelAdvice
|
You will notice that our implementation is still simple, but our
minstrel sings a better song and does a better job praising the
Knight. To plug this new implementation into our Spring
application there are two steps:
-
Create a new bean definition in the bean configuration file
-
Replace the
minstrel interceptor for our Knight with our new
betterMinstrel interceptor
These steps are going to look as follows:
After we have our new
betterMinstrel bean plugged into our
knight bean, if we re-run the same application without
touching anything else, you'll notice that our output on the
console now looks like this:
[DEBUG] 11:38:57 KnightApp - Running KnightApp
[DEBUG] 11:38:57 KnightOfTheRoundTable - La la lee la, our
fair knight Bedivere has performed embarkOnQuest with such
grace!
[DEBUG] 11:38:57 KnightApp - KnightApp Finished
|
|
Figure 9. New console output from
our betterMinstrel implementation
|
You can really see the value of the pluggability that Spring
allows you to have in your projects. One of the most obvious uses
of this plugging and unplugging of portions of an application
instantly is testing. Consider a web application; you could write
up mock DAO implementations and plug them in before running all
the test cases without needing to change anything. You could
simply wire your test classes, via Spring, to the testing DAO
classes instead of the real ones that would require an active
database connection.
Other examples could be switching out entire implementation
details of an application (like caching, data access, etc.)
without breaking the application or spending time refactoring the
classes and references in the application itself.
|
|
While the application in this tutorial may seem simple, the
techniques and information provided is critical to understanding
both Spring and MyEclipse. We took a look at some very slick code
and Spring visualization techniques that you can use to better
understand an application you are working with and also provide
documentation to others to understand it more clearly.
In addition, we explored the Spring-specific tooling, wizards and
UIs that allowed you to instrument and change your application
without any XML editing while we delved into how Spring really
works and demonstrated seamlessly replacing a portion of a
running application with a new implementation.
We hope you have found this tutorial helpful. If you had comments
about this tutorial or suggestions/questions for us, please
let us know. We always value our user's
feedback especially on educational materials such as these.
|
|
No FAQ entries at this time...
|
|
Below are links to resources that we hope will help answer most
of the questions you could have while working your way through
this tutorial pertaining to Spring. Spring is a framework of best
practices for all sorts of development scenarios. Because of
that, finding help on specifically the portion of Spring you may
have questions about can be tricky. Below we try and present
material for some of the major uses of Spring:
Reference
Basics
Web Development
Enterprise
|
|
We would like to hear from you! If you liked this tutorial, has
some suggestions or even some corrections for us please let us
know. We track all user feedback about our learning material in
our
Documentation Forum.
Please be sure to let us know which piece of MyEclipse material
you are commenting on so we can quickly pinpoint any issues that
arise.
|