Beginning Spring
FOREWORD
I have fond memories of Wrox books. The Spring story started 12 years ago from a Wrox book. Before that, I'd contributed chapters to several Wrox books and served as a reviewer on many others.
Some of these memories concern what the imprint has meant to me, but many relate to its philosophy and format, a key reason I was excited to become a Wrox author in 2000. The distinctive format offers an effective structure for imparting knowledge, encouraging the author to highlight important points, and promoting systematic explanation. It emphasizes practical code examples – the most valuable tool for teaching programming.
These virtues are evident in Mert Calıskan's and Kenan Sevindik's Beginning Spring. It's well structured, with plentiful examples that include clear instructions on getting the code running and step-by-step explanations.
Spring – like building enterprise Java applications itself – is a broad subject, making it impossible for an introductory book to cover even the core Spring Framework exhaustively. Mert and Kenan have done a good job in selecting what to focus on. The scope of this book is well chosen to get you productive as a Spring web developer: the core Dependency Injection container; the MVC framework (with a special emphasis on REST); relational data access and transaction management; the use of AOP and Spring EL to customize application behavior; and how Spring 4.0 embraces the important language enhancements in Java 8. Although Spring Security is the only Spring subproject covered, this book provides a solid base on which to build knowledge of the broad Spring ecosystem.
The level of the content is equally well targeted. There's a judicious mix of important background information (for example, the ACID properties of transactions) and specific detail about how to use Spring to get things done. Although this book assumes no knowledge of Spring, it does not waste time covering programming topics better served in more introductory books.
Although I'm no longer personally involved, I continue to observe Spring's progress with pleasure. Reading this book serves as a reminder of why Spring remains so relevant. The core concepts have stood up well over the past 12 years: for example, the consistent, lightweight approach to transaction management and the central principle of Dependency Injection. When you understand the “Spring way,” you can master additional Spring technologies quickly.
If you're a Java web developer as yet unfamiliar with Spring, you will find that Spring can make your life much easier, and I recommend this book as a good way to get started with it.
FOREWORD
The Java landscape keeps amazing me! Even after 11 years of leading the Spring Framework project, I'm learning about new fields of applications, new scenarios to optimize for, and new system architectures to consider. Software development constantly changes, and Spring doesn't just follow along – Spring keeps pushing. The Spring community often anticipates trends before they become heavily debated; for instance, people had been building microservice architectures with Spring long before the term microservice was coined. It is particularly interesting to see how many of the original design decisions behind the framework show their strengths again in 2014: Mechanisms introduced to decouple the framework from the application server infrastructure turn out to be very useful in adapting to modern embedded middleware.
A key mission behind Spring is to keep bringing the latest Java programming model styles to the widest possible range of deployment environments. In the Java community, we tend to put ourselves into straightjackets, not allowing ourselves to use our own current generation of APIs and frameworks. These constraints are primarily due to application server platforms, which contain outdated versions of the JDK and Java EE APIs, lagging behind for way too many years. This situation is particularly critical with Java 8 now: With lambda expressions, the new date-time types, collection streams, and so on, Java 8 has many things to offer that will change your source code style across the codebase. As a community, we need to be able to bring those features into our immediate environments – not just for development, but with full production support.
With Spring 4, we created a new baseline for 2014 and beyond. Spring's comprehensive annotation-oriented programming model is battle-tested, fine-tuned, and as coherent as possible now, and it's designed to be a stable foundation for years to come. We will be extending and refining the framework's functionality in the course of Spring Framework 4.x releases every year, as natural and fully compatible complements to the Spring 4 foundation. We will make a lot of this functionality available to JDK 6+ environments: for updates to existing applications, for deployments to existing data centers, and for corporate environments with conservative JDK policies. At the same time, you will see a strong focus on JDK 8+–oriented functionality with Spring, in particular toward stream- oriented programming and reactive system architectures.
Finally, I have a few personal recommendations to help you get the most out of Spring for your newly started development projects:
• First of all, get a good book on core Spring and its design philosophy. With this book, you are beyond that step already.
• Don't make compromises up front. Design a clean application architecture based on your understanding of your application's domain.
• Start with the latest Java infrastructure that you can possibly bring into production: JDK 8, Spring 4, Tomcat 8, Jetty 9, and so on.
• Don't forget to keep updating the frameworks and libraries along with the progress of your own project: Spring, in particular, has been designed for easy upgrades within an application, independent from any server installations.
Beginning Spring focuses on the key principles behind the Spring Framework 4 generation. It is a great way to get started with modern-day Spring development. So free yourself from artificial constraints, and enjoy developing with Spring!
INTRODUCTION
THE SPRING FRAMEWORK IS AN OPEN SOURCE ENTERPRISE APPLICATION framework licensed under Apache License version 2.0, which provides an extensive toolset for building applications that meet the enterprise demands. The idea itself came about because doing development with J2EE (the former version of Java Enterprise Edition) introduced a good deal of complexity in the enterprise world. Creating and deploying beans with Enterprise Java Beans (EJB) was a burden because, to create one bean, you had to create home and component interfaces along with the bean definition. Because the concept of dependency injection was also missing in the EJB world at those times, the lookup approach was the only way to find objects or resources.
The first version of the Spring Framework was based on the code published within Expert One-on-One J2EE Design and Development without EJB (Wrox, 2004) by Rod Johnson and Jürgen Höller. The main idea for the book was to reduce this complexity of enterprise application development with EJBs that was introduced to the EE world with J2EE. The first version of Spring was released in 2002, and milestone releases followed in 2004 and 2005. Version 1.0 brought the lightweight application framework along with features including JDBC abstraction, object relational mapping tools support, transaction management, scheduling and mail abstraction implementations, and the Model View Controller (MVC) web framework.
The 2.0 and 3.0 releases introduced important features to the framework, such as AspectJ support and REST support. At the time of writing this book, version 4.0 was released, and it now complies with the specifications provided by Java Enterprise Edition 7 and with Java 8 Standard Edition, which are the latest versions of Java. Within the book we also try to cover the features of the framework that ships with this latest edition.
Spring gained popularity with its core Dependency Injection (DI) pattern, which is also known as Inversion of Control (IoC). Because object-oriented programming introduces relationships between the objects, the DI approach tries to achieve a loose-coupled design by extracting the management of these dependencies to lead to an easy and manageable implementation. Another spectacular feature of Spring is provided by aspect-oriented programming (AOP), which offers an elegant approach to implementing the cross-cutting concerns in your application. Every web application is in need of features such as exception management, authentication