Spring 3.1 M2: Testing with @Configuration Classes and Profiles

Engineering | Sam Brannen | June 21, 2011 | ...

As Jürgen Höller mentioned in his post announcing the release of Spring 3.1 M2, the Spring TestContext Framework(*) has been overhauled to provide first-class testing support for @Configuration classes and environment profiles.

In this post I'll first walk you through some examples that demonstrate these new testing features. I'll then cover some of the new extension points in the TestContext framework that make these new features possible.

      Please note: this is a cross post from my company blog www.swiftmind.com.

Background

In Spring 2.5 we introduced the Spring TestContext Framework which provides annotation-driven integration testing support that can be used with JUnit or TestNG. The examples in this blog will focus on JUnit-based tests, but all features used here apply to TestNG as well.

At its core, the TestContext framework allows you to annotate test classes with @ContextConfiguration to specify which configuration files to use to load the ApplicationContext for your test. By default the ApplicationContext is loaded using the GenericXmlContextLoader which loads a context from XML Spring configuration files. You can then access beans from the ApplicationContext by annotating fields in your test class with @Autowired, @Resource, or @Inject

Defining the Future for Virtualized and Cloud Java

Engineering | Rod Johnson | June 14, 2011 | ...

Today I am proud to announce version 5 of our VMware vFabric™ application platform defining the future of enterprise Java for cloud and virtualized execution environments. vFabric blazes the path to new and modern cloud architectures by providing a modern programming model paired with next-generation platform services. A path that is not overgrown with the cruft and complexity of prior-generation technologies. With vFabric 5, VMware is ensuring that enterprise Java is ready to meet the challenges of tomorrow’s demanding, data-intensive, massively scalable applications.

vFabric 5 continues to provide the best place to run your Spring applications with vFabric tc Server and the ability to monitor and manage those production solutions with incredible intelligence via vFabric Hyperic. The platform also addresses the technical challenges of cloud computing head on, supporting new approaches to data management that enable applications to scale across elastic, geographically distributed cloud architectures with our vFabric GemFire and RabbitMQ

This week in Spring: June 14th, 2011

Engineering | Josh Long | June 14, 2011 | ...

