Topic: Java

Beginning Spring (1118892925) cover image

Beginning Spring

Mert Caliskan, Kenan Sevindik, Rod Johnson (Foreword by), Jürgen Höller (Foreword by)
ISBN: 978-1-118-89292-3
Paperback
480 pages
February 2015
Other Available Formats: Wiley E-Text
If you are an instructor, you may request an evaluation copy for this title.
Paperback Version: US $45.00 Add to Cart

About This Title  |  Download Code  |  Errata  |  P2P Forum for This Title
FOREWORDS xxvii

INTRODUCTION xxxi

CHAPTER 1: POJO PROGRAMMING MODEL, LIGHTWEIGHT CONTAINERS, AND INVERSION OF CONTROL 1

POJO Programming Model 2

Problems of the Old EJB Programming Model 2

Benefits of the POJO Programming Model 7

Lightweight Containers and Inversion of Control (IoC) 8

Lightweight Containers 8

Inversion of Control (IoC) 9

Dependency Injection 10

Setter Injection 11

Constructor Injection 11

Setter or Constructor Injection 12

Summary 12

CHAPTER 2: DEPENDENCY INJECTION WITH SPRING 17

Spring IoC Container 18

Configuration Metadata 18

Configuring and Using the Container 21

Dependency Injection 29

Setter Injection 30

Constructor Injection 31

Circular Dependencies 34

Dependency Resolution Process 35

Overriding Bean Definitions 36

Using the depends ]on Attribute 38

Autowiring 39

Bean Lookups 43

Spring ]Managed Beans 44

Naming Beans 44

Bean Instantiation Methods 45

Bean Scopes 48

Lazy Initialization 51

Life-Cycle Callbacks 52

Bean Definition Profiles 54

Environment 56

Summary 59

CHAPTER 3: BUILDING WEB APPLICATIONS USING SPRING MVC 63

Learning the Features and Benefits of Spring MVC 64

Using the Dispatcher Servlet Mechanism 65

Defining the Servlet 66

Accessing Servlet Context 67

Creating Your First Spring MVC Application 68

Configuring Spring MVC with Annotations 71

Handling Forms with JSP 73

Configuring the Form Tag Library 73

Understanding the Power of Binding 74

Working with Forms 74

Using Input Elements 75

Entering Dates 76

Selecting from a Drop ]Down 77

Selecting with Radio Buttons 78

Selecting with Checkboxes 78

Adding Labels 78

Placing Buttons 79

Styling 79

Exploiting the Power of Annotations 84

@Controller 84

@RequestMapping 84

@ModelAttribute 84

@PathVariable 85

@ControllerAdvice 85

@InitBinder 85

@ExceptionHandler 85

Validating User Input 86

Uploading Files 90

Handling Exceptions 93

Implementing Internationalization (i18n) 95

Using Themes 97

Summary 100

CHAPTER 4: JDBC DATA ACCESS WITH SPRING 103

Problems with Using Vanilla JDBC 104

Introducing Spring’s JDBC Support 105

Managing JDBC Connections 105

Embedded DB Support 108

Using a Connection ]Pooled DataSource 110

Initializing DB 111

Configuring and Using Spring’s JDBC Support 112

Performing Data Access Operations with Spring 114

Running Queries 114

Queries with Named Parameters 117

Writing Queries Using the IN Clause 118

Using PreparedStatements within JdbcTemplate 119

Inserting, Updating, and Deleting Records 121

Calling Stored Procedures and Stored Functions 124

Performing Batch Operations 126

Handling BLOB and CLOB Objects 126

Accessing Vendor ]Specific JDBC Methods 127

Executing DDL Operations 127

Modeling JDBC Operations as Java Objects 128

Encapsulating SQL Query Executions 128

Encapsulating SQL DML Operations 130

Encapsulating Stored Procedure Executions 131

Exception Handling and Error Code Translation 132

Common Data Access Exception Hierarchy 132

Automatic Handling and Translation of SQLException 132

Summary 133

CHAPTER 5: DATA ACCESS WITH JPA USING SPRING 137

Brief Introduction to ORM and JPA 138

Paradigm Mismatch 138

Building Blocks of an ORM Framework 139

What JPA Offers 139

Mapping the Object Model to the Relational Model 140

Defining Entities 140

Mapping Attributes to Columns 141

Creating Associations between Objects 142

Mapping Java Types to SQL Types 145

Configuring and Using JPA 147

Performing CRUD Operations on Objects 150

Querying with Object Query Language 155

Spring’s JPA Support 156

Setting Up JPA in Spring Container 156

Implementing DAOs Based on Plain JPA 161

Handling and Translating Exceptions 166

Further JPA Configuration in Spring Environment 167

JpaDialect 168

JpaVendorAdapter 168

JPA and Load Time Weaving 169

Dealing with Multiple Persistence Units 170

Summary 171

CHAPTER 6: MANAGING TRANSACTIONS WITH SPRING 175

Understanding Transaction Management 176

Spring’s Transaction Abstraction Model 180

Local versus Global Transactions 182

PlatformTransactionManager Implementations 182

Advantages of Spring’s Abstract Transaction Model 183

Declarative Transaction Management with Spring 183

Isolating the Service Layer from Data Access Technology Details 186

Customizing Default Transactional Behavior 189

Using @Transactional on the Class Level 190

Understanding Transaction Propagation Rules 191

