Spring 6 Recipes: A Problem-Solution Approach to Spring Framework, 5th Edition
- Length: 784 pages
- Edition: 5
- Language: English
- Publisher: Apress
- Publication Date: 2023-05-23
- ISBN-10: 1484286480
- ISBN-13: 9781484286487
- Sales Rank: #3080848 (See Top 100 Books)
This in-depth Spring-based Java developer code reference has been updated and now solves many of your Spring Framework 6 problems using reusable, complete and real-world working code examples. Spring 6 Recipes (5th Edition) now includes Spring Native which speeds up your Java-based Spring Framework built enterprise, native cloud, web applications and microservices. It also has been updated to now include Spring R2DBC for Reactive Relational Database Connectivity, a specification to integrate SQL databases, like PostgreSQL, MySQL and more, using reactive drivers.
Furthermore, this book includes additional coverage of WebFlux for more reactive Spring web applications. Reactive programming allows you to build systems that are resilient to high load, especially common in the more complex enterprise, native cloud applications that Spring Framework lets you build. This updated edition also uses code snippets and examples based on newest available standard long term support release of Java. When you start a new project, you’ll be able to copy the code and configuration files from this book, and then modify them for your specific Spring Framework-based application needs. This can save you a great deal of work over creating a project from scratch.
This powerful code reference is a “must have” for your print or digital library. This developer cookbook comes with accompanying source code that is freely available on GitHub under latest Creative Commons open source licensing.
What You’ll Learn
- Get re-usable code recipes and snippets for core Spring, annotations and other development tools
- Dive into Spring Native which merges the popular Spring and GraalVM for increased performance, speed and reliability of your Spring Framework 6 applications
- Explore Spring R2BC for reactive relational database connectivity with SQL
- Build reactive Spring MVC-based web applications and microservices using WebFlux
- Enhance your enterprise or native cloud applications using Spring Transaction Management
- Test, secure and deploy your Spring applications using reusable code templates available
Who This Book Is For
This book is for experienced Java programmers, software developers with experience using the Spring Framework.
Table of Contents About the Authors About the Technical Reviewer Acknowledgments Introduction Chapter 1: Spring Core Tasks 1-1. Using Java Config to Configure POJOs Problem Solution How It Works Create a Class to Be Instantiated as a Bean Create Java Config with @Configuration and @Bean to Create POJOs Instantiate the Spring IoC Container to Scan for Annotations Get POJO Instances or Beans from the IoC Container Create a POJO Class with the @Component Annotation to Create Beans with DAO Instantiate the Spring IoC Container with Filters to Scan for Annotations Get POJO Instances or Beans from the IoC Container 1-2. Create POJOs by Invoking a Constructor Problem Solution How It Works Create the POJO Classes with Constructors Create Java Config for Your POJOs 1-3. Use POJO References and Autowiring to Interact with Other POJOs Problem Solution How It Works Reference POJOs in a Java Config Class Autowire POJO Fields with the @Autowired Annotation Autowire Methods and Constructors with the @Autowired Annotation and Make Autowiring Optional Resolve Autowire Ambiguity with the @Primary and @Qualifier Annotations Resolve Autowire Ambiguity with the @Primary Annotation Resolve Autowire Ambiguity with the @Qualifier Annotation Resolve POJO References from Multiple Locations 1-4. Autowire POJOs with the @Resource and @Inject Annotations Problem Solution How It Works Autowire POJOs with the @Resource Annotation Autowire POJOs with the @Inject Annotation 1-5. Set a POJO’s Scope with the @Scope Annotation Problem Solution How It Works 1-6. Use Data from External Resources (Text Files, XML Files, Properties Files, or Image Files) Problem Solution How It Works Use Properties File Data to Set Up POJO Instantiation Values Use Data from Any External Resource File for Use in a POJO 1-7. Resolve I18N Text Messages for Different Locales in Properties Files Problem Solution How It Works 1-8. Customize POJO Initialization and Destruction with Annotations Problem Solution How It Works Define Methods to Run Before POJO Initialization and Destruction with @Bean Define Methods to Run Before POJO Initialization and Destruction with @PostConstruct and @PreDestroy Define Lazy Initialization for POJOs with @Lazy Define Initialization of POJOs Before Other POJOs with @DependsOn 1-9. Create Post-processors to Validate and Modify POJOs Problem Solution How It Works Create a POJO to Process Every Bean Instance Create a POJO to Process Selected Bean Instances 1-10. Create POJOs with a Factory (Static Method, Instance Method, Spring’s FactoryBean) Problem Solution How It Works Create POJOs by Invoking a Static Factory Method Create POJOs by Invoking an Instance Factory Method Create POJOs Using Spring’s Factory Bean 1-11. Use Spring Environments and Profiles to Load Different Sets of POJOs Problem Solution How It Works Create Java Configuration Classes with the @Profile Annotation Load Profiles into Environments Set a Default Profile 1-12. Making POJOs Aware of Spring’s IoC Container Resources Problem Solution How It Works 1-13. Aspect-Oriented Programming with Annotations Problem Solution How It Works Declare Aspects, Advices, and Pointcuts Aspect with the @Before Advice Aspect with the @After Advice Aspect with the @AfterReturning Advice Aspect with the @AfterThrowing Advice Aspect with the @Around Advice 1-14. Accessing the Join Point Information Problem Solution How It Works 1-15. Specifying Aspect Precedence with the @Order Annotation Problem Solution How It Works 1-16. Reuse Aspect Pointcut Definitions Problem Solution How It Works 1-17. Writing AspectJ Pointcut Expressions Problem Solution How It Works Method Signature Patterns Type Signature Patterns Combining Pointcut Expressions Declaring Pointcut Parameters 1-18. AOP Introductions for POJOs Problem Solution How It Works 1-19. Introduce States to Your POJOs with AOP Problem Solution How It Works 1-20. Load-Time Weaving AspectJ Aspects in Spring Problem Solution How It Works Load-Time Weaving by the AspectJ Weaver Load-Time Weaving by the Spring Load-Time Weaver 1-21. Configuring Aspects in Spring Problem Solution How It Works 1-22. Inject POJOs into Domain Objects with AOP Problem Solution How It Works 1-23. Concurrency with Spring and TaskExecutors Problem Solution How It Works 1-24. Communicate Application Events Between POJOs Problem Solution How It Works Define Events Using ApplicationEvent Publish Events Listen to Events 1-25. Create and Register Beans in a Functional Way Problem Solution How It Works Create a Simple Bean in a Functional Way Create Multiple Beans with Dependencies in a Functional Way Modify the BeanDefinition for a Functionally Registered Bean 1-26. Summary Chapter 2: Spring MVC 2-1. Developing a Simple Web Application with Spring MVC Problem Solution How It Works Setting Up a Spring MVC Application Creating the Configuration Files Creating Spring MVC Controllers Creating JSP Views Deploying the Web Application Bootstrap the Application Using a WebApplicationInitializer 2-2. Mapping Requests with @RequestMapping Problem Solution How It Works Mapping Requests by Method Mapping Requests by Class Mapping Requests by HTTP Request Type 2-3. Intercepting Requests with Handler Interceptors Problem Solution How It Works 2-4. Resolving User Locales Problem Solution How It Works Resolving Locales by an HTTP Request Header Resolving Locales by a Session Attribute Resolving Locales by a Cookie Changing a User’s Locale 2-5. Externalizing Locale-Sensitive Text Messages Problem Solution How It Works 2-6. Resolving Views by Names Problem Solution How It Works Resolving Views Based on a Template’s Name and Location Using the ViewResolverRegistry to Register ViewResolvers The Redirect Prefix 2-7. Views and Content Negotiation Problem Solution How It Works 2-8. Mapping Exceptions to Views Problem Solution How It Works Mapping Exceptions Using @ExceptionHandler 2-9. Handling Forms with Controllers Problem Solution How It Works Creating a Form’s Views Creating a Form’s Service Processing Creating a Form’s Controller Initializing a Model Attribute Object and Pre-populating a Form with Values Providing Form Reference Data Binding Properties of Custom Types Validating Form Data Expiring a Controller’s Session Data 2-10. Bean Validation with Annotations (Jakarta Bean Validation API) Problem Solution How It Works 2-11. Creating Excel and PDF Views Problem Solution How It Works Creating Excel Views Creating PDF Views Creating Resolvers for Excel and PDF Views Creating Date-Based PDF and XLS Filenames 2-12. Asynchronous Request Handling with Controllers Problem Solution How It Works Writing an Asynchronous Controller Configuring Async Processing 2-13. Summary Chapter 3: Spring MVC: REST Services 3-1. Publishing XML with REST Services Problem Solution How It Works Using a MarshallingView to Produce XML Using @ResponseBody to Produce XML Using @PathVariable to Limit the Results Using the ResponseEntity to Inform the Client 3-2. Publishing JSON with REST Services Problem Solution How It Works Using a MappingJackson2JsonView to Produce JSON Using @ResponseBody to Produce JSON Using GSON to Produce JSON 3-3. Receiving Payloads with a REST Controller Problem Solution How It Works Receive and Handle the Request with @RequestBody Validating the Request Payload Error Handling with Spring MVC Error Handling with Spring MVC Using RFC-7807 3-4. Accessing a REST Service with Spring Problem Solution How It Works Retrieving Data from a Parameterized URL Retrieving Data as a Mapped Object 3-5. Publishing RSS and Atom Feeds Problem Solution How It Works 3-6. Response Writers Problem Solution How It Works Send Multiple Results in a Response Sending Multiple Results as Events 3-7. Summary Chapter 4: Spring WebFlux 4-1. Developing a Reactive Application with Spring WebFlux Problem Solution How It Works Setting Up a Spring WebFlux Application Bootstrapping the Application Creating Spring WebFlux Controllers Running the Web Application 4-2. Publishing and Consuming JSON with Reactive REST Services Problem Solution How It Works Publishing JSON Consuming JSON Validating the Incoming Payload 4-3. Exception Handling with Reactive Controllers Problem Solution How It Works Create an Exception Handler for Validation Errors Use Problem Details (RFC-7808) with Spring WebFlux 4-4. Handling Forms with Reactive Controllers Problem Solution How It Works Creating a Form’s views Creating a Form’s Service Processing Creating a Form’s Controller Initializing a Model Attribute Object and Pre-populating a Form with Values Providing Form Reference Data Binding Properties of Custom Types Validating Form Data 4-5. Asynchronous Web Client Problem Solution How It Works Retrieving Data from a Parameterized URL Retrieving Data as a Mapped Object 4-6. Writing Reactive Handler Functions Problem Solution How It Works Writing Handler Functions Routing Requests to Handler Functions 4-7. Summary Chapter 5: Spring Security 5-1. Securing URL Access Problem Solution How It Works Securing URL Access Working with CSRF Protection 5-2. Logging Into Web Applications Problem Solution How It Works HTTP Basic Authentication Form-Based Login The Logout Service Anonymous Login Remember-Me Support 5-3. Authenticating Users Problem Solution How It Works Authenticating Users with In-Memory Definitions Authenticating Users Against a Database Encrypting Passwords Authenticating Users Against an LDAP Repository 5-4. Making Access Control Decisions Problem Solution How It Works Using Expressions to Make Access Control Decisions Using Expressions to Make Access Control Decisions Using Spring Beans 5-5. Securing Method Invocations Problem Solution How It Works Securing Methods with Annotations Securing Methods with Annotations and Expressions Filtering with Annotations and Expressions 5-6. Handling Security in Views Problem Solution How It Works Displaying Authentication Information Rendering View Contents Conditionally 5-7. Handling Domain Object Security Problem Solution How It Works Setting Up an ACL Service Maintaining ACLs for Domain Objects Making Access Control Decisions Using Expressions 5-8. Adding Security to a WebFlux Application Problem Solution How It Works Securing URL Access Authenticating Users HTTP Basic Authentication Form-Based Login Working with CSRF Protection Making Access Control Decisions 5-9. Summary Chapter 6: Data Access 6-1. Problems with Direct JDBC 6-2. Setting Up the Application Database Understanding the Data Access Object Design Pattern Implementing the DAO with JDBC Configuring a DataSource in Spring Running the DAO Taking It a Step Further 6-3. Using a JDBC Template to Work with a Database Problem Solution How It Works Updating a Database with a Statement Creator Updating a Database with a Statement Setter Updating a Database with a SQL Statement and Parameter Values Batch Updating a Database 6-4. Using a JDBC Template to Query a Database Problem Solution How It Works Extracting Data with a Row Callback Handler Extracting Data with a Row Mapper Querying for Multiple Rows Querying for a Single Value 6-5. Simplifying JDBC Template Creation Problem Solution How It Works Injecting a JDBC Template Extending the JdbcDaoSupport Class 6-6. Using Named Parameters in a JDBC Template Problem Solution How It Works 6-7. Handling Exceptions in the Spring JDBC Framework Problem Solution How It Works Understanding Exception Handling in the Spring JDBC Framework Customizing Data Access Exception Handling 6-8. Problems with Using ORM Frameworks Directly Problem Solution How It Works Persisting Objects Using the Hibernate API with JPA Annotations Persisting Objects Using JPA with Hibernate as the Engine 6-9. Configuring ORM Resource Factories in Spring Problem Solution How It Works Configuring a Hibernate Session Factory in Spring Configuring a JPA Entity Manager Factory in Spring 6-10. Persisting Objects with Hibernate’s Contextual Sessions Problem Solution How It Works 6-11. Persisting Objects with JPA’s Context Injection Problem Solution How It Works 6-12. Simplify JPA with Spring Data JPA Problem Solution How It Works 6-13. Reactive Database Access with R2DBC Problem with Plain R2DBC Problem Solution How It Works Spring R2DBC DatabaseClient Spring Data R2DBC Template Spring Data R2DBC Repository 6-14. Summary Chapter 7: Spring Transaction Management 7-1. Problems with Transaction Management Managing Transactions with JDBC Commit and Rollback 7-2. Choosing a Transaction Manager Implementation Problem Solution How It Works 7-3. Managing Transactions Programmatically with the Transaction Manager API Problem Solution How It Works Use and Configure the PlatformTransactionManager Use and Configure the ReactiveTransactionManager 7-4. Managing Transactions Programmatically with a Transaction Template Problem Solution How It Works Use the TransactionTemplate Use the TransactionalOperator 7-5. Managing Transactions Declaratively with the @Transactional Annotation Problem Solution How It Works 7-6. Setting the Propagation Transaction Attribute Problem Solution How It Works The REQUIRED Propagation Behavior The REQUIRES_NEW Propagation Behavior 7-7. Setting the Isolation Transaction Attribute Problem Solution How It Works The READ_UNCOMMITTED and READ_COMMITTED Isolation Levels The REPEATABLE_READ Isolation Level The SERIALIZABLE Isolation Level 7-8. Setting the Rollback Transaction Attribute Problem Solution How It Works 7-9. Setting the Timeout and Read-Only Transaction Attributes Problem Solution How It Works 7-10. Managing Transactions with Load-Time Weaving Problem Solution How It Works 7-11. Summary Chapter 8: Spring Batch 8-1. Runtime Metadata Model 8-2. Setting Up Spring Batch’s Infrastructure Problem Solution How It Works 8-3. Reading and Writing Problem Solution How It Works The Job Configuration Input Output Simplifying the ItemReader and ItemWriter Configurations 8-4. Writing a Custom ItemWriter and ItemReader Problem Solution How It Works Writing a Custom ItemReader Writing a Custom ItemWriter 8-5. Processing Input Before Writing Problem Solution How It Works Chaining Processors Together 8-6. Better Living Through Transactions Problem Solution How It Works Transactions Rollbacks 8-7. Retrying Problem Solution How It Works Configuring a Step Retry Template AOP-Based Retries 8-8. Controlling Step Execution Problem Solution How It Works Daily Sales Report Job Sequential Steps Concurrency Conditional Steps with Statuses Conditional Steps with Decisions 8-9. Launching a Job Problem Solution How It Works Launching from a Web Application Running from the Command Line Running on a Schedule 8-10. Parameterizing a Job Problem Solution How It Works Launching a Job with Parameters Accessing JobParameters 8-11. Summary Chapter 9: Spring Data Access with NoSQL 9-1. Using MongoDB Problem Solution How It Works Downloading and Starting MongoDB Connecting to MongoDB Using Spring for Configuration Using a MongoTemplate to Simplify MongoDB Code Using Annotations to Specify Mapping Information Create a Spring Data MongoDB Repository Create a Reactive Spring Data MongoDB Repository 9-2. Using Redis Problem Solution How It Works Downloading and Starting Redis Connecting to Redis Storing Objects with Redis Configuring and Using the RedisTemplate Create a Spring Data Redis Repository Create a Reactive Spring Data Redis Repository 9-3. Using Neo4j Problem Solution How It Works Downloading and Running Neo4j Starting Neo4j Mapping Objects with Neo4j Use Spring to Configure Neo4j Using the Spring Data Neo4j Neo4jTemplate Using Spring Data Neo4j Repositories Simplifying the Spring Neo4j Configuration Using Spring Data Neo4j Reactive Repositories 9-4. Using Couchbase Problem Solution How It Works Download and Install Couchbase Set Up Couchbase Storing and Retrieving Documents with Couchbase Using Spring Using Spring Data CouchbaseTemplate Using Spring Data Couchbase Repositories Using Spring Data Reactive Couchbase Repositories 9-5. Summary Chapter 10: Spring Java Enterprise Services and Remoting Technologies 10-1. Register Spring POJOs as JMX MBeans Problem Solution How It Works Register MBeans Without Spring’s Support Register MBeans with Spring Support Work with Multiple MBean Server Instances Assemble the Management Interface of MBeans Register MBeans with Annotations 10-2. Publish and Listen to JMX Notifications Problem Solution How It Works Publish JMX Notifications Listen to JMX Notifications 10-3. Expose and Access Remote JMX MBeans in Spring Problem Solution How It Works Expose MBeans Through a JMX Connector Server Access Remote MBeans Through an MBean Server Connection Access Remote MBeans Through an MBean Proxy 10-4. Send Email with Spring’s Email Support Problem Solution How It Works Send Email Using the Jakarta Mail API Send Email with Spring’s MailSender Define an Email Template Send Email with Attachments (MIME Messages) 10-5. Schedule Tasks with Spring’s Quartz Support Problem Solution How It Works Use Quartz Without Spring’s Support Use Quartz with Spring’s Support 10-6. Schedule Tasks with Spring’s Scheduling Problem Solution How It Works 10-7. Introduction to Contract-First SOAP Web Services Problem Solution How It Works Create Sample XML Messages Generate an XSD File from Sample XML Messages Optimizing the Generated XSD File Previewing the Generated WSDL File 10-8. Expose and Invoke SOAP Web Services with Spring-WS Problem Solution How It Works Set Up a Spring-WS Application Create Service Endpoints Publish the WSDL File Invoke SOAP Web Services with Spring-WS 10-9. Develop SOAP Web Services with Spring-WS and XML Marshalling Problem Solution How It Works Create Service Endpoints with XML Marshalling Invoking Web Services with XML Marshalling 10-10. Use the Java Flight Recorder to Investigate Application Startup Problem Solution How It Works Enable JFR Event Processing Publishing JFR Events 10-11. Observing Your Application with Micrometer Problem Solution Micrometer Concepts How It Works Micrometer Configuration for the Client Micrometer Configuration for the Server Expose Additional Metrics Through MeterBinder 10-12. Summary Chapter 11: Spring Messaging 11-1. Send and Receive JMS Messages with Spring Problem Solution How It Works Send and Receive Messages Without Spring’s JMS Template Support Send and Receive Messages with Spring’s JMS Template Send and Receive Messages to and from a Default Destination Extend the JmsGatewaySupport Class 11-2. Convert JMS Messages Problem Solution How It Works 11-3. Manage JMS Transactions Problem Solution How It Works 11-4. Create Message-Driven POJOs in Spring Problem Solution How It Works Listen for JMS Messages with Message Listeners Listen for JMS Messages with POJOs Convert JMS Messages Manage JMS Transactions 11-5. Cache and Pool JMS Connections Problem Solution How It Works 11-6. Send and Receive AMQP Messages with Spring Problem Solution How It Works Send and Receive Messages Without Spring’s Template Support Send Messages with Spring’s Template Support Listen for AMQP Messages with Message Listeners 11-7. Send and Receive Messages with Spring Kafka Problem Solution How It Works Send Messages with Spring’s Template Support Listening to Messages Using Spring Kafka Using a MessageConverter to Convert Payloads into Objects Converting Objects to Payloads 11-8. Summary Chapter 12: Spring Integration 12-1. Integrating One System with Another Using EAI Problem Solution How It Works Picking an Integration Style Building on an ESB Solution 12-2. Integrating Two Systems Using JMS Problem Solution How It Works Building a Message-Driven POJO (MDP) Using Spring Integration 12-3. Interrogating Spring Integration Messages for Context Information Problem Solution How It Works Using MessageHeaders for Fun and Profit 12-4. Integrating Two Systems Using a File System Problem Solution How It Works Concerns in Dealing with a File System 12-5. Transforming a Message from One Type to Another Problem Solution How It Works Modifying a Message’s Payload Modifying a Message’s Headers 12-6. Error Handling Using Spring Integration Problem Solution How It Works Custom Handler to Handle Exceptions Routing to Custom Handlers Based on the Type of Exception Building a Solution with Multiple Error Channels 12-7. Forking Integration Control: Splitters and Aggregators Problem Solution How It Works Using a Splitter Using Aggregators 12-8. Conditional Routing with Routers Problem Solution How It Works 12-9. Staging Events Using Spring Batch Problem Solution How It Works 12-10. Using Gateways Problem Solution How It Works MessagingGatewaySupport Breaking the Interface Dependency 12-11. Summary Chapter 13: Spring Testing 13-1. Creating Tests with JUnit and TestNG Problem Solution How It Works Testing with JUnit Data-Driven Testing with JUnit Testing with TestNG Data-Driven Testing with TestNG 13-2. Creating Unit Tests and Integration Tests Problem Solution How It Works Creating Unit Tests for Isolated Classes Creating Unit Tests for Dependent Classes Using Stubs and Mock Objects Creating Integration Tests 13-3. Managing Application Contexts in Integration Tests Problem Solution How It Works Accessing the ApplicationContext with the TestContext Framework in JUnit Accessing the ApplicationContext with the TestContext Framework in TestNG 13-4. Injecting Test Fixtures into Integration Tests Problem Solution How It Works Injecting Test Fixtures with the TestContext Framework in JUnit Injecting Test Fixtures with the TestContext Framework in TestNG 13-5. Managing Transactions in Integration Tests Problem Solution How It Works Managing Transactions with the TestContext Framework in JUnit Managing Transactions with the TestContext Framework in TestNG 13-6. Integration Testing Spring MVC Controllers Problem Solution How It Works Integration Testing Spring MVC Controllers with JUnit Integration Testing Spring MVC Controllers with TestNG 13-7. Integration Testing REST Clients Problem Solution How It Works 13-8. Integration Testing with Testcontainers Problem Solution How It Works Using Testcontainers with JUnit Jupiter Using Testcontainers with TestNG 13-9. Summary Chapter 14: Caching 14-1. Caching with Caffeine Problem Solution How It Works Using Caffeine Without Spring Using Caffeine with Spring for Configuration 14-2. Caching with Spring’s Cache Abstraction Problem Solution How It Works Using Caffeine with Spring’s Cache Abstraction 14-3. Declarative Caching with AOP Problem Solution How It Works Using Spring AOP Using AspectJ 14-4. Configure a Custom KeyGenerator Problem Solution How It Works 14-5. Adding and Removing Objects to and from the Cache Problem Solution How It Works Using @CacheEvict to Remove Invalid Objects Using @CachePut to Place Objects in the Cache Vetoing Results for the @Cacheable Annotation 14-6. Synchronize Caching with a Transactional Resource Problem Solution How It Works 14-7. Using Redis as a Cache Provider Problem Solution How It Works Configure the RedisCacheManager 14-8. Summary Index 978-1-4842-8649-4.png 0.PNG 0.PNG 0.PNG 0.PNG 0.PNG
Donate to keep this site alive
How to download source code?
1. Go to: https://github.com/Apress
2. In the Find a repository… box, search the book title: Spring 6 Recipes: A Problem-Solution Approach to Spring Framework, 5th Edition
, sometime you may not get the results, please search the main title.
3. Click the book title in the search results.
3. Click Code to download.
1. Disable the AdBlock plugin. Otherwise, you may not get any links.
2. Solve the CAPTCHA.
3. Click download link.
4. Lead to download server to download.