Table Of ContentSpring 5.0 Cookbook
(cid:51)(cid:70)(cid:68)(cid:74)(cid:81)(cid:70)(cid:84)(cid:2)(cid:85)(cid:80)(cid:2)(cid:67)(cid:86)(cid:74)(cid:77)(cid:69)(cid:14)(cid:2)(cid:85)(cid:70)(cid:84)(cid:85)(cid:14)(cid:2)(cid:66)(cid:79)(cid:69)(cid:2)(cid:83)(cid:86)(cid:79)(cid:2)(cid:52)(cid:81)(cid:83)(cid:74)(cid:79)(cid:72)(cid:2)(cid:66)(cid:81)(cid:81)(cid:77)(cid:74)(cid:68)(cid:66)(cid:85)(cid:74)(cid:80)(cid:79)(cid:84)(cid:2)(cid:70)(cid:96)(cid:68)(cid:74)(cid:70)(cid:79)(cid:85)(cid:77)(cid:90)
Sherwin John Calleja Tragura
BIRMINGHAM - MUMBAI
Spring 5.0 Cookbook
Copyright (cid:97) 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or
transmitted in any form or by any means, without the prior written permission of the
publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the
information presented. However, the information contained in this book is sold without
warranty, either express or implied. Neither the author, nor Packt Publishing, and its
dealers and distributors will be held liable for any damages caused or alleged to be caused
directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
First published: September 2017
Production reference: 1250917
(cid:49)(cid:86)(cid:67)(cid:77)(cid:74)(cid:84)(cid:73)(cid:70)(cid:69)(cid:2)(cid:67)(cid:90)(cid:2)(cid:49)(cid:66)(cid:68)(cid:76)(cid:85)(cid:2)(cid:49)(cid:86)(cid:67)(cid:77)(cid:74)(cid:84)(cid:73)(cid:74)(cid:79)(cid:72)(cid:2)(cid:45)(cid:85)(cid:69)(cid:16)
(cid:45)(cid:74)(cid:87)(cid:70)(cid:83)(cid:90)(cid:2)(cid:49)(cid:77)(cid:66)(cid:68)(cid:70)
(cid:21)(cid:23)(cid:2)(cid:45)(cid:74)(cid:87)(cid:70)(cid:83)(cid:90)(cid:2)(cid:52)(cid:85)(cid:83)(cid:70)(cid:70)(cid:85)
(cid:35)(cid:74)(cid:83)(cid:78)(cid:74)(cid:79)(cid:72)(cid:73)(cid:66)(cid:78)
(cid:35)(cid:21)(cid:2)(cid:20)(cid:49)(cid:35)(cid:14)(cid:2)(cid:54)(cid:44)(cid:16)
ISBN 978-1-78712-831-6
(cid:88)(cid:88)(cid:88)(cid:16)(cid:81)(cid:66)(cid:68)(cid:76)(cid:85)(cid:81)(cid:86)(cid:67)(cid:16)(cid:68)(cid:80)(cid:78)
Credits
Author Copy Editor
Sherwin John Calleja Tragura Safis Editing
Reviewer Project Coordinator
Glenn Base De Paula Vaidehi Sawant
Commissioning Editor Proofreader
Aaron Lazar Safis Editing
Acquisition Editor Indexer
Sandeep Mishra Francy Puthiry
Content Development Editor Graphics
Zeeyan Pinheiro Abhinash Sahu
Technical Editor Production Coordinator
Ketan Kamble Nilesh Mohite
About the Author
Sherwin John Calleja Tragura started his career as a student assistant and a mathematics
tutor during his college years at the University of the Philippines Los Ba(cid:98)os, Laguna,
Philippines. With meager resources, he graduated as a Department of Science and
Technology (DOST) R.A. 7687 scholar under the Bachelor Of Computer Science degree.
Immediately after graduation, he took up the offer to teach CMSC 150 (numerical and
symbolic computation) at the Institute of Computer Science and completed his master's
degree in computer science simultaneously. He became part of the International Rice
Research Institute (IRRI) software team, which gave him the opportunity to use Struts,
Spring, and RCP frameworks in many of its software projects.
Based on his experience at IRRI, he was given an opportunity to work as a Java analyst in
various companies in Manila, such as ABSI, PHILAM- AIG, and Ayala Systems and
Technology Inc. (ASTI). These companies have strengthened his skill set through training in
Java and Java Enterprise platforms and some popular tools such as EMC Documentum,
Alfresco Document, and Records Management System. He got his first career certification in
the EMC Documentum Proven Associate course (E20-120).
After a few years, he decided to become an independent consultant and trainer, providing
services mostly for Java-based projects, Alfresco, and Apache OFBiz requirements. He
started his venture as a Java-JEE Bootcamp with 77Global and is currently a trainer at
Software Laboratory Inc. (SLI), Alibata Business and Technology Services Inc. and Nityo
Infotech, Philippines. He also conducts training and talks around the Philippines, for
instance, in Cebu City and Tacloban City.
Sherwin has contributed as a technical reviewer to various books by Packt Publishing,
including Delphi Cookbook, Alfresco 3 Records Management, Alfresco Share, and Mastering
Hibernate. He owes everything to Packt Publishing with the unforgettable experience in the
technical reviewing tasks, which have been an essential part of his career.
As an Oracle Certified Associate and Java SE 7 Programmer (1Z0-803), Sherwin will
continue his mandate as a technical trainer, developer, architect, and designer to help the
industry improve its standards in information technology. He will always be an epitome of
honor, excellence, and service when it comes to software development and business
intelligence.
About the Reviewer
Glenn De Paula is a product of the University of the Philippines Integrated School and is a
computer science graduate of the country(cid:99)s most prestigious University of the Philippines.
He has 12 years of industry experience, most of which he got working for the government(cid:99)s
ICT institute and recently in the banking industry.
He uses Spring, Grails, and Javascript for his day-to-day activities. He has developed
numerous Java web applications for the government and has also been the technical team
lead for several projects. He currently manages Java developers assigned to different
projects in one of the country(cid:99)s most reputable banks.
He is consistently involved in systems analysis and design, source code review, testing,
implementation, training, and mentoring. He is currently learning NodeJS and Blockchain
technologies in his free time.
I would like to thank the author of this book, the editors, and Packt Publishing for giving
me the opportunity to review this great informative book.
I would also like to thank my managers and supervisors for mentoring me and trusting me
with projects that helped improve my career.
Thank you very much to my family and friends for all their support. Especially, I thank my
wife, Elaine, for all the love and patience.
www.PacktPub.com
For support files and downloads related to your book, please visit (cid:88)(cid:88)(cid:88)(cid:16)(cid:49)(cid:66)(cid:68)(cid:76)(cid:85)(cid:49)(cid:86)(cid:67)(cid:16)(cid:68)(cid:80)(cid:78).
Did you know that Packt offers eBook versions of every book published, with PDF and
ePub files available? You can upgrade to the eBook version at (cid:88)(cid:88)(cid:88)(cid:16)(cid:49)(cid:66)(cid:68)(cid:76)(cid:85)(cid:49)(cid:86)(cid:67)(cid:16)(cid:68)(cid:80)(cid:78) and as a
print book customer, you are entitled to a discount on the eBook copy. Get in touch with us
at (cid:84)(cid:70)(cid:83)(cid:87)(cid:74)(cid:68)(cid:70)(cid:33)(cid:81)(cid:66)(cid:68)(cid:76)(cid:85)(cid:81)(cid:86)(cid:67)(cid:16)(cid:68)(cid:80)(cid:78) for more details.
At (cid:88)(cid:88)(cid:88)(cid:16)(cid:49)(cid:66)(cid:68)(cid:76)(cid:85)(cid:49)(cid:86)(cid:67)(cid:16)(cid:68)(cid:80)(cid:78), you can also read a collection of free technical articles, sign up for a
range of free newsletters and receive exclusive discounts and offers on Packt books and
eBooks.
(cid:73)(cid:85)(cid:85)(cid:81)(cid:84)(cid:28)(cid:17)(cid:17)(cid:88)(cid:88)(cid:88)(cid:16)(cid:81)(cid:66)(cid:68)(cid:76)(cid:85)(cid:81)(cid:86)(cid:67)(cid:16)(cid:68)(cid:80)(cid:78)(cid:17)(cid:78)(cid:66)(cid:81)(cid:85)
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt
books and video courses, as well as industry-leading tools to help you plan your personal
development and advance your career.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Customer Feedback
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial
process. To help us improve, please leave us an honest review on this book's Amazon page
at (cid:73)(cid:85)(cid:85)(cid:81)(cid:84)(cid:28)(cid:17)(cid:17)(cid:88)(cid:88)(cid:88)(cid:16)(cid:66)(cid:78)(cid:66)(cid:91)(cid:80)(cid:79)(cid:16)(cid:68)(cid:80)(cid:78)(cid:17)(cid:69)(cid:81)(cid:17)(cid:19)(cid:25)(cid:26)(cid:25)(cid:19)(cid:20)(cid:26)(cid:21)(cid:19)(cid:26)(cid:16)
If you'd like to join our team of regular reviewers, you can e-mail us at
(cid:68)(cid:86)(cid:84)(cid:85)(cid:80)(cid:78)(cid:70)(cid:83)(cid:83)(cid:70)(cid:87)(cid:74)(cid:70)(cid:88)(cid:84)(cid:33)(cid:81)(cid:66)(cid:68)(cid:76)(cid:85)(cid:81)(cid:86)(cid:67)(cid:16)(cid:68)(cid:80)(cid:78). We award our regular reviewers with free eBooks and
videos in exchange for their valuable feedback. Help us be relentless in improving our
products!
Table of Contents
Preface
1
Chapter 1: Getting Started with Spring
9
Installing Java Development Kit 1.8 10
Getting started 10
How to do it... 10
How it works... 12
Installing Tomcat 9 and configuring HTTP/2 12
Getting started 12
How to do it... 13
How it works... 16
Installing STS Eclipse 3.8 IDE 17
Getting started 17
How to do it... 17
How it works... 18
Creating Eclipse projects using Maven 19
Getting started 20
How to do it... 20
How it works... 23
Creating Spring STS Eclipse projects using Gradle 23
Getting started 24
How to do it... 26
How it works... 28
Deploying Spring projects using Maven 29
Getting started 29
How to do it... 29
How it works... 31
Deploying Spring projects using Gradle 31
Getting started 31
How to do it... 32
How it works... 33
Installing the MySQL 5.7 database server 34
Getting started 34
How to do it... 35
How it works... 36
Installing the MongoDB 3.2 database server 36
Getting started 37
How to do it... 37
How it works... 38
Chapter 2: Learning Dependency Injection (DI)
40
Implementing a Spring container using XML 41
Getting started 41
How to do it... 41
How it works... 43
Implementing a Spring container using JavaConfig 44
Getting started 45
How to do it... 45
How it works... 46
Managing beans in an XML-based container 47
Getting started 48
How to do it... 48
How it works... 52
Managing beans in the JavaConfig container 53
Getting started 53
How to do it... 54
How it works... 56
Creating Singleton and Prototype beans 57
Getting started 57
How to do it... 57
How it works... 59
Defining eager and lazy spring beans 60
Getting started 60
How to do it... 60
How it works... 62
Creating an inner bean 63
Getting started 64
How to do it... 64
How it works... 66
Injecting Collections and Properties 66
Getting started 66
How to do it... 67
How it works... 71
Creating a Spring MVC using an XML-based approach 71
Getting started 71
[ ii ]