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

Learning Spring 5.0

Book Description

Build, test, and secure robust enterprise-grade applications using the Spring Framework

About This Book

  • Build an enterprise application throughout the book that communicates with a microservice
  • Define and inject dependencies into your objects using the IoC container
  • Make use of Spring’s reactive features including tools and implement a reactive Spring MVC application

Who This Book Is For

This book is for Java developers who want to make use of the Spring framework to simplify their programming needs.

What You Will Learn

  • Get to know the basics of Spring development and gain fundamental knowledge about why and where to use Spring Framework
  • Explore the power of Beans using Dependency Injection, wiring, and Spring Expression Language
  • Implement and integrate a persistent layer in your application and also integrate an ORM such as Hibernate
  • Understand how to manage cross-cutting with logging mechanism, transaction management, and more using Aspect-oriented programming
  • Explore Spring MVC and know how to handle requesting data and presenting the response back to the user
  • Get to grips with the integration of RESTful APIs and Messaging with WebSocket and STOMP
  • Master Reactive Programming using Spring MVC to handle non-blocking streams

In Detail

Spring is the most widely used framework for Java programming and with its latest update to 5.0, the framework is undergoing massive changes. Built to work with both Java 8 and Java 9, Spring 5.0 promises to simplify the way developers write code, while still being able to create robust, enterprise applications.

If you want to learn how to get around the Spring framework and use it to build your own amazing applications, then this book is for you.

Beginning with an introduction to Spring and setting up the environment, the book will teach you in detail about the Bean life cycle and help you discover the power of wiring for dependency injection. Gradually, you will learn the core elements of Aspect-Oriented Programming and how to work with Spring MVC and then understand how to link to the database and persist data configuring ORM, using Hibernate.

You will then learn how to secure and test your applications using the Spring-test and Spring-Security modules. At the end, you will enhance your development skills by getting to grips with the integration of RESTful APIs, building microservices, and doing reactive programming using Spring, as well as messaging with WebSocket and STOMP.

Style and approach

