Monday, June 20, 2011

Say Hello in Spring 2.5 / Spring 3.0

Ok you want to get set up with Spring and do a simple 'Hello World'.
Let's do it.

Step one: Sort out your IDE

There are some choices here. You can just add some Spring libs to your path, you can just download some Spring plugins for Eclipse or you can do I what I recommend which is to download SpringSource Tools. This is an eclipse based IDE developed especially for Spring development. You get the benefits of Eclipse, a guarantee the Spring stuff will work (ever have pain with plugins not working or clashing :-)) and you'll also get the developer edition of vFabric tc Server which we won't need that for this example but is still useful to have for future use.

Step two: Create a Spring Project

Select File / New / Spring Template Project. Select Simple Spring Utility Project.
Provide project name e.g. HelloWorld. Provide top level package e.g.

com.dublintech.springtutorial1


Select Finish. In the Project Explorer view you should see your project. Note the 's' just above the project folder icon. This indicates it's a spring project.

Step three: Ignore (or get rid off) the sample code

You'll note that the project template comes with example source files and test files (at time of writing these are: Service.java, Example Service.java, ExampleConfigurationTests.java and ExampleServiceTests.java). Ignore or delete all of these.

Step four: Write your own code.

Let's define an interface: GreetingService

An implementation that will give us the proverbial HelloWorld:

And another implementation of the same interface. But this time a little bit more personal!


Keep things simple and just put everything in the package you specified when creating the project. Notice that we have no dependencies to any spring libraries in our interface or either of our classes.

Step five: Add some spring configuration.

Go to file app-context.xml. It comes with the template.

Notice that the default bean that comes with the template is configured and given a Bean id.



Now it's time to make your classes Spring beans. Add the following:

Ok, no need to sweat. All we're doing here is
* giving the HelloWorldService the bean id helloWorldService
* giving the PersonalGreetingService the bean id personalGreetingService


In addition
* personalGreetinService will have its name property set to Alex.


Step Six: Write some tests so that you see your beans in action.

With respect to this test:
  • I have left out the imports to keep the page space to a min. Hopefully they are obvious enough
  • The @ContextConfiguration is used to hook a spring application context into the test. When no location is specified the default one is derived from the name of the class. In this case it will be: /resources/com/dublintech/springtutorial1/GreetingTests-context.xml. We'll see more about this file in the next step.
  • @RunWith(SpringJUnit4ClassRunner.class) means that the GreetingTests can run JUnit 4.4 tests *and* reap the benefits of the Spring framework such as dependency injection and loading of application contexts.
  • @Autowired is a spring short hand for autowiring beans. In this case the first
    autowiring wires the bean that has the id helloWorldService into your test.
    The second autowiring wires the bean that has the id personalGreetingService into your test. This is where smart naming helps you and typos will reck your head!!!

Now onto the tests themselves. The first test tests the helloWorldService bean and the second tests
personalGreetingService tests... you guessed it... that Alex will be wired in by the spring framework
into the PersonalGreetingServiceBean and 'Hello Alex' will be returned.

Step seven: Your tests need to know how to load the ApplicationContext

Create a file named: GreetinTests-context.xml. Place it in the package
com.dublintech.springtutorial1 (under src/test/resources).

It will look like:

This is pretty simple. All it does is tell your tests what Spring configuration / ApplicationContext to use.

Step Eight: Run your tests


Go back to your tests, right click and select to run as junit.
That's it.

Comment:
This was a pretty simple example but there a few points worth noting.

1. How easy it is to tell your tests what spring configuration / ApplicationContext to use.
This mean you can use different configurations, which wire your beans different ways and test
different things without ever having to recompile any code.

2. Spring isn't a lightweight as you might think. In this example, your IDE will use Maven
to ensure that all the jars your project needs are in place. Expand the Maven Dependencies in
your project explorer view and you'll see what jars your project is using.  For one of Spring simplest features still requires a lot of jars.

References
1. http://static.springsource.org/spring/docs/2.5.x/reference/testing.html

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Alex, thanks for the blog. This got me started on the Spring 3.0 utility template, which I was trying to figure out.

    A couple of errata notes: there are a couple of places where "GreetingService" is spelt "GreetinService". Also, the second test fails because it's calling the "greet" method on the wrong bean. That was actually a good debugging exercise.

    ReplyDelete