Spring 5 Design Patterns

Book description

Learn various design patterns and best practices in Spring 5 and use them to solve common design problems.

About This Book

  • Explore best practices for designing an application
  • Manage your code easily with Spring's Dependency Injection pattern
  • Understand the benefits that the right design patterns can offer your toolkit

Who This Book Is For

This book is for developers who would like to use design patterns to address common problems while designing an app using the Spring Framework and Reactive Programming approach. A basic knowledge of the Spring Framework and Java is assumed.

What You Will Learn

  • Develop applications using dependency injection patterns
  • Learn best practices to design enterprise applications
  • Explore Aspect-Oriented Programming relating to transactions, security, and caching.
  • Build web applications using traditional Spring MVC patterns
  • Learn to configure Spring using XML, annotations, and Java.
  • Implement caching to improve application performance.
  • Understand concurrency and handle multiple connections inside a web server.
  • Utilizing Reactive Programming Pattern to build Reactive web applications.

In Detail

Design patterns help speed up the development process by offering well tested and proven solutions to common problems. These patterns coupled with the Spring framework offer tremendous improvements in the development process.

The book begins with an overview of Spring Framework 5.0 and design patterns. You will understand the Dependency Injection pattern, which is the main principle behind the decoupling process that Spring performs, thus making it easier to manage your code. You will learn how GoF patterns can be used in Application Design. You will then learn to use Proxy patterns in Aspect Oriented Programming and remoting. Moving on, you will understand the JDBC template patterns and their use in abstracting database access. Then, you will be introduced to MVC patterns to build Reactive web applications. Finally, you will move on to more advanced topics such as Reactive streams and Concurrency.

At the end of this book, you will be well equipped to develop efficient enterprise applications using Spring 5 with common design patterns

Style and approach

