dagblog.
← Archive

Spring 2.5 — Annotation support, a testing framework and more


The recently released fourth milestone of Spring Framework 2.1 prompted me to move to this release. First I’d like to explain how future Spring releases will be numbered, since it’s a little confusing. The milestone builds are called 2.1. The upcoming Spring release will be 2.5, still compatible with J2SE 1.4, and the final version should be available in October. The next major release will be 3.0, which is expected to target J2SE 5.0.

If you’re wondering what’s so important about 2.5, it’s official support for annotations. That’s right, you can express the configuration metadata for the IoC container using annotations. See the documentation, 3.10. Annotation-based configuration. This goes hand in hand with automatically discovering annotated classes on the classpath.

@Component
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
Another major addition is integration with newer versions of the JUnit and TestNG testing frameworks. The previous API for writing integration tests depended on JUnit 3, which didn’t support annotations. If you wanted to use the existing support with JUnit 4.x, you needed a workaround.
A lot of work went into testing support, resulting in the Spring TestContext Framework. It’s now much easier to write tests that use transactions and dependency injection. The first difference is that you don’t have to inherit from a particular class.
@RunWith(SpringJUnit4ClassRunner.class)
// specifies the Spring configuration to load for this test fixture
@ContextConfiguration(locations={"daos.xml"})
public final class HibernateTitleDaoTests {
// this instance will be dependency injected by type
@Autowired
private HibernateTitleDao titleDao;
public void testLoadTitle() throws Exception {
Title title = this.titleDao.loadTitle(new Long(10));
assertNotNull(title);
}
}
The second difference is the design. The extra functionality is handled by listener instances of type
TestExecutionListeneru. Spring supplies listeners for transaction management and dependency injection. Of course, you can implement your own listener for whatever functionality you need. If you’ve read Honza Novotný’s excellent Writing Fast Integration Tests in Spring, you could implement that more elegantly with a listener and annotation-driven transaction management. I’m implementing something similar with DbUnit as well, so look out for a more detailed article.
Of course, 2.5 has plenty of other useful features, including officially certified WebSphere 6 support and the option of deploying the context using resource adapters for better management of resources such as threads. Here’s the full list from Jürgen Höller’s official post.
  • full Java 6 and Java EE 5 support (JDBC 4.0, JTA 1.1, JavaMail 1.4, JAX-WS 2.0, etc)
  • full-featured annotation-driven dependency injection (including support for ‘qualifier’ annotations)
  • support for component scanning in the classpath (autodetecting annotated classes)
  • “beanName” pointcut element in AspectJ pointcut expressions
  • built-in support for for AspectJ load-time weaving (based on Spring’s LoadTimeWeaver abstraction)
  • further XML configuration namespaces (“context”, “jms”) for maximum convenience
  • extended SimpleJdbcTemplate functionality (support for named parameters etc)
  • officially certified WebSphere support (support for the WebSphere 6 UOWManager facility etc)
  • Spring ApplicationContext can be deployed as RAR file (for headless application modules)
  • JCA 1.5 message endpoint management (for Spring-managed JMS and CCI message listeners)
  • completely revised framework for integration tests (with support for JUnit 4 and TestNG)

Medium version

Related articles

Related CZ Podcast episodes