Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly.
With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features.
OReilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from OReilly and other publishers, sign up for free at http://my.safaribooksonline.com.
How to Contact Us
Please address comments and questions concerning this book to the publisher:
OReilly Media, Inc. |
1005 Gravenstein Highway North |
Sebastopol, CA 95472 |
800-998-9938 (in the United States or Canada) |
707-829-0515 (international or local) |
707-829-0104 (fax) |
We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:
http://www.oreilly.com/catalog/9781449306403 |
To comment or ask technical questions about this book, send email to:
For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
Acknowledgments
I sincerely wish to thank my editor, Mike Loukides, for keeping faith in me and directing me when lost. Also to all of those in the OReilly team, especially Meghan Blanchette, Holly Bauer, Sarah Schneider. and Dan Fauxsmith, for helping shape this book.
Sincere thanks to my loving wife, Jeannette, for being very patient and supportive throughout the writing of this book. Also to my wonderful four-year-old son, Joshua, who surprisingly sacrificed his free time, allowing me to write when I explained to him what I was doing!
Chapter 1. Spring Basics
Introduction
The Spring Framework has found a very strong user base over the years. Software houses and enterprises found the framework to be the best fit for their plumbing needs. Surprisingly, the core principle that Spring has been built forthe Dependency Injection (DI)is very simple to understand. This chapter discusses the fundamentals of the framework from a high ground. It helps the reader understand the dependency injection principles. The chapter opens up with a simple problem of object coupling and dependencies. It then explains how to solve the them using Spring Framework.
Object Coupling Problem
Let us consider a simple program whose objective is to read data from various data sources. It can read data from a file system or database or even from an FTP server. For simplicity, we will start writing a program that reads the data from a file system for now. The following example code is written without employing any best practices or dependency injection patternsits just a simple and plain program that works.
shows a Client
program that uses FileReader
to fetch the data.
Example 1-1.
public class DataReaderClient { private FileReader fileReader = null; private String fileName = "res/myfile.txt"; public DataReaderClient() { fileReader = new FileReader(fileName); } private String fetchData() { return fileReader.read(); } public static void main(String[] args) { DataReaderClient dataReader = new DataReaderClient(); System.out.println("Got data: "+dataReader.fetchData()); }}
As the name suggests, the DataReaderClient
is the client that fetches the data from a data source. When the program is executed, the DataReaderClient
gets instantiated along with a referenced FileReader
object. It then uses the FileReader
object to fetch the result.
is the implementation of FileReader
class.
Example 1-2.
public class FileReader { private StringBuilder builder = null; private Scanner scanner = null; public FileReader(String fileName) { scanner = new Scanner(new File(fileName)); builder= new StringBuilder(); } public String read() { while (scanner.hasNext()) { builder.append(scanner.next()); } return builder.toString(); }}