O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Java 9 Dependency Injection

Book Description

Create clean code with Dependency Injection principles

About This Book
  • Use DI to make your code loosely coupled to manage and test your applications easily on Spring 5 and Google Guice
  • Learn the best practices and methodologies to implement DI
  • Write more maintainable Java code by decoupling your objects from their implementations
Who This Book Is For

This book is for Java developers who would like to implement DI in their application. Prior knowledge of the Spring and Guice frameworks and Java programming is assumed.

What You Will Learn
  • Understand the benefits of DI and fo from a tightly coupled design to a cleaner design organized around dependencies
  • See Java 9's new features and modular framework
  • Set up Guice and Spring in an application so that it can be used for DI
  • Write integration tests for DI applications
  • Use scopes to handle complex application scenarios
  • Integrate any third-party library in your DI-enabled application
  • Implement Aspect-Oriented Programming to handle common cross-cutting concerns such as logging, authentication, and transactions
  • Understand IoC patterns and anti-patterns in DI
In Detail

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.

Style and approach

This book will take an easy-to-understand, step-by-step approach providing hands-on examples at every stage.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Java 9 Dependency Injection
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the authors
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Why Dependency Injection?
    1. Design patterns
    2. Dependency Inversion Principle
    3. Inversion of Control
      1. Implementing DIP through IoC
        1. Inverting the interface
        2. Inverting object creation 
        3. Different ways to invert object creation
        4. Inversion of object creation through the factory pattern
        5. Inversion of object creation through service locator
    4. Dependency injection
      1. Dependency injection types
        1. Constructor injection
        2. Setter injection
        3. Interface injection
    5. IoC containers
    6. Summary
  7. Dependency Injection in Java 9
    1. Java 9 introduction
      1. Key features
        1. Java Platform Module System
        2. JShell (REPL) – The Java Shell
        3. JLink – Module Linker
        4. Multi-release JAR files
        5. Stream API enhancements
        6. Stack-walking API
        7. Immutable collections with convenient factory methods
        8. HTTP/2.0 support
    2. Modular Framework in Java 9
      1. What is modularity?
      2. Java Platform Module System
        1. The need for a Java modular system
        2. Modular JDK 
        3. What is a module?
          1. Structure of a module
          2. Module Descriptor (module-info.java)
        4. Module types
    3. Dependency Injection using the Java 9 Modular Framework
      1. Modules with Service Loader
        1. Service (API) module
        2. Service provider (Implementation) module
        3. Service client application
      2. Writing modular code using a command-line interface
        1. Defining dependency between modules
        2. Compiling and running modules
    4. Summary
  8. Dependency Injection with Spring
    1. A brief introduction to Spring framework
      1. Spring framework architecture
        1. Core container layer
        2. Data access/integration layer
        3. Spring web layer
        4. Spring test
        5. Miscellaneous
    2. Bean management in Spring container
      1. Spring IoC container
      2. Configuration
      3. Containers in action
    3. Dependency Injection (DI) in Spring
      1. Constructor-based DI
      2. Setter-based DI
      3. Spring DI with the factory method
        1. Static factory method 
        2. Instance (non-static) factory method
    4. Auto-wiring in Spring
      1. Auto-wiring by name
      2. Auto-wiring by type
      3. Auto-wiring by constructor
    5. Annotation-based DI
      1. DI through XML configuration
      2. Defining annotation
      3. Activating annotation-based configuration
      4. Defining a Java class as <bean> with annotation
      5. Annotation with the factory method
    6. DI with Java configuration
    7. Summary
  9. Dependency Injection with Google Guice
    1. A brief introduction to the Google Guice framework
      1. Guice setup
      2. Dependency injection and JSR-330
        1. Example of simple DI
    2. Basic injection in Guice
      1. Guice API and Phases
        1. Start up phase
          1. Module interface
          2. The AbstractModule class
          3. Binder
          4. Injector
          5. Guice
          6. Provider
        2. Runtime phase
      2. Guice annotations
        1. Inject
        2. ProvidedBy
        3. ImplementedBy
        4. @Named
    3. Binding in Guice
      1. Linked bindings
      2. Instance bindings
      3. Untargeted bindings
      4. Constructor bindings
      5. Built-in bindings
      6. Just-in-time Bindings
      7. Binding annotations
    4. Guice injection
      1. Constructor Injection
      2. Method injection
      3. Field injection
      4. Optional injection
      5. Static injection
    5. Summary
  10. Scopes
    1. Introduction to bean scopes in Spring
      1. Bean definition 
      2. Spring scopes
        1. Singleton scope
        2. Prototype scope
        3. Request scope
        4. Session scope
        5. Application scope
        6. Global session scope
        7. websocket scope
    2. How to define a bean scope
      1. XML metadata configuration
        1. Using the singleton scope
        2. Using the prototype scope
      2. Java configuration using annotations
        1. Singleton scope with annotation
        2. Prototype scope with annotation
    3. Dependency injection and the bean scope
    4. How to choose a bean scope
    5. Scopes in Google Guice
      1. Default scope
      2. Singleton scope
        1. Eager singletons
    6. Summary
  11. Aspect-Oriented Programming and Interceptors
    1. AOP introduction
    2. Spring AOP
      1. XML(schema)-based Spring AOP
        1. Declaring aspect
        2. Declaring a point-cut
          1. Point-cut designator
          2. Patterns
        3. Declaring Advice (interceptor)
          1. Implementing before advice
          2. Implementing after advice
          3. Implementing around advice
          4. Implementing after returning advice
          5. Implementing AfterThrowing advice
      2. @AspectJ annotation-based Spring AOP
        1. Declaring aspect
        2. Declaring point-cut
        3. Declaring Advice
        4. Declaring an advisor
    3. Choosing AOP frameworks and style of configuration
      1. Spring AOP versus AspectJ language
      2. XML versus @AspectJ-style annotation for Spring AOP
    4. Summary
  12. IoC Patterns and Best Practices
    1. Various patterns to achieve IoC
      1. The factory method pattern
        1. Defining the product (abstract type) and its concrete implementation
        2. Defining the factory method (creator interface) and its concrete implementation
      2. The service locator pattern
      3. The template method pattern
      4. The strategy pattern
    2. Configuration styles
      1. File-based (XML) versus code-based configuration
    3. Injection using the setter method versus the constructor
      1. Constructor-based DI
      2. Setter-based DI
    4. Circular dependency
      1. Problems of circular dependency
      2. Causes and solutions
        1. The single responsibility principle
        2. Deferring the setting of a dependency from constructor to setter
        3. Relocation of classes and packages
        4. Circular dependency in the Spring framework
          1. Using setter/field injection over constructor injection
          2. Using the @Lazy annotation
    5. Best practices and anti-patterns
      1. What to inject – the container itself or just dependencies?
      2. Excessive injection
      3. Achieving IoC in the absence of a container
    6. Summary
  13. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think