Spring in Action, 4th Edition

Spring in Action, 4th Edition

English | 2014 | ISBN: 978-1-6172-9120-3 | 626 Pages | PDF | 10 MB


Spring in Action, Fourth Edition is a hands-on guide to the Spring Framework, updated for version 4. It covers the latest features, tools, and practices including Spring MVC, REST, Security, Web Flow, and more. You’ll move between short snippets and an ongoing example as you learn to build simple and efficient J2EE applications. Author Craig Walls has a special knack for crisp and entertaining examples that zoom in on the features and techniques you really need.
Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.
About the Technology
Designed in 2003 as a lighter approach to J2EE development, Spring Framework has since become a standard choice for building enterprise applications and required knowledge for Java developers. Spring 4, the latest major version, provides full Java 8 integration along with key upgrades like new annotations for the IoC container, improvements to Spring Expression Language, and much-needed support for REST. Whether you’re just discovering Spring or you want to absorb the new features, there’s no better way to master Spring than with this book.
About the Book
Spring in Action, Fourth Edition is a hands-on guide to the Spring Framework. It covers Spring core, along with the latest updates to Spring MVC, Security, Web Flow, and more. You’ll move between short snippets and an ongoing example as you learn to build simple and efficient JEE applications. Author Craig Walls has a special knack for crisp and entertaining examples that zoom in on the features and techniques you really need.
Nearly 100,000 developers have used this book to learn Spring! It requires a working knowledge of Java.
What’s Inside
Updated for Spring 4
Spring Data for NoSQL
Simplifying configuration with annotations and definition profiles
Working with RESTful resources
About the Author
Craig Walls is a software developer at Pivotal. He’s a popular author and a frequent speaker at user groups and conferences. Craig lives in Cross Roads, Texas.

+

Spring on the web

Building Spring web applications

In this chapter, you’ve made a good start on the web portion of your application. As you’ve seen, Spring comes with a powerful and flexibleweb framework. Employing
annotations, Spring MVCoffers a near-POJOdevelopment model, making simple work of developing controllers that handle requests and are easy to test. Listing 5.19 processRegistration(): ensures that data submitted is valid Validate Spitter input Return to form on  validation errors

When it comes to writing controller handler methods, Spring MVCis extremely flexible. As a rule of thumb, if your handler method needs something, then it should ask for that object as a parameter. Likewise, anything it doesn’t need should be left out of the parameter list. This leads to infinite possibilities in request handling, while maintaining a simple programming model.

Although much of this chapter focused on request handlingwith controllers, response rendering is also important. We briefly looked at how to write views for your controllers using JSPs. But there’s more to Spring MVCviews than the basic JSPs you wrote in this chapter.

Coming up in chapter 6, we’ll dig deeper into Spring views, expanding on how you can take advantage of Spring tag libraries in JSP. You’ll also see how to add consistent
layouts to your views using Apache Tiles. And we’ll look at Thymeleaf, an exciting alternative to JSPthat comes with built-in Spring support.

Hitting the database with Spring and JDBC

Data is the lifeblood of an application. Some of the data-centric among you may even contend that data isthe application. With such significance placed on data, it’s important that you develop the data-access portion of your applications in a way that’s robust, simple, and clear.

JDBCis the most basic way to work with relational data in Java. But as defined in the specification, JDBCcan be somewhat unwieldy. Spring takes much of the pain out of working with JDBC, eliminating boilerplate code and simplifying JDBCexception handling, leaving you little more to deal with than writing the SQLthat should be performed.

In this chapter, we looked at Spring’s support for data persistence. We also looked at Spring’s template-based abstraction for JDBC, which greatly simplifies working with
JDBC.

Coming up in the next chapter, we’ll continue our survey of Spring’s datapersistence support by looking at Spring’s facilities for the Java Persistence API.

Working with NoSQL databases

Gone are the days when the only choice for data persistence was a relational database. Now there are several different kinds of databases, each representing data in different forms and offering capabilities to suit a variety of domain models. The Spring Data project enables developers to use these databases in their Spring applications and to use abstractions that are reasonably consistent across the various database choices. In this chapter, we built on what you learned about Spring Data in the previous
chapter when using JPA, applying it to the MongoDB document database and the Neo4j graph database. Just like their JPAcounterpart, the Spring Data MongoDB and
Spring Data Neo4j projects both offer automatic generation of repositories based on interface definitions. Additionally, you saw how to use the annotations provided by the
Spring Data projects to map domain types to documents,nodes, and relationships. Spring Data also enables data to be persisted to the Redis key-value store. Key-value
stores are significantly simpler and thus do not require support for automatic repositories or mapping annotations. Nevertheless, Spring DataRedis offers two different
template classes for working with the Redis key-value store.

No matter what kind of database you choose, fetching data from the database is a costly operation. In fact, database queries are often the biggest performance bottlenecks in any application. Now that you’ve seen how to store and fetch data from a variety of data sources, let’s look at how to avoid that bottleneck. In the next chapter, you’ll see how to apply declarative caching to prevent unnecessary database fetches.