The book takes a pragmatic approach, showing various design patterns and best-practice considerations, including the Reactive programming approach with the Spring 5 Framework and ways to solve common development and design problems for enterprise applications.

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Getting Started with Spring Framework 5.0 and Design Patterns
    1. Introducing Spring Framework
    2. Simplifying application development using Spring and its pattern
      1. Using the power of the POJO pattern
      2. Injecting dependencies between POJOs
        1. How DI works and makes things easy for development and testing
        2. Using factory helper pattern for dependent components
      3. Using DI pattern for dependent components
      4. Applying aspects for cross cutting concerns
        1. How Spring AOP works
      5. Applying the template pattern to eliminate boilerplate code
        1. The Template Design pattern in Spring
    3. Using a Spring container to manage beans with the Factory pattern
      1. Bean factory
      2. Application contexts
      3. Creating a container with an application context
    4. Life of a bean in the container
    5. Spring modules
      1. Core Spring container
      2. Spring's AOP module
      3. Spring DAO - data access and integration
      4. Spring's ORM
      5. Spring web MVC
    6. New features in Spring Framework 5.0
    7. Summary
  3. Overview of GOF Design Patterns - Core Design Patterns
    1. Introducing the power of design patterns
    2. Common GoF Design Pattern overview
    3. Creational design patterns
      1. Factory design pattern
        1. Implementing the Factory design pattern in Spring Framework
        2. Sample implementation of the Factory design pattern
      2. Abstract factory design pattern
        1. Common problems where you should apply the Abstract factory design pattern
        2. Implementing the Abstract factory design pattern in the Spring Framework
        3. Sample implementation of the Abstract Factory design pattern
      3. Singleton design pattern
        1. Common problems where you should apply Singleton pattern
        2. Singleton design pattern implementation in the Spring Framework
        3. Sample implementation of the Singleton design pattern
      4. Prototype design pattern
        1. Benefits of the Prototype design pattern
        2. UML class structure
        3. Sample implementation of the Prototype design pattern
      5. Builder design pattern
        1. Benefits of the Builder pattern:
        2. UML class structure
        3. Implementing the Builder pattern in the Spring Framework
        4. Common problems where you should apply Builder pattern
        5. Sample implementation of the Builder design pattern
    4. Summary
  4. Consideration of Structural and Behavioral Patterns
    1. Examining the core design patterns
      1. Structural design patterns
        1. The adapter design pattern
          1. Benefits of the adapter pattern
          2. Common requirements for the adapter pattern
          3. Implementation of the adapter design pattern in the Spring Framework
          4. Sample implementation of the adapter design pattern
        2. The Bridge design pattern
          1. Benefits of the Bridge pattern
          2. Common problems solved by the Bridge design pattern
          3. Implementing the Bridge design pattern in the Spring Framework
          4. Sample implementation of the Bridge design pattern
        3. Composite design pattern
          1. Common problems solved by the composite pattern
          2. UML structure of the Composite design pattern
          3. Sample implementation of the Composite design pattern
        4. Decorator design pattern
          1. Common problems solved by the Decorator pattern
          2. Implementing the Decorator pattern
          3. Decorator design pattern in the Spring Framework
        5. Facade Design Pattern
          1. Knowing when to use the Facade Pattern
          2. Implementing the Facade design pattern
          3. The UML structure for the Facade design pattern
          4. Facade Pattern in the Spring Framework
        6. Proxy design pattern
          1. Purpose of the Proxy pattern
          2. UML structure for the Proxy design pattern
          3. Implementing the Proxy design pattern
          4. Proxy pattern in the Spring Framework
      2. Behavioral design patterns
        1. Chain of Responsibility design pattern
          1. Chain of Responsibility pattern in the Spring Framework
        2. Command design pattern
          1. Command design pattern in the Spring Framework
        3. Interpreter Design pattern
          1. Interpreter design pattern in the Spring Framework
        4. Iterator Design Pattern
          1. Iterator design pattern in the Spring Framework
        5. Observer pattern in the Spring Framework
        6. Template Design Pattern
    2. JEE design patterns
    3. Summary
  5. Wiring Beans using the Dependency Injection Pattern
    1. The dependency injection pattern
      1. Solving problems using the dependencies injection pattern
        1. Without dependency injection
        2. With dependency injection pattern
    2. Types of dependency injection patterns
      1. Constructor-based dependency injection pattern
      2. Setter-based dependency injection
    3. Configuring the dependency injection pattern with Spring
    4. Dependency injection pattern with Java-based configuration
      1. Creating a Java configuration class - AppConfig.java
        1. Declaring Spring beans into configuration class
        2. Injecting Spring beans
        3. Best approach to configure the dependency injection pattern with Java
    5. Dependency injection pattern with XML-based configuration
      1. Creating an XML configuration file
        1. Declaring Spring beans in an XML file
        2. Injecting Spring beans
          1. Using constructor injection
          2. Using setter injection
    6. Dependency injection pattern with Annotation-based configuration
      1. What are Stereotype annotations?
        1. Creating auto searchable beans using Stereotype annotations
        2. Searching beans using component scanning
        3. Annotating beans for autowiring
          1. Using @Autowired with setter method
          2. Using @Autowired with the fields
      2. The Autowiring DI pattern and disambiguation
        1. Resolving disambiguation in Autowiring DI pattern
        2. Implementing the Abstract Factory Pattern in Spring (FactoryBean interface)
          1. Implementation of FactoryBean interface in Spring
          2. Sample implementation of FactoryBean interface
    7. Best practices for configuring the DI pattern
    8. Summary
  6. Understanding the Bean Life Cycle and Used Patterns
    1. The Spring bean life cycle and its phases
      1. The initialization phase
        1. Creating the application context from configuration
        2. Load bean definitions
        3. Initializing bean instances
        4. Customizing beans using a BeanPostProcessor
        5. The Initializer extension point
      2. The Use phase of beans
        1. Implementing the Decorator and Proxy patterns in Spring using Proxies
      3. The destruction phase of the beans
    2. Understanding bean scopes
      1. The singleton bean scope
      2. The prototype bean scope
      3. The session bean scope
      4. The request bean scope
      5. Other scopes in Spring
        1. Custom scopes
          1. Creating custom scopes
    3. Summary
  7. Spring Aspect Oriented Programming with Proxy and Decorator pattern
    1. Proxy pattern in Spring
      1. Proxying classes using Decorator pattern in Spring
    2. What are cross-cutting concerns?
    3. What is Aspect-Oriented Programming?
      1. Problems resolved by AOP
        1. Code tangling
        2. Code scattering
      2. How AOP Works to solve problems
    4. Core AOP terminology and concepts
      1. Advice
      2. Join Point
      3. Pointcut
      4. Aspect
      5. Weaving
    5. Defining pointcuts
      1. Writing pointcuts
    6. Creating aspects
      1. Define aspects using Annotation
    7. Implementing Advice
      1. Advice type - Before
        1. Before Advice example
      2. Advice Types: After Returning
        1. After Returning Advice example
      3. Advice Types: After Throwing
        1. After Throwing Advice example
      4. Advice Types: After
        1. After Advice example
      5. Advice Types - Around
        1. Around Advice example
    8. Define aspects using XML configuration
    9. Understanding AOP proxies
    10. Summary
  8. Accessing a Database with Spring and JDBC Template Patterns
    1. The best approach to designing your data-access
      1. The resource management problem
      2. Implementing the template design pattern
        1. Problems with the traditional JDBC
        2. Solving problems with Spring's JdbcTemplate
    2. Configuring the data source and object pool pattern
      1. Configuring a data source using a JDBC driver
      2. Configuring the data source using pool connections
    3. Implementing the Builder pattern to create an embedded data source
      1. Abstracting database access using the DAO pattern
    4. The DAO pattern with the Spring Framework
      1. Working with JdbcTemplate
      2. When to use JdbcTemplate
        1. Creating a JdbcTemplate in an application
        2. Implementing a JDBC-based repository
        3. Jdbc callback interfaces
          1. Creating a RowMapper class
          2. Implementing RowCallbackHandler
          3. Implementing ResultSetExtractor
    5. Best practices for Jdbc and configuring JdbcTemplate
    6. Summary
  9. Accessing Database with Spring ORM and Transactions Implementing Patterns
    1. ORM Framework and the patterns used
      1. Resource and transaction management
      2. Consistent exception handling and translation
    2. The data access object pattern
      1. Creating DAOs using the Factory design pattern in Spring
      2. The Data Mapper pattern
      3. The domain model pattern
      4. Proxy for the lazy loading pattern
      5. Spring's Hibernate template pattern
    3. Integrating Hibernate with Spring
      1. Configuring Hibernate's SessionFactory in a Spring container
      2. Implementing DAOs based on the plain Hibernate API
    4. Transaction management strategies in Spring
      1. Declarative transaction demarcation and implementation
      2. Deploying the transaction manager
        1. Step 1 - Implementing the transaction manager
        2. Step 2 - Declaring the transaction demarcation
      3. Programmatic transaction demarcation and implementation
    5. Best practices for Spring ORM and transaction module in an application
    6. Summary
  10. Improving Application Performance Using Caching Patterns
    1. What is cache?
      1. Where do we use caching?
    2. Understanding cache abstraction
    3. Enabling caching via the Proxy pattern
      1. Enabling the caching proxy using Annotation
      2. Enabling the Caching Proxy using the XML namespace
    4. Declarative Annotation-based caching
      1. The @Cacheable annotation
      2. The @CachePut annotation
        1. Customizing the cache key
        2. Conditional caching
      3. The @CacheEvict annotation
      4. The @Caching annotation
      5. The @CacheConfig annotation
    5. Declarative XML-based caching
    6. Configuring the cache storage
      1. Setting up the CacheManager
    7. Third-party cache implementations
      1. Ehcache-based cache
        1. XML-based configuration
    8. Creating custom caching annotations
    9. Top caching best practices to be used in a web application
    10. Summary
  11. Implementing the MVC Pattern in a Web Application using Spring
    1. Implementing the MVC pattern in a web application
    2. Model 2 architecture MVC pattern with Spring
      1. The Front Controller design pattern
        1. Processing the life of a request
        2. Configuring DispatcherServlet as the Front Controller
          1. Defined by XML configuration
          2. Defined by Java configuration
    3. Enabling the Spring MVC
      1. Implementing controllers
        1. Defining a controller with @Controller
      2. Mapping requests with @RequestMapping
        1. @RequestMapping at method level
        2. @RequestMapping at the class level
        3. Defining @RequestMapping handler methods
    4. Passing model data to the view
      1. Accepting request parameters
        1. Taking query parameters
        2. Taking request parameters via path variables
      2. Processing forms of a web page
      3. Implementing a form handling controller
    5. Data binding with Command Design pattern
      1. Using @ModelAttributes for customizing data binding
    6. Validating forms input parameters
    7. Implementing View in the MVC pattern
      1. Defining ViewResolver in the Spring MVC
        1. Implement the View
        2. Register ViewResolver with Spring MVC
      2. The View Helper pattern
      3. Composite View pattern using Apache tile view resolver
        1. Configuring a Tiles ViewResolver
    8. Best practices for web application design
    9. Summary
  12. Implementing Reactive Design Patterns
    1. Understanding application requirement over the years
    2. Understanding the reactive pattern
      1. The reactive pattern traits
        1. Responsiveness
        2. Resilience
        3. Scalable
        4. Message-driven architecture
    3. Blocking calls
    4. Non-blocking calls
    5. Back-pressure
    6. Implementing reactive with the Spring 5 Framework
      1. Reactive Streams
    7. Spring Web reactive module
      1. Implementing a reactive web application at the server side
      2. The Annotation-based programming model
      3. The functional programming model
      4. Implementing a Reactive Client-Side application
    8. Request and response body conversion
    9. Summary
  13. Implementing Concurrency Patterns
    1. Active object pattern
    2. Monitor object pattern
    3. Half-Sync/Half-Async patterns
    4. Leader/follower pattern
    5. Reactor pattern
    6. Thread-specific storage pattern
      1. Best practices for concurrency module
    7. Summary

Product information

  • Title: Spring 5 Design Patterns
  • Author(s): Dinesh Rajput
  • Release date: October 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781788299459