Saturday, February 11, 2012

Running a spring 2.5 program using Spring STS templates.

As I mentioned, I'm tackling the awkward task of learning Spring 3 via a spring 2.5 book, called Spring Recipes. I managed to get the first program mentioned in Chapter 2, a hello world, running, and I'ld like to share it.

The first thing I did was use Spring's STS templates to generate a utility project. That's file, new, spring template project, spring utility project. I'll give it a name of SpringRecipesHello, and a package name of "com.apress.springrecipes.hello", because that's what's in the book.

Now, we can examine what exactly is generated. Under src/main, we have a couple of classes, ExampleService.java and Service.java.

Service.java is just an interface:

package com.apress.springrecipes.hello;

public interface Service {

String getMessage();

}


ExampleService.java implements the Service:

package com.apress.springrecipes.hello;

import org.springframework.stereotype.Component;


/**
* {@link Service} with hard-coded input data.
*/
@Component
public class ExampleService implements Service {

/**
* Reads next record from input
*/
public String getMessage() {
return "Hello world!";
}

}



You'll notice the "Component" annotation. I'm pretty sure this is telling Spring that this is a java class to be instantiated, and a scan statement in the configuration file will pick it up.

But, how do you run the project?

Well, one way to do is is via the test classes that also get generated. Let's take a look at those.



package com.apress.springrecipes.hello;

import com.apress.springrecipes.hello.ExampleService;
import junit.framework.TestCase;

public class ExampleServiceTests extends TestCase {

private ExampleService service = new ExampleService();

public void testReadOnce() throws Exception {
assertEquals("Hello world!", service.getMessage());
}

}


If we right-click on this and run it as a jUnit test, we'll get a successful test. Disappointingly, it doesn't seem to leverage the spring framework at all - there's no dependency injection.

Let's look at the other test class:


package com.apress.springrecipes.hello;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import com.apress.springrecipes.hello.Service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ExampleConfigurationTests {

@Autowired
private Service service;

@Test
public void testSimpleProperties() throws Exception {
assertNotNull(service);
}

@Test
public void testReadOnce() throws Exception {
assertEquals("Hello world!", service.getMessage());
}

}


I actually added the second test. The @Autowired seems to indicate that the service should be automatically wired, i.e., instantiated and set, into the "service" member. Adding the second test, which succeeds, shows this to be the case. I didn't have as much luck when I tried changing the other class to include @Autowired - some type of illegal state applicationcontext not loaded error, so - I'll leave it at this for now.

No comments:

Post a Comment