Spring Cookbook
()
About this ebook
- Build full-featured web applications with Spring MVC
- Use Spring 4 Java configuration style to write less code
- Learn how to use dependency injection and aspect-oriented programming to write compartmentalized and testable code
This book is for you if you have some experience with Java and web development (not necessarily in Java) and want to become proficient quickly with Spring.
Related to Spring Cookbook
Related ebooks
Angular 2 Cookbook Rating: 0 out of 5 stars0 ratingsMeteor: Full-Stack Web Application Development Rating: 0 out of 5 stars0 ratingsNode Cookbook: Second Edition Rating: 3 out of 5 stars3/5Entity Framework Core Cookbook - Second Edition Rating: 0 out of 5 stars0 ratingsApache Maven Cookbook Rating: 0 out of 5 stars0 ratingsJavaFX 1.2 Application Development Cookbook Rating: 0 out of 5 stars0 ratingsJava Hibernate Cookbook Rating: 0 out of 5 stars0 ratingsAlfresco 3 Cookbook Rating: 0 out of 5 stars0 ratingsApache Camel Developer's Cookbook Rating: 0 out of 5 stars0 ratingsSpring MVC: Designing Real-World Web Applications Rating: 0 out of 5 stars0 ratingsSpring MVC Cookbook Rating: 0 out of 5 stars0 ratingsLearning PHP Data Objects Rating: 5 out of 5 stars5/5Spring Essentials Rating: 0 out of 5 stars0 ratingsPractical Advanced TypeScript Rating: 0 out of 5 stars0 ratingsMastering Web Application Development with Express Rating: 0 out of 5 stars0 ratingsSpring Data Rating: 0 out of 5 stars0 ratingsMastering Spring Application Development Rating: 1 out of 5 stars1/5JavaScript at Scale Rating: 0 out of 5 stars0 ratingsContinuous Integration in .NET Rating: 0 out of 5 stars0 ratingsTest-Driven JavaScript Development Rating: 0 out of 5 stars0 ratingsRestlet in Action: Developing RESTful web APIs in Java Rating: 0 out of 5 stars0 ratingsSpring Microservices Rating: 0 out of 5 stars0 ratingsSpring Security 3.x Cookbook Rating: 0 out of 5 stars0 ratingsLearning Behavior-driven Development with JavaScript Rating: 5 out of 5 stars5/5The Java Workshop: Learn object-oriented programming and kickstart your career in software development Rating: 0 out of 5 stars0 ratingsJava Testing with Spock Rating: 0 out of 5 stars0 ratingsSpring Boot in Practice Rating: 0 out of 5 stars0 ratingsWeb Development with MongoDB and Node.js Rating: 0 out of 5 stars0 ratingsSpring Boot 2: How To Get Started and Build a Microservice - Third Edition Rating: 5 out of 5 stars5/5
Databases For You
Python Projects for Everyone Rating: 0 out of 5 stars0 ratingsArtificial Intelligence for Fashion: How AI is Revolutionizing the Fashion Industry Rating: 0 out of 5 stars0 ratingsFileMaker Pro Design and Scripting For Dummies Rating: 0 out of 5 stars0 ratingsExcel 2021 Rating: 4 out of 5 stars4/5SQL Clearly Explained Rating: 5 out of 5 stars5/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5Practical Data Analysis Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5The AI Bible, Making Money with Artificial Intelligence: Real Case Studies and How-To's for Implementation Rating: 4 out of 5 stars4/5Access 2010 All-in-One For Dummies Rating: 4 out of 5 stars4/5Schaum’s Outline of Fundamentals of SQL Programming Rating: 3 out of 5 stars3/5COMPUTER SCIENCE FOR ROOKIES Rating: 0 out of 5 stars0 ratingsQuery Store for SQL Server 2019: Identify and Fix Poorly Performing Queries Rating: 0 out of 5 stars0 ratingsServerless Architectures on AWS, Second Edition Rating: 5 out of 5 stars5/5Access 2019 For Dummies Rating: 0 out of 5 stars0 ratingsBeginning Microsoft Power BI: A Practical Guide to Self-Service Data Analytics Rating: 0 out of 5 stars0 ratingsBeginning Microsoft SQL Server 2012 Programming Rating: 1 out of 5 stars1/5Codeless Data Structures and Algorithms: Learn DSA Without Writing a Single Line of Code Rating: 0 out of 5 stars0 ratingsLearn SQL Server Administration in a Month of Lunches Rating: 0 out of 5 stars0 ratingsSQL: Practical Guide for Developers Rating: 2 out of 5 stars2/5SQL Server: Tips and Tricks - 2 Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Building a Scalable Data Warehouse with Data Vault 2.0 Rating: 4 out of 5 stars4/5Relational Database Design and Implementation Rating: 5 out of 5 stars5/5Data Stewardship: An Actionable Guide to Effective Data Management and Data Governance Rating: 4 out of 5 stars4/5Access for Beginners: Access Essentials, #1 Rating: 0 out of 5 stars0 ratingsPractical SQL Rating: 4 out of 5 stars4/5100+ SQL Queries T-SQL for Microsoft SQL Server Rating: 4 out of 5 stars4/5Learning PostgreSQL Rating: 1 out of 5 stars1/5
Reviews for Spring Cookbook
0 ratings0 reviews
Book preview
Spring Cookbook - Jérôme Jaglale
Table of Contents
Spring Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Creating a Spring Application
Introduction
Installing Java, Maven, Tomcat, and Eclipse on Mac OS
How to do it…
Installing Java
Installing Maven
Installing Tomcat
Installing Eclipse
There's more…
Installing Java, Maven, Tomcat, and Eclipse on Ubuntu
How to do it…
Installing Java
Installing Maven
Installing Tomcat
Installing Eclipse
There's more…
Installing Java, Maven, Tomcat, and Eclipse on Windows
How to do it…
Installing Java
Installing Maven
Installing Tomcat
Installing Eclipse
Creating a Spring web application
How to do it…
Creating a new Maven project in Eclipse
Adding Spring to the project using Maven
Creating the configuration classes for Spring
Creating a Hello World
web page
How it works…
Creating a new Maven project in Eclipse
Adding Spring to the project using Maven
Creating the configuration classes for Spring
Creating a Hello World
web page
Running a Spring web application
How to do it…
How it works…
There's more…
Using Spring in a standard Java application
How to do it…
Creating a new Maven project in Eclipse
Adding Spring to the project using Maven
Creating a configuration class for Spring
Creating the User class
Defining a User singleton in the Spring configuration class
Using the User singleton in the main() method
How it works...
2. Defining Beans and Using Dependency Injection
Introduction
Defining a bean explicitly with @Bean
How to do it…
How it works…
There's more…
Defining a bean implicitly with @Component
Getting ready
How to do it…
How it works…
There's more…
Using a bean via dependency injection with @Autowired
Getting ready
How to do it…
How it works…
There's more…
Using a bean directly
Getting ready
How to do it…
How it works…
There's more…
Listing all beans
Getting ready
How to do it…
How it works…
There's more…
Using multiple configuration classes
Getting ready
How to do it…
There's more…
3. Using Controllers and Views
Introduction
Associating a route to a controller method
How to do it…
How it works…
Using a JSP view
How to do it…
How it works…
There's more…
Passing attributes from a controller to a JSP view
How to do it…
How it works…
Using dynamic route parameters in a controller method
How to do it…
How it works…
Using a common prefix for the routes of a controller
How to do it…
How it works…
Using a page template with Tiles
How to do it…
How it works…
There's more…
Organizing the JSP with subfolders
Using multiple page templates
Defining page titles only once using a text attribute
Executing some code before and after controllers using interceptors
How to do it…
How it works…
There's more…
Building multilingual pages
How to do it…
How it works…
There's more…
Getting the current language
Using the language in the URL
4. Querying a Database
Introduction
Connecting to a database
Getting ready
How to do it…
How it works…
Creating a DAO class
How to do it…
How it works…
Calling a DAO method from a controller class
Getting ready
How to do it…
How it works…
Saving an object
Getting ready
How to do it…
How it works…
Retrieving an object
How to do it…
How it works…
There's more…
Retrieving a list of objects
How to do it…
How it works…
Retrieving a list of objects with their dependencies
Getting ready
How to do it…
How it works…
Updating an object
How to do it…
There's more…
Deleting an object
How to do it…
Finding the number of results for an SQL query
How to do it…
Saving a list of objects at once
How to do it…
How it works…
Reverting incomplete database modifications using transactions
How to do it…
How it works…
Using Hibernate for powerful object persistence and querying
Getting ready
How to do it…
5. Using Forms
Introduction
Displaying and processing a form
How to do it…
How it works…
There's more…
See also
Getting a submitted form value using a controller method argument
How to do it…
How it works…
There's more…
See also
Setting a form's default values using a model object
How to do it…
How it works…
Saving form data in an object automatically
How to do it…
How it works…
There's more…
Using text, textarea, password, and hidden fields
How to do it…
How it works…
Using a select field
How to do it…
How it works…
There's more…
Using a List
Using a List
Using a checkbox
How to do it…
How it works…
Using a list of checkboxes
How to do it…
How it works…
There's more…
Using a list of radio buttons
How to do it…
How it works…
There's more…
Validating a form using annotations
How to do it…
How it works…
There's more…
Uploading a file
How to do it…
How it works…
There's more…
6. Managing Security
Introduction
Enabling Spring Security
How to do it…
How it works…
Authenticating users using the default login page
How to do it…
How it works…
Authenticating users using a custom login page
How to do it…
How it works…
Authenticating users using a database
How to do it…
How it works…
Adding a logout link
How to do it…
How it works…
Using public folders
How to do it…
There's more…
Authorizing only users with a specific role to view some pages
How to do it…
How it works…
Displaying page elements only to authenticated users in views
How to do it…
How it works…
There's more…
Using HTTPS with Tomcat
How to do it…
How it works…
There's more…
7. Unit Testing
Introduction
Unit testing with JUnit 4
Getting ready
How to do it…
How it works…
There's more…
Unit testing with TestNG 6
Getting ready
How to do it…
How it works…
There's more…
Simulating dependencies with mocks using Mockito
Getting ready
How to do it…
How it works…
There's more…
Unit testing with JUnit 4 using Spring's application context
How to do it…
How it works…
There's more…
Unit testing with TestNG 6 using Spring's application context
How to do it…
How it works…
There's more…
Unit testing with transactions
How to do it…
How it works…
Unit testing controller methods
Getting ready
How to do it…
How it works…
There's more…
8. Running Batch Jobs
Introduction
Installing and configuring Spring Batch
How to do it…
How it works…
Creating a job
How to do it…
How it works…
There's more…
Executing a job from the command line
Getting Ready
How to do it…
There's more…
Executing a job from a controller method
Getting ready
How to do it…
How it works…
Using job parameters
Getting ready
How to do it…
How it works…
Executing a system command
Getting ready
How to do it…
How it works…
There's more…
Scheduling a job
Getting ready
How to do it…
How it works…
There's more…
Creating a read/process/write step
Getting ready
How to do it…
How it works…
There's more…
Reading an XML file
Getting ready
How to do it…
How it works…
Generating a CSV file
Getting ready
How to do it…
How it works…
Reading from a database
Getting ready
How to do it…
How it works…
Unit testing batch jobs
How to do it…
How it works…
9. Handling Mobiles and Tablets
Introduction
Installing Spring Mobile
How to do it…
How it works…
There's more…
Detecting mobiles and tablets
How to do it…
How it works…
Switching to the normal view on mobiles
How to do it…
How it works…
There's more…
Using different JSP views for mobiles automatically
How to do it…
How it works…
There's more…
Using a .mobi domain name on mobiles
Getting ready
How to do it…
How it works…
Using an m. subdomain on mobiles
Getting ready
How to do it…
How it works…
Using a different domain name on mobiles
Getting ready
How to do it…
How it works…
Using a subfolder path on mobiles
Getting ready
How to do it…
How it works…
10. Connecting to Facebook and Twitter
Introduction
Creating a Facebook app
Getting ready
How to do it…
Creating a test Facebook app and test users
Getting ready
How to do it…
How it works…
Connecting to Facebook
Getting ready
How to do it…
How it works…
Retrieving a Facebook user's profile
Getting ready
How to do it…
How it works…
There's more…
Retrieving the list of friends of a Facebook user
Getting ready
How to do it…
How it works…
Posting a Facebook status update
Getting ready
How to do it…
Posting a link to Facebook
Getting ready
How to do it…
Posting a custom object to Facebook
Getting ready
How to do it…
Creating a Twitter application
Getting ready
How to do it…
Connecting to Twitter
Getting ready
How to do it…
How it works…
Retrieving a user's Twitter profile
Getting ready
How to do it…
Retrieving the tweets of a Twitter user
Getting ready
How to do it…
Posting a tweet to Twitter
Getting ready
How to do it…
Sending a private message to another Twitter user
Getting Ready
How to do it…
11. Using the Java RMI, HTTP Invoker, Hessian, and REST
Introduction
Creating a Java RMI service
Getting ready
How to do it…
How it works…
Querying an existing Java RMI service
Getting ready
How to do it…
How it works…
Creating an HTTP Invoker service
Getting ready
How to do it…
How it works…
Querying an existing HTTP Invoker service
Getting ready
How to do it…
How it works…
Creating a Hessian service
Getting ready
How to do it…
How it works…
Querying an existing Hessian service
Getting ready
How to do it…
How it works…
Creating a REST service
Getting ready
How to do it…
How it works…
There's more…
Querying an existing REST service
Getting ready
How to do it…
How it works…
There's more…
12. Using Aspect-oriented Programming
Introduction
Creating a Spring AOP aspect class
How to do it…
How it works…
Measuring the execution time of methods using an around advice
Getting ready
How to do it…
How it works…
Logging method arguments using a before advice
Getting ready
How to do it…
How it works…
Logging methods' return values using an after-returning advice
Getting ready
How to do it…
How it works…
Logging exceptions using an after-throwing advice
Getting ready
How to do it…
How it works…
Using an after advice to clean up resources
Getting ready
How to do it…
How it works…
Making a class implement an interface at runtime using an introduction
Getting ready
How to do it…
How it works…
Setting the execution order of the aspects
Getting ready
How to do it…
How it works…
There's more…
Index
Spring Cookbook
Spring Cookbook
Copyright © 2015 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: May 2015
Production reference: 1190515
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78398-580-7
www.packtpub.com
Credits
Author
Jérôme Jaglale
Reviewers
Alexandre Arcanjo de Queiroz
Manuel Jordan
Stanojko Markovik
Bert Van den Brande
Commissioning Editor
Julian Ursell
Acquisition Editor
James Jones
Content Development Editor
Ajinkya Paranjape
Technical Editor
Siddhesh Ghadi
Copy Editor
Neha Vyas
Project