Propagation REQUIRED 191

Propagation REQUIRES_NEW 192

Propagation NESTED 192

Propagation SUPPORTS 192

Propagation NOT_SUPPORTED 192

Propagation NEVER 193

Propagation MANDATORY 193

Using for Declarative Transaction Management 195

Programmatic Transaction Management with Spring 197

Using the PlatformTransactionManager Approach 201

Executing Custom Logic Before or After Transactions 203

Advising Transactional Operations 203

Executing Logic after Transactions Using TransactionSynchronization 204

Summary 205

CHAPTER 7: TEST ]DRIVEN DEVELOPMENT WITH SPRING 209

Configuring and Caching ApplicationContext 210

Using XML ] and Java ]Based Context Configuration in Tests 210

Confi guring Context with ApplicationContextInitializer 214

Inheriting Context Configuration 214

ApplicationContext Caching 216

Injecting Dependencies of Test Fixtures 217

Using Transaction Management in Tests 219

Testing Web Applications 222

Context Hierarchies in Tests 225

Testing Request ] and Session ]Scoped Beans 225

Testing Spring MVC Projects 227

Testing Controllers 227

Testing Form Submit 228

Testing Exception Handlers 230

Printing Mock Request and Response 231

Using Mock Objects and Other Utilities for Testing 231

Spring Provided Mock Objects for Testing 231

Other Utilities and Test Annotations 232

Summary 233

CHAPTER 8: ASPECT ]ORIENTED PROGRAMMING WITH SPRING 237

Getting Started with AOP with Spring 239

Becoming Familiar with Types of Advices 243

Before 245

After Returning 245

After Throwing 245

After (Finally) 246

Around 247

Defining Point ]Cut Designators 248

The Type Signature Expressions 248

The Method Signature Expressions 249

Other Alternative Point ]Cut Designators 249

Wildcards 250

Capitalizing on the Power of Annotations 250

@Before 250

@Pointcut 251

@After 252

@AfterReturning 252

@AfterThrowing 252

@Aspect 253

@Around 253

@DeclareParents 254

Blending AspectJ with Spring 255

Configuring Spring AOP with Annotations 259

Summary 259

CHAPTER 9: SPRING EXPRESSION LANGUAGE 263

Configuring Applications with SpEL 264

Creating a Parser 267

Invoking Methods 270

Calling Constructors 272

Calling Static Methods 272

Working with Variables and Functions 273

#root 273

#this 274

Accessing System Properties and Environment 274

Inline Lists 274

Registering Functions 274

Understanding SpEL Operators 275

Relational Operators 276

Arithmetic Operators 276

Logical Operators 276

Conditional Operators 277

Regular Expression Operator 278

Safe Navigation Operator 278

Collection Selection and Projection 279

Selecting the First and Last Element of a Collection 280

Using Utilities in SpEL 280

Accessing Spring Beans 280

281

Expressions in Caching 281

Summary 281

CHAPTER 10: CACHING 285

Building Your First Caching Application 286

Configuring the Cache Manager with a Different Name 289

Configuring the Caching Abstraction with Annotations 289

Working with Cache Annotations 290

@Cacheable 290

Key Generator 291

Conditional Caching 291

@CacheEvict 292

@CachePut 292

@Caching 293

Implementing Cache Managers 293

SimpleCacheManager 293

NoOpCacheManager 294

ConcurrentMapCacheManager 294

CompositeCacheManager 294

Casting Your SpEL on Caches 295

Initializing Your Caches Programmatically 296

Finding Alternative Cache Providers 298

Ehcache 299

Guava 302

Hazelcast 302

Summary 303

CHAPTER 11: RESTFUL WEB SERVICES WITH SPRING 305

Creating Your First REST Web Service 306

Returning Different HTTP Status Codes from REST Web Service 318

Learning an Annotation ]Based Configuration Alternative 318

Using REST Web Services with XML 320

Using the Exception Handling Mechanism 322

Unit Testing RESTful Services 326

Summary 328

CHAPTER 12: SECURING WEB APPLICATIONS WITH SPRING SECURITY 331

Why Spring Security? 332

Features of Spring Security 333

Configuring and Using Spring Security 334

Understanding the Fundamental Building Blocks of Spring Security 340

Authenticating Users 341

Unsuccessful Login Flow 342

Successful Login Flow 342

Anonymous Authentication 344

Customizing the Login Page 344

Logout Process 346

Accessing UserDetails Using JDBC 346

Encrypting Passwords 349

Remember ]Me Support 350

User Session Management 351

Basic Authentication 352

Authorizing Web Requests and Service Method Calls 353

Authorizing Web Requests 353

How Does Authorization Work? 355

Expression ]Based Authorization 357

Using JSP Security Tags 358

Authorize Tag 359

Authenticate Tag 359

Authorizing Service Methods 359

Summary 364

CHAPTER 13: NEXT STOP: SPRING 4.0 369

Keeping Up with the Latest: Java 8 and Java EE7 Support 370

Lambda Expressions 370

Method References 373

Bean Validation Integration 374

JSR 310: Date Time Value Type Support 374

Configuring Injection with Conditional Dependency 374

Ordering the Elements of Autowired Collections 377

Repeating Annotations 379

Introducing New Annotations 381

Documenting with @Description 381

Using the @RestController Annotation 382

Summary 382

APPENDIX: SOLUTIONS TO EXERCISES 385

INDEX 425