Have You Seen Spring Lately?


Josh Long gives us an extensive walk through everything that’s new with the Spring Framework, which went live with version 4.0 just yesterday.

Open source application framework Spring went live with version 4.0 yesterday. Billed as a development which, “keeps Spring at the cutting edge of modern Java development”, the revamped platform is designed with the upcoming Java 8 firmly in mind. In November’s JAX Magazine, Spring Developer Advocate Josh Long wrote a cohesive catch up to what’s new with Spring – and, in light of this week’s development, we thought it would be a perfect time to share it.

2013 has been a banner year for Spring! In April, we on the Spring team joined the Pivotal initiative, and we’re not alone! The new company is composed of Spring (including the Grails, RabbitMQ, Tomcat, Redis, andGemFire teams), Cloud Foundry (the open source PaaS (platform-as-a-service), and Greenplum (who have leadership in smart data warehousing and Hadoop, among other things).

Today, developers are being asked to handle more challenges then ever, and to do so more quickly. It can be a dizzying task to keep up with today’s technologies, especially given the ongoing innovation surrounding big-data, cloud computing, mobile, and web development, but Spring (and Pivotal (@gopivotal) has your back! Spring is part of a larger platform that we’re calling Spring IO.

In this article (originally published in JAX Magazine) we’ll look at the latest and greatest Spring projects and their place in the Spring IO platform.

 While you can pick up any library and use it a la carte, we hope that developers will enter the platform through one of the three technologies at theIO Execution tier. The IO Execution tier describes anything that manages things that ultimately manifest as processes on the operating system. TheIO Execution tier currently includes:

  • Spring XD for big-data, batch and integration work

  • Spring Boot for convention-over-configuration-centric and singularity-of-focus-centric Spring application development.

  • Grails for rapid web application development with the Groovy language.

From there, a developer may expect to use any of the Spring modules in the IO Foundation tier supporting integration, batch processing, big data, and web applications, all of which work with our data-access and processing APIs and enjoy the support of cross-cutting technologies like the core dependency injection container, the reactive Reactor framework, and the Spring Security framework.

Let’s look at some of the things that Spring makes easy.

A New Beginning for Spring and For You: Spring Has a New Home!

Spring’s new home on the web is is your one-stop shop for documentation, the forums, the blog, detailed project information, and new getting-started guides and tutorials. We put up a lot of great, technical content on the blog every week. Every tuesday, I put up a post called This Week in Spring which aggregates great blogs, tutorials, and videos from our team and from the Spring community.

“How Do I Get Started, Quickly?”
We’ve worked hard on the Spring team to answer the question: “how do I get started, quickly?” We’ve developed a two-pronged solution: better guidance / education and productive-by-default tools. 

(More) Guidance from the Source

Spring’s always had a good documentation story. Spring’s always given away the source code and documentation, even when everyone else was selling it. Spring itself was initially the example code given in the pages of a book that prescribed a better way forward for enterprise Java. That way forward has taken us further and further from those initial few steps as Spring has raced to meet the ever-moving horizons of technology. It can be easy to forget what those first few steps looked like.

We have revisited our “getting started” experience and created the task-focused getting-started guides that explain how to achieve a task in a short text that you should be able to work through in a lunchtime (or less!). Each guide starts with a finished, working code base against which you may check your work, and a blank, starter codebase to levelset and start fresh.

Suppose you’ve heard about REST and want to incorporate it in your application; just check out the Building a RESTful Web Service guide, written by experts on the Spring team. If you like, you can skip to the punchline, and you’ll get working code you can use. For a richer narrative, just follow along with the guide!

Bootstrap Your Productivity with Spring Boot! 

Whenever possible, these guides are written using Spring Boot. Spring Boot makes it easy to create stand-alone, production-grade Spring-based applications that can you can “just run.” Spring Boot takes an opinionated, convention-over-configuration view of the Spring platform and third-party libraries so you can get started with minimum fuss. It focuses attention on your application and away from the conceptual overhead.

Want to try it? Install the spring command line tool (you can use GVM, or Homebrew on OSX, or simply build the code), and then put the following into a file named service.groovy:

class Example {

    String home() {
        [message: 'Hello World']

This is a Groovy-language Spring MVC REST controller that simply returns an object with a property, message. On the command line, run spring run service.groovy and it will start up and give you a working REST endpoint which you can hit from any browser athttp:// The @Grab annotation tells Groovy to pull down a Java dependency (as you might do with Maven or Gradle, for example) that the Spring Boot project provides complete with all the libraries you’re likely to need for a given task.

Bootstrapping Your Development with our open-source and Eclipse-Based Spring Tool Suite and Groovy Tool Suite

If you’re a Java developer working in Eclipse then check out our open-source Eclipse-based Spring Tool Suite and Groovy/Grails Tool Suitedistributions. They layer on top of an existing instance of the latest-and-greatest Eclipse distribution or you can download them as separate IDEs. I think this last approach is the far more productive path, and, if you want anything else, just add it!


Dealing with Data

Spring provides a rich set of technologies to work with data.

Traditional RDMBSes

The core Spring framework has great support for JDBC, and JDBC-based ORM access. It includes a transaction API that insulates you from the (incompatible) nuances typical of transactional resources like SQL databases, JMS and AMQP message brokers, and JPA / Hibernate ORM clients. Spring’s support here is already legendary, and with Java 8’s developer previews already available, becomes even more expressive! Below us an example of using Java 8’s lambda expressions with Spring’s JdbcTemplate, a workhorse for JDBC-driven data access:

   User user = jdbcTemplate.queryForObject( 
      "select * from USERS where ID = ?" ,  
       (resultSet, rowNum ) -> new User( resultSet.getLong("ID"), resultSet.getString("EMAIL")),
       423L ) ;

NoSQL, NewSQL, and NOSQL with Spring Data

The Spring Data projects embrace the new breed of so-called NoSQL, NewSQL, or NOSQL (not-only-SQL) solutions. Spring Data is not one API to rule them all, instead it is an umbrella project with specific APIs for popular “database” technologies like Redis, MongoDB, and Neo4J. In addition, there are many third party bindings for projects like ElasticSearch, SOLR, CouchDB, and Riak.

One concept common to many Spring Data modules, including the Spring Data JPA module, is the notion of a repository. A Spring Data repository, named for the repository design pattern, is an interface that Spring Data implements at runtime using convention-over-configuration to dynamically build the implementation, typically by interacting with some backend resource. Here’s a Spring Data JPA repository that I can inject and use anywhere in my code. I’ve added in comments a rough example of the query that each method generates.

@RestResource (path = "users", rel = "users")  // automatically exposes a REST endpoint at /users/*
public interface UserRepository extends PagingAndSortingRepository<User, Long> {

    // select * from users where username = ?
    User findByUsername(@Param("username") String username); 

    // select * from users where firstName =? or lastName = ? or username = ?
    List<User> findUsersByFirstNameOrLastNameOrUsername(
      @Param("firstName") String firstName,
      @Param("lastName") String lastName,
      @Param("username") String username);


In this case I’ve used Spring Data JPA’s repository support, but I could’ve just as easily used Spring Data MongoDB’s, Spring Data Neo4j’s or Spring Data GemFire’s and those query methods would’ve instead mapped to those backend databases, instead of SQL. Note, also, [email protected] annotation provided by Spring Data REST. This annotation transparently maps the repository to a RESTful service endpoint and works in conjunction with Spring MVC.

That’s not too shabby for a single Java interface!

Surviving the Big-Data Wild, Wild West with Spring for Apache Hadoop

Today’s Hadoop ecosystem represents a fantastic new opportunity with some very familiar problems. Think back to 2003 – Spring was new on the scene – and being the developer that managed to Hibernate, Quartz and Struts working in the same web application ten years ago was the office rock-star!

These projects were developed in isolation by individual open-source projects absent each other’s use-cases and standards: they did not share common runtime concepts and did not share common API concepts like transaction management. In today’s world, using Hibernate and Struts (or Spring MVC) and Quartz in the same application is a snap thanks to Spring.

In the Hadoop ecosystem today, we find ourselves with the same sort of integration challenges and here again Spring offers a solution. Spring for Apache Hadoop makes working with Hadoop-ecosystem technologies like HBase, Hive, Pig, Cascading, and YARN from within standard Spring as easy as possible. In addition, Spring for Apache Hadoop can rely on platform niceties like property-placeholder resolution to ease configuration.

Spring for Apache Hadoop is known to work with the following Hadoop distributions:

  • Apache Hadoop 1.2.1 stable

  • Apache Hadoop 2.0.6-alpha

  • Apache Hadoop 2.1.0-beta

  • Cloudera CDH4

  • Hortonworks HDP 1.3

Pivotal HD 1.0 – this is the @GoPivotal Hadoop distribution. It includes the world’s first true SQL processing for enterprise-ready Hadoop, builds on top of Hadoop 2, is supported by EMC’s global, 24×7 support infrastructure, and is available as a seperate, easy-to-use single-node virtual machine for development.

To learn more about Spring for Apache Hadoop, check out these resources:

Online and Batch Processing of Data

Spring clearly knows how to talk to the data sources you’re probably most interested in talking to. The next question, of course, is how to process data from those datastores. Spring integration and data-processing solutions are rich.

Spring Integration to Build Messaging-Driven Architectures

Spring Integration is an integration framework in the style of the patterns in Gregor Hohpe and Bobby Woolf’s epic tome, Enterprise Integration Patterns. In the world of Enterprise Integration, systems are connected through channels (message queueus).

Messages flow from one component to another which can transform, enrich, split, aggregate and route messages. Spring Integration offers a set of adapters – components that adapt messages from external systems into standard Spring Integration messages. In this way, Spring Integration applications can integrate data and services from different systems and protocols like FTPS, XMPP, JDBC, SFTP, Hadoop’s HDFS, Twitter, Splunk, JDBC, JMS, AMQP, Twitter, and GemFire continuous-queries.

Batch Processing with Spring Batch to Avoid Idle Computational Resources

Spring Integration is all about reacting to events in realtime, about being able to integrate with data and services from disparate systems. Spring Batch is all about the safe processing of large amounts of sequential data like large datasets in, for example, a flat file, a JDBC database, or a large XML document. Its design is also largely intact in the Batch JSR, where we contributed a lot of leadership to make it the best possible standard for the community. As a specification, JSR 352 is necessarily vague on some poionts. In particular, it does not prescribe any item readers and writers out of the box. Users going to the JSR via the Spring Batch implementation will appreciate the rich set of readers and writers unique to Spring Batch.

Spring Batch jobs contain a sequence of one or more steps. Each step may optionally read (through its provided ItemReader), optionally process (through a provided ItemProcessor), and optionally write (through a provided ItemWriter).

Here’s an example of using Spring Integration to respond to new file system events (“there’s a new file in a monitored folder”) and then process the file using Spring Batch. The code is available in the resources below.


For more resources on using Spring Batch and Spring Integration:

Surfing the Tidal Data Deluge with Spring XD

Today’s applications are real-time, and work with lots of data. It’s one thing to setup a Hadoop cluster to handle batch processing and warehousing, but many developers today are struggling with a different problem: how do I absorb the load of data fast enough to get it into Hadoop in the first place? This challenge is called stream processing or data integration.

Spring XD puts these types of problems squarely in its cross hairs. Spring XD is built on top of Spring Integration and Spring Batch, so benefits from the rich set of adapters and the event-driven processing paradigm that Spring Integration brings with it, and it benefits from the data-processing- and job-orchestration smarts that Spring Batch brings with it.

In Spring XD, data comes in from a source and may then be processed, analysed and ultimately written to a sink. A given path through which data transits in Spring XD is called a stream. Spring XD is a turnkey runtime. Components are defined in terms of Spring Integration flows and Spring Batch jobs and given IDs, and then managed by Spring XD.

Spring XD components are orchestrated (and referenced by their ID) using a DSL that looks (uncannily!) like what Unix bash command-line incantation piping stdin and stdout from one singly-focused command line utility to another. This is no accident: Spring XD embraces the pipes and filters paradigm. The other benefit of this orchestration DSL is that it’s high-level enough that a business analyst or higher-level operator might use it. Spring XD can transparently scale out to accomodate load, distributing processing across multiple nodes.

An example of defining a stream is shown below: messages are expected from the time component and are passed to the log component, whatever these two components (time, and log) are and do is up to you. This is of course a very simple example. You can script very complex workflows in this way and change flows on the fly all from the Spring XD operator shell.

xd:> stream create --definition " time | log  " --name ticktock 

In the video below, I demonstrate sending orders through a Spring XD HTTP endpoint and then having that data transit through a machine learning library (Madlib) hosted in the Pivotal HD single-node virtual machine. Pivotal HD is Pivotal’s Hadoop distribution. The Madlib engine has a model, pre-trained with knowledge of certain types of fraudulent orders. It uses that model and linear regression to evaluate whether incoming orders are fraudulent, live. The results are reflected in the browser which updates every 5 seconds and highlights regions with fraudulent orders.


For more resources on using Spring XD:

A Richer Web with Spring: Build Web Applications and Web Services with Spring

Today’s web applications are as often as not REST-powered backends with rich JavaScript / HTML5 or native mobile front ends. REST is not a standard but a set of principles designed to promote the use and consumption of services that fully exploit, as opposed to merely transit through, HTTP.


Spring MVC (part of the Spring core framework) makes it dead simple to build RESTful web services, and there’s an even more concise syntax for building REST services in Spring framework 4’s new @RestController annotation:

package demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

public class GreetingController {

    public static class Greeting { 
        private String message; 

        public Greeting(String msg){
          this.message = msg;
        public String getMessage(){
          return this.message;

    public Greeting sayHi(@RequestParam String name){
        return new Greeting( "Hi, " + name +"!" );

Here’s an example of a REST web service that exposes information about user and customer entities under the /users and /customersendpoints. In the video I manipulate the REST service through regular old browser access and through a REST plugin. Note that the output is in JSON encoding.


Smarter Clients with Hypermedia and Spring HATEOAS

This is a good start, but we can do better. First, clients would need to know the deep-links a-priori to be able to access any other resources related to a given entity. In addition, there’s no accounting for state! Some resources are stateful. For example, it shouldn’t be possible to add something to a shopping cart after the user’s already checked out, but there’s nothing stopping a client from trying, anyway.

Hypermedia, as espoused in the HATEOAS design pattern, can help us here. Ultimately, HATEOAS is just about encoding metadata, orhypermedia, along with the response.

Spring HATEOAS Resource objects act as envelope for your data and carry links that describe relevant resources. Here’s a Spring MVC REST controller method that uses Spring HATEOAS:

 @RequestMapping(method = RequestMethod.DELETE, value = "/users/{user}")
    @ResponseBody Resource<User> deleteUser( @PathVariable Long userId ) {
        User user = crmService.removeUser(userId);
        Collection<Link> links = new ArrayList<Link>();

        // "/users/5"
        Link self = linkTo(methodOn(UserController.class)

        // "/users/5/customers"
        Link customers = linkTo(methodOn(UserController.class)
            .loadUserCustomers(userId)).withRel( "customers" );

        // "/users/5/photo"
        Link photo = linkTo(methodOn(UserProfilePhotoController.class)
            .loadUserProfilePhoto(userId)).withRel( "photo" );

        links.addAll(photo, customers, self);

        Resource<User> userResource = new Resource<User>(user, links);
        return new ResponseEntity<Resource<User>>(userResource, HttpStatus.OK);

In the example above, I return a Spring HATEOAS Resource object which contains a collection of Link objects and a payload of type User.User is the entity being returned to the client. The result looks something like this:

    "firstName": "Josh",
    "lastName": "Long",
    "username": "joshlong",
    "links" : [
        { "rel": "self", "href" :"http://localhost:8080/users/5" },
        { "rel": "photo", "href" :"http://localhost:8080/users/5/photo" },
        { "rel": "customers", "href" :"http://localhost:8080/users/5/customers" }

This extra metadata links to resources that have relevant relationships with this resource. Clients follow rels, not URIs, and can tailor their behavior based on the presence of absence of links. A link to pay for an order in a commerce application would be inappropriate when there’s nothing in the shopping cart, for example.


Here are some resources to get started with Spring and REST:


REST is a great approach to building HTTP-bound web services, but HTTP is essentially a one-sided conversation which only the client can initiate. Websockets is a standardized protocol for bi-directional, messaging-style communication between the client and the server. A secure version of the WebSocket protocol is implemented in Firefox 6, Safari 6, Google Chrome 14, Opera 12.10 and Internet Explorer 10.

Spring supports the related websocket JSR 356, but offers far more than just that, filling in the gaps where the JSR was (understandably) vague. Spring’s WebSocket support can work independently of the JSR, and offers support for STOMP-framed messaging over WebSockets. Spring also supports the Sock.js protocol, a superset of the WebSocket protocol that the popular Sock.js JavaScript client can use to work around firewall restrictions and offer graceful degradation of communication on older browsers where websockets may not be available.

Here is an example of a WebSocket-ready Spring MVC controller. The getPositions method handles all calls from the client to subscribe to events on the /positions destination. This is a passive channel that sends back positions at the time of the call.

The executeTrade method handles calls from the client into the Spring MVC websocket controller. Both methods are accompanied by an argument for the which Spring Security places in the request for us, automatically.

public class PortfolioController {

        private static final Log logger = LogFactory.getLog(PortfolioController.class);
        private final PortfolioService portfolioService;
        private final TradeService tradeService;

        public PortfolioController(PortfolioService portfolioService, TradeService tradeService) {
                this.portfolioService = portfolioService;
                this.tradeService = tradeService;

        public List<PortfolioPosition> getPositions(Principal principal) throws Exception {
                logger.debug("Positions for " + principal.getName());
                Portfolio portfolio = this.portfolioService.findPortfolio(principal.getName());
                return portfolio.getPositions();

        public void executeTrade(Trade trade, Principal principal) {
                logger.debug("Trade: " + trade);

        public String handleException(Throwable exception) {
                return exception.getMessage();


To see WebSockets in action, check out the following video.


Secure, Connected Web Applications and Web Services with Spring


Spring Security integrates well with enterprise authentication and authorization backends like SAML, Kerberos, pam, and LDAP. Spring Security provides best-of-breed, open web-ready, and enterprise-ready, security for modern applications. Spring Security supports – for example – acting as both an OAuth client and securing a service with OAuth and provides protection for modern web applications including [comprehensive CSRF (Cross Site Request Forgery) protection and session-fixation protection.

Here are some resources to help you get started with Spring Security:

Building on the Connected Web with Spring Social

Your users are online, connected, and social. It’s hard to ignore the opportunity presented as integration with service provider APIs like Facebook, Twitter, and Pinterest. There are many rich third party APIs with which to integrate today – SOA has finally arrived! – and by and large they’re all secured with either OAuth 1.0, OAuth 1.0.a or OAuth 2.0. I would be dishonest if I didn’t tell you that OAuth is an extraordinary pain to implement without support. Spring Social provides that support. Spring Social provides numerous API bindings that build on top of Spring Social’s authentication and authorization support. There are even more API bindings available through the rich ecosystem of third party providers that build on top of Spring Social!

Here, for example, is how we might use the Spring Social Facebook binding in one of our Spring MVC controllers:

public class HelloController {

    @Inject private Facebook facebook;

    public String helloFacebook(Model model) {
        return "hello";

In the following video, we walk through the Spring Social Showcase which demonstrates Spring Social making short work of typical “Sign in with ..” style forms and API communication. Third party bindings often add their own screens when they fork this particular project to showcase their binding’s unique features.


Here are some resources to get started with Spring Social:

The Mobile Web

Today’s users access the web in increasing numbers through their mobile devices. Mobile applications extend significantly the reach of your application to your users’ mobile devices (which never leave most people’s pockets!), tablets, cars, and more.

The numbers presented in the chart are numbers most developers can’t afford to ignore. Thankfully they don’t have to if they use Spring! When I look at that chart, it’s pretty clear to me that building for Android and iOS makes a lot of sense. These are rich platforms with native, idiomatic ways of doing things. Spring for Android helps you build connected Android clients, quicker. Particularly, it brings Spring framework’sRestTemplate to Android. RestTemplate is an HTTP client from the core Spring framework that makes it trivial to work with HTTP resources. Spring Social builds on top of the RestTemplate and Spring Social bindings can work on Android, too!

Spring Mobile makes it easy to serve mobile form-factor friendly HTML applications and handle mobile visitors in a device-appropriate way. This can be a useful approach when developing fallback experiences for platforms you don’t intend to prioritize. Many companies adopt such a strategy, in addition to native Android and iOS applications, to accomodate all the other platforms out there for which it may not be worthwhile to develop a native solution. This is also a future-proof strategy: future platforms will no doubt support a browser, and – if nothing else – can use your application through a mobile web applications.

Here’s an example of working with two applications, one an iOS Objective-C client (on the right) and the other a Spring for Android client (on the left). They both use OAuth to communicate with the Spring Security OAuth-secured REST service I introduced earlier.


Spring Runs Where Your Applications Run

Spring’s portability-story is second to none. Today, the deployment targets for modern applications vary considerably and this portability has become more important than ever.

..on Java EE

If you’re stuck on a Java EE application server, don’t worry! Spring features first class support with Java EE and even brings many of the new features in the latest generations of Java EE (in this case, Java EE 7) to you before they’re commonly available in a commercially supported application server. Spring supports many of the Java EE JSRs, and even the Spring team even helped shape a few of those that thare are valuable outside of an application server like the webocksets JSR (JSR 356), the batch processing JSR (JSR 352, which bares a strikingsimialarity to Spring Batch!). Some JSRs, like the caching JSR (JSR 107) are already supported in Spring applications and have been for a couple of years now, even though the final JSR hasn’t even shipped as part of Java EE yet! Spring 4 features great support for other Java EE 7 JSRs like the Date/Time API, JTA 1.2, JMS 2.0, JPA 2.1, Bean Validation 1.1, the new concurrency API in JSR-236, Servlet 3.1, and WebSockets (JSR 356).

..on Apache Tomcat

Happily, most developers are using lighter containers like Jetty and (by a wide margin!) Apache Tomcat. Spring runs just fine on these destinations. Apache Tomcat is the heart of Pivotal’s tcServer, which is a binary-compatible version of Apache Tomcat with extra goodness for management and monitoring, among other things. the Cloud

Spring’s ability to insulate you from the underlying platform makes it an ideal choice for the cloud, where deployment destinations vary and runtime services vary wildly. Spring runs in all cloud environments. It works on the usual suspects (e.g., Amazon Web Services) as well as more exotic destinations like Google App Engine where full on Java EE applications can’t hope to run because of Google’s JDK whitelist.

Spring works well in other PaaS environments like Heroku, OpenShift, and – in particular – it enjoys a special place in the sun on Cloud Foundry, the open-source Platform-as-a-service from Pivotal. Here, for example, is a Spring configuraton class that uses a feature called profiles, which let the developer define environment-specific configurations. This configuratoin class is meant to run on Cloud Foundry, and uses theCloudFactory API to dynamically look up the local javax.sql.DataSource instance. It’s easy to imagine defining other such configuration classes for dev, production, legacy application servers like GlassFish, and more as required to suit your application’s use cases!

public class CloudConfig {

    @Bean public Cloud cloud()  throws CloudException {
            CloudFactory cloudFactory = new CloudFactory();
            return cloudFactory.getCloud();
    @Bean public DataSource dataSource() {
            return cloud().getSingletonServiceConnector(DataSource.class, null);   

This is Just the Beginning…

We’ve taken a look at the rich Spring IO platform and – I hope – looked at enough of the various technologies that you feel like you can take the next steps.

Spring continues to lead the charge, empowering modern developers “to move at the speed (and scale) of business.” It’s growing better and better and – as part of Pivotal – it’s happening even faster. The community is a huge, and most important, part of that. My name is Josh Long, I’m the Spring Developer Advocate, and I’d love to continue the discussion with you online. Don’t hesitate to reach me (@starbuxman) or the rest of the Spring team (@SpringCentral) on Twitter. We’re always happy to hear from you, and help wherever possible.







Josh Long (@starbuxman, and [email protected]) is the Spring developer advocate. Josh is the lead author on Apress

Inline Feedbacks
View all comments