xvi PREFACE Since I couldn't fit everything into one volume,I was choosy about what topics made it into this edition.I decided to focus on what I believe are the core Spring top- ics that most Spring developers should know.That's not to say that the other topics aren't important,but these are the essentials of Spring development. The other reason this edition is smaller is due to the fact that while Spring's reach has continued to expand,it has continued to become simpler with each release Spring's rich set of configuration namespaces, adoption of annotation-driven pro gramming models,and application of sensible conventions and defaults have reduced Spring configuration from page upon page of XML down to only a handful of elements But make no mistake:though there are fewer pages,I've still managed to pack a lot of new Spring goodness into them.Along with the dependency injection,AOP,and declarative transactions Spring has long provided,here's a sampling of the stuff you'll learn in this edition that's new or changed since the second edition: Annotation-based bean wiring that dramatically reduces the amount of Spring XML configuration "A new expression language for evaluating values wired into bean properties dynamically at runtime Spring's all-new annotation-driven Spring MVC framework,which is far more flexible than the former hierarchical controller framework Securing Spring applications with Spring Security,much simpler now with a new configuration namespace,convenient defaults,and support for expression- oriented security rules First-class support for building and consuming REST resources,based on Spring MVC Whether you're new to Spring or a Spring veteran,I hope that you'll find this book to be an indispensable guide as you use Spring in your projects
xvi PREFACE Since I couldn’t fit everything into one volume, I was choosy about what topics made it into this edition. I decided to focus on what I believe are the core Spring topics that most Spring developers should know. That’s not to say that the other topics aren’t important, but these are the essentials of Spring development. The other reason this edition is smaller is due to the fact that while Spring’s reach has continued to expand, it has continued to become simpler with each release. Spring’s rich set of configuration namespaces, adoption of annotation-driven programming models, and application of sensible conventions and defaults have reduced Spring configuration from page upon page of XML down to only a handful of elements. But make no mistake: though there are fewer pages, I’ve still managed to pack a lot of new Spring goodness into them. Along with the dependency injection, AOP, and declarative transactions Spring has long provided, here’s a sampling of the stuff you’ll learn in this edition that’s new or changed since the second edition: ■ Annotation-based bean wiring that dramatically reduces the amount of Spring XML configuration ■ A new expression language for evaluating values wired into bean properties dynamically at runtime ■ Spring’s all-new annotation-driven Spring MVC framework, which is far more flexible than the former hierarchical controller framework ■ Securing Spring applications with Spring Security, much simpler now with a new configuration namespace, convenient defaults, and support for expressionoriented security rules ■ First-class support for building and consuming REST resources, based on Spring MVC Whether you’re new to Spring or a Spring veteran, I hope that you’ll find this book to be an indispensable guide as you use Spring in your projects. Licensed to Christian Cederquist <chrisman@kaus.dk>
acknowledgments Before you put your hands on this book,it was touched by many other hands-hands that edited it,reviewed it,proofread it,and managed the whole publishing process. You wouldn't be reading this book if it weren't for all those hands. First,I'd like to thank everyone at Manning for working hard,pressuring me to get this darn thing done,and for doing their part to make sure that this book is the best it could be:Marjan Bace.Michael Stephens,Christina Radloff,Karen Tegtmeyer. Maureen Spencer,Mary Piergies,Sebastian Stirling,Benjamin Berg,Katie Tennant, Janet Vail,and Dottie Marsico. Along the way,a handful of other people were given the oppo ortunity to read the manuscript in its roughest form and provide feedback,telling me what I got right and (gasp)where I missed the mark.Many thanks to all of those reviewers for their valu- able feedback:Valentin Crettaz,Jeff Addison,John Ryan,Olivier Nouguier,Joshua White,Deiveehan Nallazhagappan,Adam Taft,Peter Pavlovich,Mykel Alvis,Rick Wagner,Patrick Steger,Josh Devins,Dan Alford,Alberto Lagna,Dan Dobrin,Robert Hanson,Chad Davis,Carol McDonald,Deepak Vohra,and Robert O'Connor.And a special thanks to Doug Warren for taking on the role of technical reviewer and going over the technical details of the book with a fine-toothed comb. My gratitude is also due to those who played no direct part in producing the book, but were there providing support,friendship,good conversation,and making sure that I had adequate breaks from writing to doother things. First and foremost,thanks to my wife Raymie.You're my best friend,the love of my life,and the reason for everything I do.I love you very much.Thank you for putting up with another writing project and for supporting me xvii
xvii acknowledgments Before you put your hands on this book, it was touched by many other hands—hands that edited it, reviewed it, proofread it, and managed the whole publishing process. You wouldn’t be reading this book if it weren’t for all those hands. First, I’d like to thank everyone at Manning for working hard, pressuring me to get this darn thing done, and for doing their part to make sure that this book is the best it could be: Marjan Bace, Michael Stephens, Christina Rudloff, Karen Tegtmeyer, Maureen Spencer, Mary Piergies, Sebastian Stirling, Benjamin Berg, Katie Tennant, Janet Vail, and Dottie Marsico. Along the way, a handful of other people were given the opportunity to read the manuscript in its roughest form and provide feedback, telling me what I got right and (gasp) where I missed the mark. Many thanks to all of those reviewers for their valuable feedback: Valentin Crettaz, Jeff Addison, John Ryan, Olivier Nouguier, Joshua White, Deiveehan Nallazhagappan, Adam Taft, Peter Pavlovich, Mykel Alvis, Rick Wagner, Patrick Steger, Josh Devins, Dan Alford, Alberto Lagna, Dan Dobrin, Robert Hanson, Chad Davis, Carol McDonald, Deepak Vohra, and Robert O’Connor. And a special thanks to Doug Warren for taking on the role of technical reviewer and going over the technical details of the book with a fine-toothed comb. My gratitude is also due to those who played no direct part in producing the book, but were there providing support, friendship, good conversation, and making sure that I had adequate breaks from writing to do other things. First and foremost, thanks to my wife Raymie. You’re my best friend, the love of my life, and the reason for everything I do. I love you very much. Thank you for putting up with another writing project and for supporting me. Licensed to Christian Cederquist <chrisman@kaus.dk>
xi进 ACKNOWLEDGMENTS To Maisy and Madi,my little princesses,thank you for your hugs,laughs,imagina tion,and the occasional Mario Kart breaks. To my colleagues at SpringSource,thank you for continuing to revolutionize how we develop software and for giving me the opportunity to be a part of what you do. Special thanks to the two SpringSourcers I work with every day,Keith Donald and Roy Clarkson-we've done some awesome stuff in the past year and I look forward to the amazing things that lie ahead Many thanks to my No Fluff/Just Stuff cohorts for reminding me every few week- ends that I'm not nearly as smart as you guys:Ted Neward,Venkat Subramaniam,Tim Berglund,Matthew McCullough,Matt Stine,Brian Goetz,Jeff Brown,Dave Klein,Ken Sipe,Nathaniel Schutta,Neal Ford,Pratik Patel,Rohit Bhardwaj,Scott Davis,Mark Richards,and of course,Jay Zimmerman. Finally,there are many other folks out there that I'd like to send a shout out to for their part in shaping me,my career,and this book:Ryan Breidenbach,Ben Rady, Mike Nash,Matt Smith,John Woodward,Greg Vaughn,Barry Rogers,Paul Holser. Derek Lane,Erik Weibust,and Andrew Rubalcaba
xviii ACKNOWLEDGMENTS To Maisy and Madi, my little princesses, thank you for your hugs, laughs, imagination, and the occasional Mario Kart breaks. To my colleagues at SpringSource, thank you for continuing to revolutionize how we develop software and for giving me the opportunity to be a part of what you do. Special thanks to the two SpringSourcers I work with every day, Keith Donald and Roy Clarkson—we’ve done some awesome stuff in the past year and I look forward to the amazing things that lie ahead. Many thanks to my No Fluff/Just Stuff cohorts for reminding me every few weekends that I’m not nearly as smart as you guys: Ted Neward, Venkat Subramaniam, Tim Berglund, Matthew McCullough, Matt Stine, Brian Goetz, Jeff Brown, Dave Klein, Ken Sipe, Nathaniel Schutta, Neal Ford, Pratik Patel, Rohit Bhardwaj, Scott Davis, Mark Richards, and of course, Jay Zimmerman. Finally, there are many other folks out there that I’d like to send a shout out to for their part in shaping me, my career, and this book: Ryan Breidenbach, Ben Rady, Mike Nash, Matt Smith, John Woodward, Greg Vaughn, Barry Rogers, Paul Holser, Derek Lane, Erik Weibust, and Andrew Rubalcaba. Licensed to Christian Cederquist <chrisman@kaus.dk>
about this book The Spring Framework was created with a very specific goal in mind-to make devel oping Java EE applications easier.Along the same lines,Spring in Action,Third Edition was written to make learning how to use Spring easier.My goal is not to give you a blow-by-blow listing of Spring APIs.Instead,I hope to present the Spring Framework in a way that is most relevant to a Java EE developer by providing practical code exam- ples from real-world experiences.Since Spring is a modular framework,this book was written in the same way.I recognize that not all developers have the same needs.Some may want to learn the Spring Framework from the ground up,while others may want to pick and choose different topics and go at their own pace.That way,the book can act as a tool for learning Spring for the first time as well as a guide and reference for those wanting to dig deeper into specific features. Who should read this book Spring in Action,Third Edition,is for all Java developers,but enterprise Java developers will find it particularly useful.While I will guide you along gently through code exam- ples that build in complexity throughout each chapter,the true power of Spring lies in its ability to make enterprise applications easier to develop.Therefore,enterprise developers will most fully appreciate the examples presented in this book. Because a vast portion of Spring is devoted to providing enterprise services,many parallels can be drawn between Spring and EJB.Therefore,any experience you have will be useful in making comparisons between these two frameworks.A portion of this book is dedicated to this topic.In fact,the final five chapters demonstrate how Spring 好
xix about this book The Spring Framework was created with a very specific goal in mind—to make developing Java EE applications easier. Along the same lines, Spring in Action, Third Edition was written to make learning how to use Spring easier. My goal is not to give you a blow-by-blow listing of Spring APIs. Instead, I hope to present the Spring Framework in a way that is most relevant to a Java EE developer by providing practical code examples from real-world experiences. Since Spring is a modular framework, this book was written in the same way. I recognize that not all developers have the same needs. Some may want to learn the Spring Framework from the ground up, while others may want to pick and choose different topics and go at their own pace. That way, the book can act as a tool for learning Spring for the first time as well as a guide and reference for those wanting to dig deeper into specific features. Who should read this book Spring in Action, Third Edition, is for all Java developers, but enterprise Java developers will find it particularly useful. While I will guide you along gently through code examples that build in complexity throughout each chapter, the true power of Spring lies in its ability to make enterprise applications easier to develop. Therefore, enterprise developers will most fully appreciate the examples presented in this book. Because a vast portion of Spring is devoted to providing enterprise services, many parallels can be drawn between Spring and EJB. Therefore, any experience you have will be useful in making comparisons between these two frameworks. A portion of this book is dedicated to this topic. In fact, the final five chapters demonstrate how Spring Licensed to Christian Cederquist <chrisman@kaus.dk>
ABOUT THIS BOOK support enterprise integration of web applications.If you are an enterprise appli- cation developer,you will find the last part of this book especially valuable. Roadmap Spring in Action,Third Edition,is divided into three parts.The first part introduces you to the essentials of the Spring Framework.Part 2 goes beyond that by delving into the common elements of a Spring application.The final part shows how Spring can be used to integrate with other applications and services. In part 1,you'll explore dependency injection(DI)and aspect-oriented program- ming(AOP),two essential features of the Spring Framework.This will give you a good understanding of Spring's fundamentals that will be utilized throughout the book. In chapter 1,you'll be introduced to DI and AOP and how they lend themselves to developing loosely coupled Java applications Chapter 2 takes a more detailed look at how to configure and associate your appli- cation objects using dependency injection.You'll learn how to write loosely coupled components and wire their dependencies and properties within the Spring container using XML. Once you have the basics of Spring XML configuration down,chapter 3 will pres ent annotation-oriented alternatives to XML configuration. Chapter 4 explores how to use Spring's AoP to decouple cross-cutting concerns from the objects that they service.This chapter also sets the stage for later chapters where you'll use Spring AOP to provide declarative services such as transactions,secu- rity,and caching. Part 2 builds on the DI and AOP features introduced in part 1,and shows you how to apply these concepts to build the common elements of an application. Chapter 5 covers Spring's support for data persistence.You'll be introduced to Springs JDBC support,which helps you remove much of the boilerplate code associ- ated with JDBC.You'll also see how Spring integrates with persistence frameworks such as Hibernate and the Java Persistence API (JPA). Chapter 6 complements chapter 5,showing you how to ensure integrity in your database using Spring's transaction support.You'll see how Spring uses AOP to give simple application objects the power of declarative transactions Chapter 7 introduces you to Spring's MVC web framework.You'll discover how Spring can transparently bind web parameters to your business objects and provide validation and error handling at the same time.You'll also see how easy it is to add functionality to your web applications using Spring MVC controllers. Chapter 8 explores Spring Web Flow,an extension to Spring MVC that enables development of conversational web applications.In this chapter you'll learn how to build web applications that guide the user through a specific flow. In chapter 9 you'll learn how to apply security to your application using Spring Security.You'll see how Spring Security secures applications both at the web reques level using servlet filters and at the method level using Spring AOP
xx ABOUT THIS BOOK can support enterprise integration of web applications. If you are an enterprise application developer, you will find the last part of this book especially valuable. Roadmap Spring in Action, Third Edition, is divided into three parts. The first part introduces you to the essentials of the Spring Framework. Part 2 goes beyond that by delving into the common elements of a Spring application. The final part shows how Spring can be used to integrate with other applications and services. In part 1, you’ll explore dependency injection (DI) and aspect-oriented programming (AOP), two essential features of the Spring Framework. This will give you a good understanding of Spring’s fundamentals that will be utilized throughout the book. In chapter 1, you’ll be introduced to DI and AOP and how they lend themselves to developing loosely coupled Java applications. Chapter 2 takes a more detailed look at how to configure and associate your application objects using dependency injection. You’ll learn how to write loosely coupled components and wire their dependencies and properties within the Spring container using XML. Once you have the basics of Spring XML configuration down, chapter 3 will present annotation-oriented alternatives to XML configuration. Chapter 4 explores how to use Spring’s AOP to decouple cross-cutting concerns from the objects that they service. This chapter also sets the stage for later chapters, where you’ll use Spring AOP to provide declarative services such as transactions, security, and caching. Part 2 builds on the DI and AOP features introduced in part 1, and shows you how to apply these concepts to build the common elements of an application. Chapter 5 covers Spring’s support for data persistence. You’ll be introduced to Spring’s JDBC support, which helps you remove much of the boilerplate code associated with JDBC. You’ll also see how Spring integrates with persistence frameworks such as Hibernate and the Java Persistence API (JPA). Chapter 6 complements chapter 5, showing you how to ensure integrity in your database using Spring’s transaction support. You’ll see how Spring uses AOP to give simple application objects the power of declarative transactions. Chapter 7 introduces you to Spring’s MVC web framework. You’ll discover how Spring can transparently bind web parameters to your business objects and provide validation and error handling at the same time. You’ll also see how easy it is to add functionality to your web applications using Spring MVC controllers. Chapter 8 explores Spring Web Flow, an extension to Spring MVC that enables development of conversational web applications. In this chapter you’ll learn how to build web applications that guide the user through a specific flow. In chapter 9 you’ll learn how to apply security to your application using Spring Security. You’ll see how Spring Security secures applications both at the web request level using servlet filters and at the method level using Spring AOP. Licensed to Christian Cederquist <chrisman@kaus.dk>