Welcome back to another installment of "This Week in Spring," and what a week it's been! This last week saw the release of the Spring 3.1 M2 and vFabric 5! Lots of exciting stuff to talk about there, as well as general community news, so let's get to it!

  1. Today VMware announced the release of VMware vFabric 5, the application platform that defines the future of enterprise Java for cloud and virtualized execution environments. vFabric 5 contains many of the technologies that the Spring community is already familiar with including tc Server, Hyperic, GemFire, and RabbitMQ, but now adds some new technology.
    • Elastic Memory for Java (EM4J): a new capability for tc Server that provides a completely new level of coordination between the application server and the underlying virtual machine. EM4J uses the underlying vSphere virtualization to overcome some of the limitations of the Java's static memory heap.
    • Spring Insight Operations: leverages the same code-level tracing technology from the Spring Insight project but pulls together information from multiple application servers into a single console with roll-up views, drill downs, and historical comparisons ready for production systems.
    • SQLFire: vFabric SQLFire leverages the time-tested vFabric GemFire underpinnings providing data at memory speed and horizontal scale but vFabric SQLFire adds familiar and standard SQL and JDBC interfaces to the service.

    Rod Johnson discusses all the details of the release in his latest blog. Be sure to check out the latest release and try it out.

  2. Spring core lead Juergen Hoeller has announced that Spring 3.1.0 M2 has been released! At long last, the next step on the steady march to Spring 3.1 GA! The new release is as feature-packed as the last one, with a long list of major new features including (but definitely not limited to!) improved Java configuration support, XML-free and hassle-free Servlet 3.0-based Spring MVC application bootstrapping, new Builder APIs for JPA and Hibernate, and much, much more! Check out the release announcement here and get the bits from your build dependency management tool of choice or the download page
  3. <LI> Hot on the heels of the Spring 3.1 release announcement, <a href="http://blog.springsource.com/2011/06/10/spring-3-1-m2-configuration-enhancements/">Chris Beams chimes in</a> on the much-improved Java-centric configuration model in Spring 3.1, M2, even as compared to M1! The features are really starting to come together to make this one of the smoothest, well arranged releases, yet! </LI> 
    
    <lI> 
    

    Spring 3.1 M2 represents a marked improvement in core Spring, as well as Spring MVC! Rossen Stoyanchev chimes in to introduce the numerous (truly, you'll need to read the detailed blog to…

Spring 3.1 M2: Spring MVC Enhancements

Engineering | Rossen Stoyanchev | June 13, 2011 | ...

This post focuses on what's new for Spring MVC in Spring 3.1 M2. Here are the topics:

  • Code-based equivalent for the MVC namespace.
  • Customizable @MVC processing.
  • Programming model improvements.

A brief reminder that the features discussed here are in action at the Greenhouse project.

Code-based Configuration For Spring MVC

As Chris pointed out in his blog post last Friday, XML namespaces cut down configuration dramatically but also reduce transparency and sometimes flexibility. This holds true for the MVC namespace, which supports a number of customizations but not everything that's available. That means you are either able to use it or otherwise leave it. We believe code-based configuration has a solution for that and a path from simple to advanced.

Let's begin with this simple, familiar snippet:


<mvc:annotation-driven />

Although not required for using annotated controllers, <mvc:annotation-driven> does a number of useful things -- it detects the presence of a JSR-303 (Bean Validation) implementation and configures data binding with it, it adds a JSON message converter if Jackson JSON library is available, and a few other things that can save quite a bit of configuration.

Now let's match that with code-based configuration:


@Configuration
@EnableWebMvc
public class WebConfig {
}

Here @EnableWebMvc imports an @Configuration class that matches the goodness of <mvc:annotation-driven>. As simple as that.

The next step is to use an attribute in <mvc:annotation-driven> perhaps to provide a FormattingConversionService, or to add a sub-element perhaps configuring message converters, or to use other MVC namespace elements like <mvc:interceptors>, <mvc:resources>, etc.

Let's see how to do all of that in code-based configuration:


@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        // register converters and formatters...
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // add message converters...
    }

    @Override
    public void configureInterceptors(InterceptorConfigurer configurer) {
        configurer.addInterceptor(new…

Spring 3.1 M2: Configuration Enhancements

Engineering | Chris Beams | June 10, 2011 | ...

As Juergen mentioned in his post yesterday, and as I've mentioned in my previous posts on 3.1 M1, one of the major themes of Spring 3.1 is completing our vision for code-based configuration in Spring. We think a modern enterprise Java application should have a choice between Java and XML as first class options for its configuration. In this post we'll see how Spring 3.1 M2 helps make this a reality.

Note that although Java-based configuration has been available since Spring 3.0, with this release it is now on par with many more of the XML-based features that have been developed over the…

Spring Framework 3.1 M2 released

Engineering | Juergen Hoeller | June 09, 2011 | ...

Spring Framework 3.1 M2 has been released this week, marking the end of the 3.1 milestone phase. We are moving on to the release candidate phase now, preparing for a feature-complete RC1 in July and a GA release in September.

3.1 M2 completes the work on several major themes started in 3.1 M1 back in February:

  • We've stabilized our environment abstraction and the environment profile mechanism. If you haven't given it a try already, now is a great time to check it out!

  • Our Java-based application configuration approach has changed from the @Feature approach in M1 to @Enable* annotations on regular @Configuration classes in M2.

  • The cache abstraction has been revised for delivering a minimal cache interaction SPI. Our declarative caching solution (@Cacheable etc) keeps sitting on top of it.

This week in Spring: June 7th, 2011

Engineering | Josh Long | June 08, 2011 | ...

Welcome back to another exciting roundup! This week's been a blur. Honestly. So much new stuff happening, all after the rush of excitement that was the S2G Forums in Europe last week. Leave's a guy breathless, but excited. Read on!

  1. Mark Fisher and Ramnivas Laddad's webinar Spring From Zero to Cloud in 60 Minutes is available online.
    This webinar is a breakneck-speed tour of some of the Spring, Spring Roo and Grails support on CloudFoundry. Check it out!
    Before you start watching, however, quickly signup at CloudFoundry.com to get access to the public, free-beta cloud service. If you want to checkout the code and learn more, check out CloudFoundry.org.

  2. Jeremy Grelle, Spring BlazeDS lead and general "Spring web dude," has announced the first release candidate of the Spring Flex project. The Spring Flex project integrates the Flex BlazeDS middleware with Spring, providing a dead-simple way to expose Spring beans in a way that can be consumed by Flex or Adobe AIR web and desktop clients. The Spring Flex project also provides integration with Spring Security and provides tight-knit support for server-side push based messaging, entirely in-BlazeDS, or through JMS or Spring Integration. Ever wanted to notify users logged into an application that something's happened on the server side (Twitter message, new AMQP message, new XMPP message, whatever..)? Spring Flex makes it easy.
  3. Martin Lippert, SpringSource Tool Suite team lead, has given an interview about the latest and greatest in SpringSource Tool Suite 2.6. He talks about many of the highlights, including STS 2.6's reworked Spring Webflow visualization, Java configuration support, the cloud, agent-based reloading, and what's next. Check it out!
  4. Thomas Risberg has announced the Spring Data Document support for MongoDB, release 1.0.0.M3. The changes and new features in Spring Data Document 1.0.0.M3 includes much improved mapping and conversion support. The MappingMongoConverter is now the default converter used by the MongoTemplate and the SimpleMongoConverter has been deprecated and will be removed. The concept of a default collection name has also been removed and all operations of the MongoTemplate are based on the collection name used for the entity class that is the target of the operation. The collection name used for an entity class defaults to the classname starting with a lower-case letter but it can be customized using the @Document annotation. See the changelog for more details.
  5. Milestone 5 of Virgo 3.0.0 is available for download. This is an important milestone which adds significant functional enhancements, upgrades several dependencies to their latest levels including Spring 3.0.5, Tomcat 7.0.12, and Servlet 3.0, and fixes a number of bugs. Full details are available in the release notes. The Virgo Web Server from EclipseRT is a completely module-based Java application server that is designed to run enterprise Java applications and Spring-powered applications with a high degree of flexibility and reliability. It offers a simple yet comprehensive platform to develop, deploy, and service enterprise Java applications.
  6. Marius Bogoevici - a Spring Integration committer - has written a fantastic post on the options for using a JPA EntityManager in JBoss AS with Spring. The main thrust of the post is that the application server automatically creates an EntityManager, by default, so there may be no need to recreate one in Spring - you can simply inject the existing reference. This approach is specifically to get around the presumptuous behavior of a full blown application server. If you'd like to run in Tomcat, then Spring's the easiest way to configure a JPA EntityManager. Marius also explains how to let Spring run the show entirely by disabling the application server behavior. This has the plural benefits of usually being more performant, and of keeping configuration with the application itself, not the server.
  7. Matt Raible has posted a follow up to his blog posts and screencasts on security in web applications. Previously, he demonstrated how to use Spring Security, Apache Shiro, and Java EE security in a pseudo identical fashion to secure a web application, highlighting the differences as appropriate. This follow up article talks about all three technologies and provides a comparison for enabling programmatic login when integrated in a Spring MVC application. The Spring Security support has been around for a long time and works in numerous containers (not Just Java EE 6 compliant containers) with no fuss. Nice!
  8. Have you dabbled in other JVM based languages? Have you taken a look at Scala? Well at the recent Scala Days conference in Palo Alto, CA, the Cloud Foundry team announced new Scala support on CloudFoundry.com!
  9. If you were at the S2G Forums in London last week, you would've received a free copy of the Open Source Journal - a printed (and freely downloadable .PDF) magazine. This publication has done a bang up job covering some of the Spring framework technologies. It's available from the publisher's web site as a free download. Check out the first and second issues here. The second issue, for example, has a great introduction to Spring.NET (including the new code configuration - the .NET analog to Spring Java's Java configuration), a look at Spring.NET's RestTemplate (a nice analog to Spring Java's RestTemplate), and a look at using Spring Integration (and Spring Web Services) to make short work of exposing web services. This format is especially ideal if you have a .PDF-capable e-reader or tablet PC. Check it out!

