Book Image

Java 9 Dependency Injection

By : Nilang Patel, Krunal Patel
Book Image

Java 9 Dependency Injection

By: Nilang Patel, Krunal Patel

Overview of this book

Dependency Injection (DI) is a design pattern that allows us to remove the hard-coded dependencies and make our application loosely coupled, extendable, and maintainable. We can implement DI to move the dependency resolution from compile-time to runtime. This book will be your one stop guide to write loosely coupled code using the latest features of Java 9 with frameworks such as Spring 5 and Google Guice. We begin by explaining what DI is and teaching you about IoC containers. Then you’ll learn about object compositions and their role in DI. You’ll find out how to build a modular application and learn how to use DI to focus your efforts on the business logic unique to your application and let the framework handle the infrastructure work to put it all together. Moving on, you’ll gain knowledge of Java 9’s new features and modular framework and how DI works in Java 9. Next, we’ll explore Spring and Guice, the popular frameworks for DI. You’ll see how to define injection keys and configure them at the framework-specific level. After that, you’ll find out about the different types of scopes available in both popular frameworks. You’ll see how to manage dependency of cross-cutting concerns while writing applications through aspect-oriented programming. Towards the end, you’ll learn to integrate any third-party library in your DI-enabled application and explore common pitfalls and recommendations to build a solid application with the help of best practices, patterns, and anti-patterns in DI.
Table of Contents (14 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Preface

Dependency Injection is a design pattern that allows us to remove the hardcoded dependencies and make our application loosely coupled, extendable, and maintainable. We can implement dependency injection to move the dependency resolution from compile-time to runtime. This book will be your one-stop guide to writing loosely coupled code using the latest features of Java 9 with frameworks such as Spring 5 and Google Guice.

Who this book is for

This book is for Java developers who would like to understand how to implement Dependency Injection in their applications. Prior knowledge about the Spring and Guice frameworks and Java programming is assumed.

What this book covers

Chapter 1, Why Dependency Injection?, gives you a detailed insight into various concepts, such as Dependency Inversion of Principle (DIP), Inversion of Control (IoC), and Dependency Injection (DI). It also talks about practical use cases where DI is commonly used.

Chapter 2, Dependency Injection in Java 9, gets you acquainted with Java 9 features and its modular framework, and explains how to implement DI using the service loader concept.

Chapter 3, Dependency Injection with Spring, teaches you how to manage dependency injection in the Spring framework. It also describes a different way to implement DI using Spring.

Chapter 4, Dependency Injection with Google Guice, talks about Guice and its dependency mechanism, and it teaches us dependency binding and the various injection methods of the Guice framework.

Chapter 5, Scopes, teaches you about the different scopes defined in the Spring and Guice frameworks.

Chapter 6, Aspect-Oriented Programming and Interceptors, shows the purpose of Aspect-Oriented Programming (AOP), how it solves different design problems by isolating repeated code from applications and plug them dynamically using Spring framework.

Chapter 7, IoC Patterns and Best Practices, gives an overview of various design patterns that can use to achieve IoC. Apart from this, you will be acquainted with best practices and anti-patterns to follow while injecting DI.

To get the most out of this book

  1. It would be good if you know Java, Spring, and the Guice framework. This will help you understand dependency injection
  2. We assume you have an installation of Java 9, and Maven on your system, before beginning

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/Java-9-Dependency-Injection. We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/Java9DependencyInjection_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Mount the downloaded WebStorm-10*.dmg disk image file as another disk on your system."

A block of code is set as follows:

module javaIntroduction {
}

Any command-line input or output is written as follows:

$ mkdir css
$ cd css

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Select System info from the Administration panel."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.