Filled with examples, this tutorial-based book will help you gain all the knowledge you need to start producing enterprise-grade applications with Spring.

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 code file.

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. Spring at Glance
    1. Introduction to the Spring Framework
    2. Problems addressed by Spring
      1. Scalability
      2. Plumbing code
      3. Boiler plate code
      4. Unavoidable non-functional code
      5. Unit testing of the application
      6. POJO-based development
      7. Loose coupling through the dependency injection
      8. Declarative programming
      9. Boilerplate code reduction using aspects and templates
      10. Layered architecture
    3. Spring architecture
      1. Core modules
        1. Core
        2. Beans
        3. Context
        4. SpEL
      2. Data access and integration modules
        1. JDBC (DAO)
        2. ORM
        3. OXM
        4. JMS
        5. Transaction
      3. Web MVC and remoting modules
        1. Web
        2. Servlet
        3. Portlet
        4. WebSocket
      4. AOP modules
        1. AOP
        2. Aspects
      5. Instrumentation modules
        1. Instrumentation
        2. Instrument Tomcat
        3. Messaging
      6. Test module
    4. What more Spring supports underneath?
      1. Security module
      2. Batch module
      3. Spring integration
      4. Mobile module
      5. LDAP module
      6. The new module
    5. Spring road map
      1. 1.0 March 2004
      2. 2.0 October 2006
      3. 2.5 November 2007
      4. 3.0 GA December 2009
      5. 3.1 GA December 2011
      6. 4.0 December 2013
      7. 5.0 RC1
    6. Container - the heart of Spring
      1. Inversion of Control
      2. BeanFactory--the past
        1. XMLBeanFactory
      3. ApplicationContext--the present
        1. ClassPathXmlApplicationContext
        2. FileSystemXmlApplicationContext
        3. WebXmlApplicationContext
    7. How are beans made available from container?
      1. Summary
  3. Dependency Injection
    1. The life of a bean
      1. Loading the configuration
      2. Object creation
      3. Setting the bean name
      4. Setting the bean factory
      5. Bean post processing with postProcessBeforeInitialization
      6. Property population
      7. Initializing the bean
        1. The afterPropertiesSet() method
        2. The Custom init() method
      8. Bean post processing with postProcessAfterInitialization
      9. Use the bean
      10. Destruct the bean
        1. Dispose the bean with destroy()
        2. Destruction with custom destroy()
      11. Case 1: Using custom initialization and destruction methods
      12. Case 2: Using InitializingBean to provide initialization
      13. Case 3: Using DisposableBean to provide release of memory
      14. Case 4: Making the bean aware of the container
      15. Case 5: Using BeanPostProcessor
    2. Using JSR-250 annotations for the bean life cycle
    3. Instance creation
      1. Using constructor
      2. Using factory method
        1. Using instance factory method
        2. Using static factory method
    4. Dependency injection
      1. XML-based configuration
      2. Setter injection
      3. Constructor injection
        1. Way 1: Without ambiguities
        2. Way 2: With ambiguities
      4. Namespace p for property
        1. Namespace p for property of secondary types
          1. Configuring the inner beans
          2. Inheritance mapping
          3. Configuring the null values
          4. Configuring Collection
        2. Bean scope
      5. Annotation-based configuration
        1. Spring-based annotations
          1. Stereotype annotations
          2. Wiring annotations
          3. Case 1 - Using @Autowired
          4. Case 2 - Using autowired=false
          5. Case 3 - Using @Qualifier
          6. Case 3 - Using @Required
          7. Case 4 - Scope management annotations
        2. JSR standard annotation
          1. Annotations for life cycle
      6. Spring Expression Language
        1. Using literals
        2. Using bean reference
        3. Using mathematical, logical, and relational operators
    5. Summary
  4. Accelerate with Spring DAO
    1. Handling databases
      1. DataSource
        1. Configuring DataSource
          1. Case 1 - Using XML configuration of DriverManagerDataSource
          2. Case 2 - Using annotations for DriverManagerDataSource
      2. Using template classes to perform JDBC operations
        1. JdbcTemplate
        2. NamedParameterJdbcTemplate
          1. Using JdbcTemplate
          2. Using NamedParameterJdbctemplate
        3. SimpleJdbcInsert
        4. SimpleJdbcCall
        5. Modeling JDBC operations
    2. Handling exceptions
    3. Object relation mapping
      1. Hibernate
        1. Hibernate architecture
          1. Configuration interface
          2. The SessionFactory interface
          3. Session interface
          4. Transaction interface
          5. Query interface
          6. Criteria interface
          7. Case 1 - Using SessionFactory
        2. Cache declaration
          1. @Cacheable
          2. @Caching
          3. @CacheConfig
          4. @CacheEvict
          5. @CachePut
        3. The cache configuration
    4. Summary
  5. Aspect-Oriented Programming
    1. Core and cross-cutting concerns
      1. Aspect-oriented programming
        1. Join point
        2. Pointcut
        3. Advice
          1. Before
          2. After
          3. After returning
          4. After throwing
          5. Around
        4. Aspect
        5. Introduction
        6. Target object
        7. AOP proxy
        8. Weaving
          1. AspectJ
          2. AspectWertz
          3. JBoss AOP
          4. Dynaop
          5. CAESAR
          6. Spring AOP
        9. XML-based aspect configuration
          1. Using method signatures
          2. Using type
          3. Using Bean name
          4. Using this
          5. Using target
          6. For annotation tracking
      2. Part 1 - Creating applications for the core concern (JDBC)
      3. Part 2 - Integration of Log4j
      4. Part 3 - Writing logging aspects
        1. Adding After advice
        2. Adding After Returning advice
        3. Adding Around advice
        4. Adding After throwing advice
      5. Annotation-based aspects
      6. Introduction advice
    2. Summary
  6. Be Consistent - Transaction Management
    1. Understanding transactions
      1. Atomicity
      2. Consistency
      3. Isolation
      4. Durability
    2. Life cycle of transaction management
      1. Local transaction
      2. Global or distributed transactions
      3. Spring and transaction management
        1. Programmatic transaction management
          1. Using PlatformTransactionManager
          2. TransactionTemplate
        2. Declarative transaction management
          1. XML-based declarative transaction management
          2. Annotation-based transaction management
    3. Summary
  7. Exploring Spring MVC
    1. Features for faster development
    2. The Spring MVC
      1. The Front Controller DispatcherServlet
      2. The controller
      3. The ModelAndView interface
      4. The ModelMap interface
      5. The ViewResolver interface
      6. First Spring MVC demo
    3. A deep dive into Spring MVC
      1. Case 1 - Reading request parameters
        1. @RequestMapping variants
      2. Case 2 - Handling form submission
        1. Form submission
          1. Displaying the form
          2. Postprocessing the form
        2. Form preprocessing
          1. Defining the attribute with values to add in the view
          2. Populating the values of the attribute in the form
      3. Performing Form Validation in Spring MVC
      4. Custom validator based on Spring Validator interface
        1. Using @InitBinder and @Valid for validation
          1. Externalizing the messages
          2. Using ValidationUtils
      5. JSR annotation-based validation
        1. Part 1 - Creating a basic application
        2. Part 2 - Applying validations
          1. Using the message attribute
          2. Using the properties file
    4. Summary
  8. Be Assured, Take a Test Drive
    1. Testing - an important step
      1. Unit testing
      2. Integration testing
      3. System testing
      4. User acceptance testing
    2. Testing tools
      1. JTest
      2. JMeter
      3. Grinder
      4. JWalk
      5. PowerMock
      6. TestNG
      7. The Arquillian framework
        1. The ShrinkWrap framework
          1. Test runners
          2. Java container
          3. Integration of test cases into the Java container
      8. JUnit
    3. Phase 1 - Unit testing DAO Unit testing by JUnit
      1. Creating a base application
      2. Performing testing
      3. Spring TestContext framework
        1. @ContextConfiguration
        2. @WebAppConfiguration
    4. Mock testing
      1. Mockito
        1. Spring MVC controller testing using the Spring TestContext framework
        2. Spring MockMvc
    5. Phase 2 - Integration testing
      1. Integration testing of service and DAO layers
      2. Integration testing of controller and service layer
    6. Phase 3 - System testing
      1. Using Eclipse IDE for deployment
      2. Manually deploying the application
      3. Summary
  9. Make It Safe - Security Management
    1. Importance of security in an application
      1. Authentication
        1. Basic authentication
        2. Form-based authentication
      2. Authorization
    2. The Spring Security framework
    3. New features added in Spring Security 5.0
    4. Core components of Spring Security
      1. SecurityContextHolder
      2. Authentication
      3. UserDetailsService
      4. GrantedAuthority
      5. AuthenticationManager
    5. Spring Security mechanism
    6. Configuring security in Spring via namespace
      1. Configuring filters
      2. Configuring <http>
    7. Configuring AuthenticationManager
      1. Step 1 - Creating a basic Spring MVC application
      2. Step 2 - Adding security
      3. In-memory authentication
      4. The JDBC authentication
      5. The Remember-Me authentication
    8. Logout
      1. Demo logout
    9. Managing the Session
      1. Working of the Spring session
        1. Demo of Session management to limit maximum session to handle per user
      2. Method-level security
        1. @Secured
        2. @PreAuthorize and @PostAuthorize
        3. Demo applying method-level security
      3. Summary
  10. Explore the Power of RESTful Web Services
    1. Web services
      1. Web services, the need
        1. Usability
        2. Reusing the developed application
        3. Loosely-coupled modules
        4. Ease in deployment
      2. Types of web service
        1. SOAP web service
        2. RESTful web service
      3. Spring and RESTful web services
      4. RestTemplate
      5. Presentation of the data
        1. HTTP-based message converters
        2. Negotiating view-based rendering of the view
    2. Summary
  11. Exchange Information - Use Messaging
    1. Spring and messaging
      1. Using WebSocket
    2. Overview of the WebSocket API
      1. Creating a WebSocketHandler
      2. Registering WebSocketHandler
        1. Java-based configuration
        2. XML-based configuration
        3. WebSocket engine configuration
          1. Configuration of allowed origins
    3. SockJS
    4. STOMP
      1. Spring STOMP support
    5. Summary
  12. Be Active - Reactive Programming
    1. Introduction to Reactive Streams
    2. Reactive Stream implementations
      1. RxJava 1.x and RxJava2.x
      2. Akka
        1. Components of the Akka toolkit
          1. ActorSystem
          2. ActorRef
          3. MessageDispatcher
          4. MailBox
          5. MessageQueue
          6. Actor
      3. Vert.x
      4. Slick
      5. Project Reactor
      6. Operators
    3. Reactive types
      1. The Future interface
    4. Understanding stream
      1. Reactive streams in depth
        1. Back pressure
    5. Spring WebMVC and Spring web reactive programming
      1. The reactive web application
    6. WebClient
    7. Summary