Countdown to Grails 2.0: Unit testing

Engineering | Peter Ledbrook | June 07, 2011 | ...

The first milestone of Grails 1.4 (now 2.0) has now been released and we are on the last stages of the journey towards 1.4 2.0 final. As we approach that point, I will be writing a series of blog posts that cover the various new features and changes that the 1.4 2.0 version brings. I'll be starting with the new testing support.

Since the beginning, Grails has provided three levels of testing support for developers: unit, integration, and functional. Unit tests had and still have the benefit of running independently of Grails, but they typically required a fair bit of extra work in the form of…

A Simple Groovy DSL for building RabbitMQ AMQP Applications

Engineering | Jon Brisbin | June 01, 2011 | ...

Asynchronous applications can sometimes be a challenge while you're developing them since you usually need two separate components to see the full message publication and consumption lifecycle. It often happens that you write a consumer that can dump messages to System.out or your log file, just so you can make sure your publisher is doing the right thing. It would be really handy if you could mock the message publication and consumption interaction in a single component so you could actually see what's going on.

The RabbitMQ Groovy DSL aims to help with this by providing a very concise and…

This week in Spring: May 31st, 2011

Engineering | Josh Long | May 31, 2011 | ...

The excitement continues today at the SpringSource S2G forums here in London! The energy leading up to the event has been staggering, and the talks - on a wide variety of deep, technical topics - are very impressive! I've had several of my questions answered, and learned a lot about some of the new, interesting, upcoming technologies from SpringSource. If you didn't get a chance to attend this year, we will be posting the session slides next week. Also don't forget, there is still SpringOne 2GX later this year (October) in Chicago!

  1. Many people love Spring Batch as soon as they give it a try, and many of those people then start trying to tell others about it precisely because it's so wonderful to know that they won't have to solve the problem themselves. Batch processing's something we all do at some point or another: moving data from database to another, reading from a file system, making web service calls and need to handle retry logic, etc. These use cases (and many more) are natural fits for Spring Batch. If you want to see one very succinct, useful introduction to the technology with an emphasis on code, check out Sanjoy Kumar Roy's blog introducing Spring Batch. Very cool! If you give Spring Batch a try and feel like you have something to add to the discussion, write a blog and ping me to let me know so I can highlight it on this page!.
  2. 	<li>
    		Roy Clarkson notes that starting May 28, 2011, the repositories for <a href="http://www.springsource.org/spring-android">Spring Android</a> and <A HREF ="http://www.springsource.org/spring-mobile">Spring Mobile</a> have moved to GitHub, and are available at the following URLs:
    
    	<div><b>Spring Android:<br/></b>
    		<UL><li><a href="https://github.com/SpringSource/spring-android">Spring Android</a></li>
    		<LI><A href="https://github.com/SpringSource/spring-android-samples">Spring Android Samples</a>
    			</li> </div>
    				<div><b>Spring Mobile:<br/></b>
    					<UL><li><a href="https…

Get the Spring newsletter

Stay connected with the Spring newsletter

Subscribe

Get ahead

VMware offers training and certification to turbo-charge your progress.

Learn more

Get support

Tanzu Spring offers support and binaries for OpenJDK™, Spring, and Apache Tomcat® in one simple subscription.

Learn more

Upcoming events

Check out all the upcoming events in the Spring community.

View all