You are previewing Spring Recipes: A Problem-Solution Approach, Third Edition.
O'Reilly logo
Spring Recipes: A Problem-Solution Approach, Third Edition

Book Description

Spring Recipes: A Problem-Solution Approach, Third Edition builds upon the best-selling success of the previous editions and focuses on the latest Spring Framework features for building enterprise Java applications. This book provides code recipes for the following, found in the latest Spring:

  • Spring fundamentals: Spring IoC container, Spring AOP/ AspectJ, and more
  • Spring enterprise: Spring Java EE integration, Spring Integration, Spring Batch, Spring Remoting, messaging, transactions, and working with big data and the cloud using Hadoop and MongoDB
  • Spring web: Spring MVC, other dynamic scripting, integration with the popular Grails Framework (and Groovy), REST/web services, and more
  • This book guides you step-by-step through topics using complete and real-world code examples. When you start a new project, you can consider copying the code and configuration files from this book, and then modifying them for your needs. This can save you a great deal of work over creating a project from scratch

    Table of Contents

    1. Cover
    2. Title
    3. Copyright
    4. Dedication
    5. Contents at a Glance
    6. Contents
    7. About the Authors
    8. About the Technical Reviewer
    9. Acknowledgments
    10. Introduction
    11. Chapter 1: Spring Development Tools
      1. 1-1. Build a Spring application with the Spring Tool Suite
        1. Problem
        2. Solution
        3. How It Works
      2. 1-2. Build a Spring application with the Eclipse IDE
        1. Problem
        2. Solution
        3. How It Works
      3. 1-3. Build a Spring application with the IntelliJ IDE
        1. Problem
        2. Solution
        3. How It Works
      4. 1-4. Build a Spring application with the Maven command line interface
        1. Problem
        2. Solution
        3. How It Works
      5. 1-5. Build a Spring application with the Gradle command line interface
        1. Problem
        2. Solution
        3. How It Works
      6. 1.6 Build a Spring application with the Gradle Wrapper
        1. Problem
        2. Solution
        3. How It Works
      7. Summary
    12. Chapter 2: Spring Core Tasks
      1. 2-1. Manage and Configure POJOs with the Spring IoC Container
        1. Problem
        2. Solution
        3. How It Works
      2. 2-2. Create POJOs by Invoking a Constructor
        1. Problem
        2. Solution
        3. How It Works
      3. 2-3. Use POJO References, Auto-Wiring, and Imports to Interact with Other POJOs
        1. Problem
        2. Solution
        3. How It Works
      4. 2-4. Configure POJOs with Java Collection Attributes
        1. Problem
        2. Solution
        3. How It Works
      5. 2-5. Set a POJOs Scope
        1. Problem
        2. Solution
        3. How It Works
      6. 2-6. Use Data from External Resources (Text Files, XML Files, Properties Files, or Image Files)
        1. Problem
        2. Solution
        3. How It Works
      7. 2-7. Resolve I18N Text Messages for Different Locales in Properties Files
        1. Problem
        2. Solution
        3. How It Works
      8. 2-8. Customize POJO Initialization and Destruction
        1. Problem
        2. Solution
      9. 2-9. Create Post Processors to Validate and Modify POJOs
        1. Problem
        2. Solution
        3. How It Works
      10. 2-10. Create POJOs with a Factory (Static Method, Instance Method, Spring’s FactoryBean)
        1. Problem
        2. Solution
        3. How It Works
      11. 2-11. Use Spring Environments and Profiles to Load Different Sets of POJOs
        1. Problem
        2. Solution
        3. How It Works
      12. 2-12. Aspect Orientated Programming
        1. Problem
        2. Solution
        3. How It Works
      13. 2-13. Declare POJOs from Static Fields or Object Properties
        1. Problem
        2. Solution
        3. How It Works
      14. 2-14. Making POJOs Aware of Spring’s IoC Container Resources
        1. Problem
        2. Solution
        3. How It Works
      15. 2-15. Communicate Application Events Between POJOs
        1. Problem
        2. Solution
        3. How It Works
      16. 2-16. Use Property Editors in Spring
        1. Problem
        2. Solution
        3. How It Works
      17. 2-17. Inherit POJO Configurations
        1. Problem
        2. Solution
        3. How It Works
      18. 2-18. Implement POJOs with Scripting Languages
        1. Problem
        2. Solution
        3. How It Works
      19. 2-19. Inject Spring POJOs into Scripts
        1. Problem
        2. Solution
        3. How It Works
      20. 2-20. Refresh POJOs from Scripts
        1. Problem
        2. Solution
        3. How It Works
      21. 2-21. Define Script Sources as Inline Code and not in External Files
        1. Problem
        2. Solution
        3. How It Works
      22. 2-22. Use the Spring Expression Language to Configure POJOs
        1. Problem
        2. Solution
        3. How It Works
      23. Summary
    13. Chapter 3: Spring Annotation Driven Core Tasks
      1. 3-1. Using Java Config to configure POJOs
        1. Problem
        2. Solution
        3. How It Works
      2. 3-2. Create POJOs by Invoking a Constructor
        1. Problem
        2. Solution
        3. How It Works
      3. 3-3. Use POJO References and Auto-Wiring to Interact with other POJOs
        1. Problem
        2. Solution
        3. How It Works
      4. 3-4. Auto-wire POJOs the @Resource and @Inject annotation
        1. Problem
        2. Solution
        3. How It Works
      5. 3-5. Set a POJOs Scope with the @Scope annotation
        1. Problem
        2. Solution
        3. How It Works
      6. 3-6. Use data from External Resources (Text files, XML files, properties files, or image files)
        1. Problem
        2. Solution
        3. How It Works
      7. 3-7. Resolve I18N Text Messages for different locales in properties files
        1. Problem
        2. Solution
        3. How It Works
      8. 3-8. Customize POJO Initialization and Destruction with annotations
        1. Problem
        2. Solution
        3. How It Works
      9. 3-9. Create Post Processors to validate and modify POJOs
        1. Problem
        2. Solution
        3. How It Works
      10. 3-10. Create POJOs with a factory (Static method, Instance method, Spring’s FactoryBean)
        1. Problem
        2. Solution
        3. How It Works
      11. 3-11. Use Spring Environments and Profiles to load different sets of POJOs
        1. Problem
        2. Solution
        3. How it works
      12. 3-12. Aspect Orientated programming with Annotations
        1. Problem
        2. Solution
        3. How It Works
      13. 3-13. Accessing the Join Point Information
        1. Problem
        2. Solution
        3. How It Works
      14. 3-14. Specifying Aspect Precedence with the @Order annotation
        1. Problem
        2. Solution
        3. How It Works
      15. 3-15. Reuse Aspect Pointcut Definitions
        1. Problem
        2. Solution
        3. How It Works
      16. 3-16. Writing AspectJ Pointcut Expressions
        1. Problem
        2. Solution
        3. How It Works
      17. 3-17. AOP introductions for POJOs
        1. Problem
        2. Solution
        3. How It Works
      18. 3-18. Introduce states to your POJOs with AOP
        1. Problem
        2. Solution
        3. How It Works
      19. 3-19. Load-Time Weaving AspectJ Aspects in Spring
        1. Problem
        2. Solution
        3. How It Works
      20. 3-20. Configuring AspectJ Aspects in Spring
        1. Problem
        2. Solution
        3. How It Works
      21. 3-21. Inject POJOs into Domain Objects with AOP
        1. Problem
        2. Solution
        3. How It Works
      22. 3-22. Concurrency with Spring and TaskExecutors
        1. Problem
        2. Solution
        3. How It Works
      23. Summary
    14. Chapter 4: Spring @MVC
      1. 4-1. Developing a Simple Web Application with Spring MVC
        1. Problem
        2. Solution
        3. How It Works
      2. 4-2. Mapping requests with @RequestMapping
        1. Problem
        2. Solution
        3. How It Works
      3. 4-3. Intercepting Requests with Handler Interceptors
        1. Problem
        2. Solution
        3. How It Works
      4. 4-4. Resolving User Locales
        1. Problem
        2. Solution
        3. How It Works
      5. 4-5. Externalizing Locale-Sensitive Text Messages
        1. Problem
        2. Solution
        3. How It Works
      6. 4-6. Resolving Views by Names
        1. Problem
        2. Solution
        3. How It Works
      7. 4-7. Views and Content Negotiation
        1. Problem
        2. Solution
        3. How It Works
      8. 4-8. Mapping Exceptions to Views
        1. Problem
        2. Solution
        3. How It Works
        4. Mappings exceptions using @ExceptionHandler
      9. 4-9. Handling Forms with Controllers
        1. Problem
        2. Solution
        3. How It Works
      10. 4-10. Bean validation with Annotations (JSR-303)
        1. Problem
        2. Solution
        3. How It Works
      11. 4-11. Creating Excel and PDF Views
        1. Problem
        2. Solution
        3. How It Works
      12. Summary
    15. Chapter 5: Spring REST
      1. 5-1. Publishing XML with REST Services
        1. Problem
        2. Solution
        3. How It Works
      2. 5-2. Publishing JSON with REST services
        1. Problem
        2. Solution
        3. How It Works
      3. 5-3. Accessing a REST Service with Spring
        1. Problem
        2. Solution
        3. How It Works
      4. 5-4. Publishing RSS and Atom feeds
        1. Problem
        2. Solution
        3. How It Works
      5. Summary
    16. Chapter 6: Spring Social
      1. 6-1. Setting Up Spring Social
        1. Problem
        2. Solution
        3. How It Works
      2. 6-2. Connecting to Twitter
        1. Problem
        2. Solution
        3. How It Works
      3. 6-3. Connecting to Facebook
        1. Problem
        2. Solution
        3. How It Works
      4. 6-4. Showing Service Provider Connection Status
        1. Problem
        2. Solution
        3. How It Works
      5. 6-5. Using the Twitter API
        1. Problem
        2. Solution
        3. How It Works
      6. 6-6. Using a Persistent UsersConnectionRepository
        1. Problem
        2. Solution
        3. How It Works
      7. 6-7. Integrating Spring Social and Spring Security
        1. Problem
        2. Solution
        3. How It Works
      8. Summary
    17. Chapter 7: Spring Security
      1. 7-1. Securing URL Access
        1. Problem
        2. Solution
        3. How It Works
      2. 7-2. Logging In to Web Applications
        1. Problem
        2. Solution
        3. How It Works
      3. 7-3. Authenticating Users
        1. Problem
        2. Solution
        3. How It Works
      4. 7-4. Making Access Control Decisions
        1. Problem
        2. Solution
        3. How It Works
      5. 7-5. Securing Method Invocations
        1. Problem
        2. Solution
        3. How It Works
      6. 7-6. Handling Security in Views
        1. Problem
        2. Solution
        3. How It Works
      7. 7-7. Handling Domain Object Security
        1. Problem
        2. Solution
        3. How It Works
      8. Summary
    18. Chapter 8: Spring Mobile
      1. Recipe 8-1. Device detection without Spring Mobile
        1. Problem
        2. Solution
        3. How It Works
      2. Recipe 8-2. Device detection with Spring Mobile
        1. Problem
        2. Solution
        3. How It Works
      3. 8-3. Using Site preferences
        1. Problem
        2. Solution
        3. How It Works
      4. 8-4. Using the Device Information to Render Views
        1. Problem
        2. Solution
        3. How It Works
      5. 8-5. Site Switching
        1. Problem
        2. Solution
        3. How It Works
      6. Summary
    19. Chapter 9: Spring with Other Web Frameworks
      1. 9-1. Accessing Spring in Generic Web Applications
        1. Problem
        2. Solution
        3. How It Works
      2. 9-2. Using Spring in Your Servlets and Filters
        1. Problem
        2. Solution
        3. How It Works
      3. 9-3. Integrating Spring with JSF
        1. Problem
        2. Solution
        3. How It Works
      4. Summary
    20. Chapter 10: Data Access
      1. Problems with Direct JDBC
        1. Setting Up the Application Database
        2. Understanding the Data Access Object Design Pattern
        3. Implementing the DAO with JDBC
        4. Configuring a Data Source in Spring
        5. Running the DAO
        6. Taking It a Step Further
      2. 10-1. Using a JDBC Template to Update a Database
        1. Problem
        2. Solution
        3. How It Works
      3. 10-2. Using a JDBC Template to Query a Database
        1. Problem
        2. Solution
        3. How It Works
      4. 10-3. Simplifying JDBC Template Creation
        1. Problem
        2. Solution
        3. How It Works
      5. 10-4. Using Named Parameters in a JDBC Template
        1. Problem
        2. Solution
        3. How It Works
      6. 10-5. Handling Exceptions in the Spring JDBC Framework
        1. Problem
        2. Solution
        3. How It Works
      7. 10-6. Problems with Using ORM Frameworks Directly
        1. Problem
        2. Solution
        3. How It Works
        4. Persisting Objects Using the Hibernate API with Hibernate XML Mappings
        5. Persisting Objects Using the Hibernate API with JPA Annotations
        6. Persisting Objects Using JPA with Hibernate as the Engine
      8. 10-7. Configuring ORM Resource Factories in Spring
        1. Problem
        2. Solution
        3. How It Works
      9. 10-8. Persisting Objects with Spring’s ORM Templates
        1. Problem
        2. Solution
        3. How It Works
      10. 10-9. Persisting Objects with Hibernate’s Contextual Sessions
        1. Problem
        2. Solution
        3. How It Works
      11. 10-10. Persisting Objects with JPA’s Context Injection
        1. Problem
        2. Solution
        3. How It Works
      12. 10-11. Simplify JPA with Spring Data JPA
        1. Problem
        2. Solution
        3. How It Works
      13. Summary
    21. Chapter 11: Spring Transaction Management
      1. 11-1. Problems with Transaction Management
      2. 11-2. Choosing a Transaction Manager Implementation
        1. Problem
        2. Solution
        3. How It Works
      3. 11-3. Managing Transactions Programmatically with the Transaction Manager API
        1. Problem
        2. Solution
        3. How It Works
      4. 11-4. Managing Transactions Programmatically with a Transaction Template
        1. Problem
        2. Solution
        3. How It Works
      5. 11-5. Managing Transactions Declaratively with Transaction Advices
        1. Problem
        2. Solution
        3. How It Works
      6. 11-6. Managing Transactions Declaratively with the @Transactional Annotation
        1. Problem
        2. Solution
        3. How It Works
      7. 11-7. Setting the Propagation Transaction Attribute
        1. Problem
        2. Solution
        3. How It Works
      8. 11-8. Setting the Isolation Transaction Attribute
        1. Problem
        2. Solution
        3. How It Works
      9. 11-9. Setting the Rollback Transaction Attribute
        1. Problem
        2. Solution
        3. How It Works
      10. 11-10. Setting the Timeout and Read-Only Transaction Attributes
        1. Problem
        2. Solution
        3. How It Works
      11. 11-11. Managing Transactions with Load-Time Weaving
        1. Problem
        2. Solution
        3. How It Works
      12. Summary
    22. Chapter 12: Spring Batch
      1. Runtime Metadata Model
      2. 12-1. Setting Up Spring Batch’s Infrastructure
        1. Problem
        2. Solution
        3. How It Works
      3. 12-2. Reading and Writing
        1. Problem
        2. Solution
        3. How It Works
      4. 12-3. Writing a Custom ItemWriter and ItemReader
        1. Problem
        2. Solution
        3. How It Works
      5. 12-4. Processing Input Before Writing
        1. Problem
        2. Solution
        3. How It Works
      6. 12-5. Better Living through Transactions
        1. Problem
        2. Solution
        3. How It Works
      7. 12-6. Retrying
        1. Problem
        2. Solution
        3. How It Works
      8. 12-7. Controlling Step Execution
        1. Problem
        2. Solution
        3. How It Works
      9. 12-8. Launching a Job
        1. Problem
        2. Solution
        3. How It Works
      10. 12-9. Parameterizing a Job
        1. Problem
        2. Solution
        3. How It Works
      11. Summary
    23. Chapter 13: NoSQL and BigData
      1. 13-1. Using MongoDB
        1. Problem
        2. Solution
        3. How It Works
      2. 13-2. Using Redis
      3. 13-3. Using Hadoop
        1. Problem
        2. Solution
        3. How It Works
      4. 13-4. Using Neo4j
        1. Problem
        2. Solution
        3. How It Works
      5. Summary
    24. Chapter 14: Spring Java Enterprise Services and Remoting Technologies
      1. 14-1.Register Spring POJOs as JMX MBeans
        1. Problem
        2. Solution
        3. How It Works
      2. 14-2. Publish and Listen to JMX Notifications
        1. Problem
        2. Solution
        3. How It Works
      3. 14-3. Access Remote JMX MBeans in Spring
        1. Problem
        2. Solution
        3. How It Works
      4. 14-4. Send E-mail with Spring’s E-mail Support
        1. Problem
        2. Solution
        3. How It Works
      5. 14-5. Schedule tasks with Spring’s Quartz Support
        1. Problem
        2. Solution
        3. How It Works
      6. 14-6. Schedule tasks with Spring’s Scheduling
        1. Problem
        2. Solution
        3. How It Works
      7. 14-7. Expose and Invoke Services through RMI
        1. Problem
        2. Solution
        3. How It Works
      8. 14-8. Expose and Invoke Services through HTTP
        1. Problem
        2. Solution
        3. How It Works
      9. 14-9. Expose and invoke SOAP Web Services with JAX-WS
        1. Problem
        2. Solution
        3. How It Works
      10. 14-10. Introduction to contract first SOAP Web Services
        1. Problem
        2. Solution
        3. How It Works
      11. 14-11. Expose and invoke SOAP Web Services with Spring-WS
        1. Problem
        2. Solution
        3. How It Works
      12. 14-12. Develop SOAP Web Services with Spring-WS and XML Marshalling
        1. Problem
        2. Solution
        3. How It Works
      13. Summary
    25. Chapter 15: Spring Messaging
      1. 15-1. Send and Receive JMS Messages with Spring
        1. Problem
        2. Solution
        3. How It Works
      2. 15-2. Convert JMS Messages
        1. Problem
        2. Solution
        3. How It Works
      3. 15-3. Manage JMS Transactions
        1. Problem
        2. Approach
        3. Solution
      4. 15-4. Create Message-Driven POJOs in Spring
        1. Problem
        2. Solution
        3. How It Works
      5. 15-5. Cache and pool JMS connections
        1. Problem
        2. Solution
        3. How It Works
      6. 15-6 Send and Receive AMQP Messages with Spring
        1. Problem
        2. Solution
        3. How It Works
      7. Summary
    26. Chapter 16: Spring Integration
      1. 16-1. Integrating One System with Another Using EAI
        1. Problem
        2. Solution
        3. How It Works
      2. 16-2. Integrating Two Systems Using JMS
        1. Problem
        2. Solution
        3. How It Works
      3. 16-3. Interrogating Spring Integration Messages for Context Information
        1. Problem
        2. Solution
        3. How It Works
      4. 16-4. Integrating Two Systems Using a File System
        1. Problem
        2. Solution
        3. How It Works
      5. 16-5. Transforming a Message from One Type to Another
        1. Problem
        2. Solution
        3. How It Works
      6. 16-6. Error Handling Using Spring Integration
        1. Problem
        2. Solution
        3. How It Works
      7. 16-7. Forking Integration Control: Splitters and Aggregators
        1. Problem
        2. Solution
        3. How It Works
      8. 16-8. Conditional Routing with Routers
        1. Problem
        2. Solution
        3. How It Works
      9. 16-9. Staging Events Using Spring Batch
        1. Problem
        2. Solution
        3. How It Works
      10. 16-10. Using Gateways
        1. Problem
        2. Solution
        3. How It Works
      11. Summary
    27. Chapter 17: Spring Testing
      1. 17-1. Creating Tests with JUnit and TestNG
        1. Problem
        2. Solution
        3. How It Works
      2. 17-2. Creating Unit Tests and Integration Tests
        1. Problem
        2. Solution
        3. How It Works
      3. 17-3. Unit Testing Spring MVC Controllers
        1. Problem
        2. Solution
        3. How It Works
      4. 17-4. Managing Application Contexts in Integration Tests
        1. Problem
        2. Solution
        3. How It Works
      5. 17-5. Injecting Test Fixtures into Integration Tests
        1. Problem
        2. Solution
        3. How It Works
      6. 17-6. Managing Transactions in Integration Tests
        1. Problem
        2. Solution
        3. How It Works
      7. 17-7. Accessing a Database in Integration Tests
        1. Problem
        2. Solution
        3. How It Works
      8. 17-8. Using Spring’s Common Testing Annotations
        1. Problem
        2. Solution
        3. How It Works
      9. 17-9. Integration Testing Spring MVC Controllers
        1. Problem
        2. Solution
        3. How It Works
      10. Summary
    28. Chapter 18: Grails
      1. 18-1. Getting and Installing Grails
        1. Problem
        2. Solution
        3. How It Works
      2. 18-2. Creating a Grails Application
        1. Problem
        2. Solution
        3. How It Works
      3. 18-3. Grails Plug-Ins
        1. Problem
        2. Solution
        3. How It Works
      4. 18-4. Developing, Producing, and Testing in Grails Environments
        1. Problem
        2. Solution
        3. How It Works
      5. 18-5. Creating an Application’s Domain Classes
        1. Problem
        2. Solution
        3. How It Works
      6. 18-6. Generating CRUD Controllers and Views for an Application’s Domain Classes
        1. Problem
        2. Solution
        3. How It Works
      7. 18-7. Internationalization (I18n) Message Properties
        1. Problem
        2. Solution
        3. How It Works
      8. 18-8. Changing Permanent Storage Systems
        1. Problem
        2. Solution
        3. How It Works
      9. 18-9. Logging
        1. Problem
        2. Solution
        3. How It Works
      10. 18-10. Running Unit and Integration Tests
        1. Problem
        2. Solution
        3. How It Works
      11. 18-11. Using Custom Layouts and Templates
        1. Problem
        2. Solution
        3. How It Works
      12. 18-12. Using GORM Queries
        1. Problem
        2. Solution
        3. How It Works
      13. 18-13. Creating Custom Tags
        1. Problem
        2. Solution
        3. How It Works
      14. 18-14. Adding Security
        1. Problem
      15. Summary
    29. Index