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 Modularity

Book Description

The upcoming Java 9 module system will affect existing applications and offer new ways of creating modular and maintainable applications. With this hands-on book, Java developers will learn not only about the joys of modularity, but also about the patterns needed to create truly modular and reliable applications. Authors Sander Mak and Paul Bakker teach you the concepts behind the Java 9 module system, along with the new tools it offers. You’ll also learn how to modularize existing code and how to build new Java applications in a modular way.

  • Understand Java 9 module system concepts
  • Master the patterns and practices for building truly modular applications
  • Migrate existing applications and libraries to Java 9 modules
  • Use JDK 9 tools for modular development and migration

Table of Contents

  1. Foreword
  2. Preface
    1. Who Should Read This Book
    2. Why We Wrote This Book
    3. Navigating This Book
    4. Using Code Examples
    5. Conventions Used in This Book
    6. O’Reilly Safari
    7. How to Contact Us
    8. Acknowledgments
  3. I. Introduction to the Java Module System
  4. 1. Modularity Matters
    1. What Is Modularity?
    2. Before Java 9
      1. JARs as Modules?
      2. Classpath Hell
    3. Java 9 Modules
  5. 2. Modules and the Modular JDK
    1. The Modular JDK
    2. Module Descriptors
    3. Readability
    4. Accessibility
    5. Implied Readability
    6. Qualified Exports
    7. Module Resolution and the Module Path
    8. Using the Modular JDK Without Modules
  6. 3. Working with Modules
    1. Your First Module
      1. Anatomy of a Module
      2. Naming Modules
      3. Compilation
      4. Packaging
      5. Running Modules
      6. Module Path
      7. Linking Modules
    2. No Module Is an Island
      1. Introducing the EasyText Example
      2. A Tale of Two Modules
    3. Working with Platform Modules
      1. Finding the Right Platform Module
      2. Creating a GUI Module
    4. The Limits of Encapsulation
      1. Interfaces and Instantiation
  7. 4. Services
    1. Factory Pattern
    2. Services for Implementation Hiding
      1. Providing Services
      2. Consuming Services
      3. Service Life Cycle
      4. Service Provider Methods
    3. Factory Pattern Revisited
    4. Default Service Implementations
    5. Service Implementation Selection
      1. Service Type Inspection and Lazy Instantiation
    6. Module Resolution with Service Binding
    7. Services and Linking
  8. 5. Modularity Patterns
    1. Determining Module Boundaries
    2. Lean Modules
    3. API Modules
      1. What Should Be in an API Module?
      2. Implied Readability
      3. API Module with a Default Implementation
    4. Aggregator Modules
      1. Building a Facade over Modules
      2. Safely Splitting a Module
    5. Avoiding Cyclic Dependencies
      1. Split Packages
      2. Breaking Cycles
    6. Optional Dependencies
      1. Compile-Time Dependencies
      2. Implementing Optional Dependencies with Services
    7. Versioned Modules
      1. Module Resolution and Versioning
    8. Resource Encapsulation
      1. Loading Resources from a Module
      2. Loading Resources Across Modules
      3. Working with ResourceBundles
  9. 6. Advanced Modularity Patterns
    1. Strong Encapsulation Revisited
      1. Deep Reflection
      2. Open Modules and Packages
      3. Dependency Injection
    2. Reflection on Modules
      1. Introspection
      2. Modifying Modules
      3. Annotations
    3. Container Application Patterns
      1. Layers and Configurations
      2. Classloading in Layers
      3. Plug-in Architectures
      4. Container Architectures
      5. Resolving Platform Modules in Containers
  10. II. Migration
  11. 7. Migration Without Modules
    1. The Classpath Is Dead, Long Live the Classpath
    2. Libraries, Strong Encapsulation, and the JDK 9 Classpath
    3. Compilation and Encapsulated APIs
    4. Removed Types
    5. Using JAXB and Other Java EE APIs
    6. The jdk.unsupported Module
    7. Other Changes
  12. 8. Migration to Modules
    1. Migration Strategies
    2. A Simple Example
    3. Mixing Classpath and Module Path
    4. Automatic Modules
    5. Open Packages
    6. Open Modules
    7. VM Arguments to Break Encapsulation
    8. Automatic Modules and the Classpath
    9. Using jdeps
    10. Loading Code Dynamically
    11. Split Packages
  13. 9. Migration Case Study: Spring and Hibernate
    1. Getting Familiar with the Application
    2. Running on the Classpath with Java 9
    3. Setting Up for Modules
    4. Using Automatic Modules
    5. Java Platform Dependencies and Automatic Modules
    6. Opening Packages for Reflection
    7. Fixing Illegal Access
    8. Refactor to Multiple Modules
  14. 10. Library Migration
    1. Before Modularization
    2. Choosing a Library Module Name
    3. Creating a Module Descriptor
    4. Updating a Library with a Module Descriptor
    5. Targeting Older Java Versions
    6. Library Module Dependencies
      1. Internal Dependencies
      2. External Dependencies
    7. Targeting Multiple Java Versions
      1. Multi-Release JARs
      2. Modular Multi-Release JARs
  15. III. Modular Development Tooling
  16. 11. Build Tools and IDEs
    1. Apache Maven
      1. Multimodule Projects
      2. EasyText with Apache Maven
      3. Running a Modular Application with Apache Maven
    2. Gradle
    3. IDEs
  17. 12. Testing Modules
    1. Blackbox Tests
    2. Blackbox Tests with JUnit
    3. Whitebox Tests
    4. Test Tooling
  18. 13. Scaling Down with Custom Runtime Images
    1. Static Versus Dynamic Linking
    2. Using jlink
    3. Finding the Right Service Provider Modules
    4. Module Resolution During Linking
    5. jlink for Classpath-Based Applications
    6. Reducing Size
    7. Improving Performance
    8. Cross-Targeting Runtime Images
  19. 14. A Modular Future
    1. OSGi
    2. Java EE
    3. Microservices
    4. Next Steps
  20. Index