CONTENTS 6.2 Choosing a transaction manager 150 JDBC transactions 151.Hibernate transactions 151.Java Persistence API transactions 152.Java transaction API transactions 153 6.3 Programming transactions in Spring 153 6.4 Declaring transactions 155 Defining transaction attributes 156 Declaring transactions in XML.160.Defining annotation-driven transactions 162 6.5 Summary 163 Building web applications with Spring MVC 164 7.1 Getting started with Spring MVC 165 Following a request through Spring MVC 165 Setting up Spring MVC 167 7.2 Writing a basic controller 169 Configuring an annotation-driven Spring MVC 170.Defining the home page controller 170.Resolving views 173.Defining the home page view 177 Rounding out the Spring application context 179 7.3 Handling controller input 181 Writing a controller that processes input 181.Rendering the view 183 7.4 Processing forms 185 Displaying the registration form 185.Processing form input 187.Validating input 189 7.5 Handling file uploads 193 7.6 Summary 197 Working with Spring Web Flow 199 8.1 Installing Spring Web Flow 200 Configuring Web Flow in Spring 200 8.2 The components of a flow 203 States 203.Transitions 206.Flow data 207 8.3 Putting it all together:the pizza flow 209 Defining the base flow 209"Collecting customer information 213.Building an order 218.Taking payment 221
x CONTENTS 6.2 Choosing a transaction manager 150 JDBC transactions 151 ■ Hibernate transactions 151 ■ Java Persistence API transactions 152 ■ Java transaction API transactions 153 6.3 Programming transactions in Spring 153 6.4 Declaring transactions 155 Defining transaction attributes 156 ■ Declaring transactions in XML 160 ■ Defining annotation-driven transactions 162 6.5 Summary 163 7 Building web applications with Spring MVC 164 7.1 Getting started with Spring MVC 165 Following a request through Spring MVC 165 ■ Setting up Spring MVC 167 7.2 Writing a basic controller 169 Configuring an annotation-driven Spring MVC 170 ■ Defining the home page controller 170 ■ Resolving views 173 ■ Defining the home page view 177 ■ Rounding out the Spring application context 179 7.3 Handling controller input 181 Writing a controller that processes input 181 ■ Rendering the view 183 7.4 Processing forms 185 Displaying the registration form 185 ■ Processing form input 187 ■ Validating input 189 7.5 Handling file uploads 193 Adding a file upload field to the form 193 ■ Receiving uploaded files 194 ■ Configuring Spring for file uploads 197 7.6 Summary 197 8 Working with Spring Web Flow 199 8.1 Installing Spring Web Flow 200 Configuring Web Flow in Spring 200 8.2 The components of a flow 203 States 203 ■ Transitions 206 ■ Flow data 207 8.3 Putting it all together: the pizza flow 209 Defining the base flow 209 ■ Collecting customer information 213 ■ Building an order 218 ■ Taking payment 221 Licensed to Christian Cederquist <chrisman@kaus.dk>
CONTENTS 8.4 Securing web flows 222 8.5 Summary 223 ○Securing Spring224 9.1 Introducing Spring Security 225 Getting started with Spring Security 226.Using the Spring Security configuration namespace 226 9.2 Securing web requests 227 secunty 9.3 Securing view-level elements 235 Accessing authentication details 235 Rendering with authorities 236 9.4 Authenticating users 238 Configuring an in-memory user repository 239 Authenticating against a database 240.Authenticating against IDAP 241 Enabling remember-me functionality 245 9.5 Securing methods 246 SpEL.247 Declaring method-level security pointcuts 252 9.6 Summary 252 PART 3 INTEGRATING SPRING..253 Working with remote services 255 10.1 An overview of Spring remoting 256 10.2 Working with RMI 258 10.3 Exposing remote services with Hessian and Burlap 263 Exposing bean functionality with Hessian/Burlap 264 Accessing Hessian/Burlap services 266 10.4 Using Spring's HttpInvoker 268 Exposing beans as HTTP services 268.Accessing services via HTTP 269
CONTENTS xi 8.4 Securing web flows 222 8.5 Summary 223 9 Securing Spring 224 9.1 Introducing Spring Security 225 Getting started with Spring Security 226 ■ Using the Spring Security configuration namespace 226 9.2 Securing web requests 227 Proxying servlet filters 228 ■ Configuring minimal web security 228 ■ Intercepting requests 232 9.3 Securing view-level elements 235 Accessing authentication details 235 ■ Rendering with authorities 236 9.4 Authenticating users 238 Configuring an in-memory user repository 239 Authenticating against a database 240 ■ Authenticating against LDAP 241 ■ Enabling remember-me functionality 245 9.5 Securing methods 246 Securing methods with @Secured 246 ■ Using JSR-250’s @RolesAllowed 247 ■ Pre-/Post-invocation security with SpEL 247 ■ Declaring method-level security pointcuts 252 9.6 Summary 252 PART 3INTEGRATING SPRING....................................253 10 Working with remote services 255 10.1 An overview of Spring remoting 256 10.2 Working with RMI 258 Exporting an RMI service 259 ■ Wiring an RMI service 261 10.3 Exposing remote services with Hessian and Burlap 263 Exposing bean functionality with Hessian/Burlap 264 Accessing Hessian/Burlap services 266 10.4 Using Spring’s HttpInvoker 268 Exposing beans as HTTP services 268 ■ Accessing services via HTTP 269 Licensed to Christian Cederquist <chrisman@kaus.dk>
xii CONTENTS 10.5 Publishing and consuming web services 270 Creating Spring-enabled JAX-WS endpoints 271 Proxying JAX-WS services on the client side 274 10.6 Summary 276 Giving Spring some REST 277 11.1 Getting REST 278 The fundamentals of REST 278.How Spring supports REST 279 11.2 Writing resource-oriented controllers 279 Dissecting a RESTless controller 280.Handling RESTful URLs 281.Performing the REST verbs 284 11.3 Representing resources 287 Negotiating resource representation 288.Working with HTTP message converters 291 11.4 Writing REST clients 294 Exploring RestTemplate's operations 295CE resources 206 299.DELETE-ing resources 30l POSTing resource data30l·Exchanging resources 304 11.5 Submitting RESTful forms 306 Rendering hidden method fields in Sp 306.Unmasking the real request 307 11.6 Summary 309 Messaging in Spring 310 12.1 A briefintroduction to JMS 311 Architecting IMS 312.Assessing the benefits of [MS 314 12.2 Setting up a message broker in Spring 316 Creating a connection factory 316.Declaring an ActiveMQ message destination 317 12.3 Using Spring's JMS template 318 Tackling runaway [MS code 318.Working with [MS templates 319 12.4 Creating message-driven POJOs 325 Creating a message listener 326.Configuring message listeners 327
xii CONTENTS 10.5 Publishing and consuming web services 270 Creating Spring-enabled JAX-WS endpoints 271 Proxying JAX-WS services on the client side 274 10.6 Summary 276 11 Giving Spring some REST 277 11.1 Getting REST 278 The fundamentals of REST 278 ■ How Spring supports REST 279 11.2 Writing resource-oriented controllers 279 Dissecting a RESTless controller 280 ■ Handling RESTful URLs 281 ■ Performing the REST verbs 284 11.3 Representing resources 287 Negotiating resource representation 288 ■ Working with HTTP message converters 291 11.4 Writing REST clients 294 Exploring RestTemplate’s operations 295 ■ GETting resources 296 ■ PUTting resources 299 ■ DELETE-ing resources 301 ■ POSTing resource data 301 ■ Exchanging resources 304 11.5 Submitting RESTful forms 306 Rendering hidden method fields in JSP 306 ■ Unmasking the real request 307 11.6 Summary 309 12 Messaging in Spring 310 12.1 A brief introduction to JMS 311 Architecting JMS 312 ■ Assessing the benefits of JMS 314 12.2 Setting up a message broker in Spring 316 Creating a connection factory 316 ■ Declaring an ActiveMQ message destination 317 12.3 Using Spring’s JMS template 318 Tackling runaway JMS code 318 ■ Working with JMS templates 319 12.4 Creating message-driven POJOs 325 Creating a message listener 326 ■ Configuring message listeners 327 Licensed to Christian Cederquist <chrisman@kaus.dk>
CONTENTS xi进 12.5 Using message-based RPC 327 Working with Spring message-based RPC 328.Asynchronous RPC with Lingo 330 12.6 Summary 332 Managing Spring beans with IMX 333 13.1 Exporting Spring beans as MBeans 334 Exposing methods by name 337.Using interfaces to define operations and with annolation-driven MBeans 340 Handing MBca collisions 342 13.2 Remoting MBeans 343 Exposing remote MBeans 343.Accessing remote MBeans 344 Proxying MBeans 345 13.3 Handling notifications 346 Listening for notifications 348 13.4 Summary 349 Odds and ends 350 14.1 Externalizing configuration 351 m 14.2 Wiring JNDI objects 357 Working with comentional JNDI357Injecting NDI objects 359.Wiring E[Bs in Spring 362 14.3 Sending email 363 Configuring a mail sender 363.Constructing the email 365 14.4 Scheduling and background tasks 370 Declaring scheduled methods 371.Declaring asynchronous methods 373 14.5 Summary 374 14.6 The end.?374 index 377
CONTENTS xiii 12.5 Using message-based RPC 327 Working with Spring message-based RPC 328 ■ Asynchronous RPC with Lingo 330 12.6 Summary 332 13 Managing Spring beans with JMX 333 13.1 Exporting Spring beans as MBeans 334 Exposing methods by name 337 ■ Using interfaces to define MBean operations and attributes 339 ■ Working with annotation-driven MBeans 340 ■ Handing MBean collisions 342 13.2 Remoting MBeans 343 Exposing remote MBeans 343 ■ Accessing remote MBeans 344 Proxying MBeans 345 13.3 Handling notifications 346 Listening for notifications 348 13.4 Summary 349 14 Odds and ends 350 14.1 Externalizing configuration 351 Replacing property placeholders 352 ■ Overriding properties 354 ■ Encrypting external properties 355 14.2 Wiring JNDI objects 357 Working with conventional JNDI 357 ■ Injecting JNDI objects 359 ■ Wiring EJBs in Spring 362 14.3 Sending email 363 Configuring a mail sender 363 ■ Constructing the email 365 14.4 Scheduling and background tasks 370 Declaring scheduled methods 371 ■ Declaring asynchronous methods 373 14.5 Summary 374 14.6 The end...? 374 index 377 Licensed to Christian Cederquist <chrisman@kaus.dk>
preface Wow!As I write this,it's been almost seven years since Spring 1.0 was released and Ryan Breidenbach and I started work on the first edition of Spring in Action.Back then,who would have guessed that Spring would transform Java development as much as it has? In that first edition,Ryan and I tried to cover every corner of the Spring Frame- work.For the most part,we were successful.Back then the entire Spring story could easily be told in 11 chapters with dependency injection,AOP,persistence,transactions, Spring MVC,and Acegi Security as the main characters.Of course,back then that story had to be told with a lot of XML.(Does anybody remember what it was like declaring transactions with TransactionProxyFactoryBean?) By the time I got around to writing the second edition,Spring had grown quite a bit.Again,I tried to squeeze everything I could into a single book.I found out it wasn't possible.Spring had expanded well beyond what could be discussed in a 700-to 800-page book.In fact,entire,completely written chapters were cut out of the second edition because there wasn't room. More than three years and two major versions of Spring have passed since the sec- ond edition was printed.Spring covers more ground than ever before and it would take several volumes to comprehensively cover the entire Spring portfolio.It's not possible to cram everything there is to know about Spring into a single book. So I'm not going to even try. Often books get thicker with each successive edition.But you've probably noticed by now that this third edition of Spring in Action has fewer pages than the second edi- tion.That's possible for a couple of reasons
xv preface Wow! As I write this, it’s been almost seven years since Spring 1.0 was released and Ryan Breidenbach and I started work on the first edition of Spring in Action. Back then, who would have guessed that Spring would transform Java development as much as it has? In that first edition, Ryan and I tried to cover every corner of the Spring Framework. For the most part, we were successful. Back then the entire Spring story could easily be told in 11 chapters with dependency injection, AOP, persistence, transactions, Spring MVC, and Acegi Security as the main characters. Of course, back then that story had to be told with a lot of XML. (Does anybody remember what it was like declaring transactions with TransactionProxyFactoryBean?) By the time I got around to writing the second edition, Spring had grown quite a bit. Again, I tried to squeeze everything I could into a single book. I found out it wasn’t possible. Spring had expanded well beyond what could be discussed in a 700- to 800-page book. In fact, entire, completely written chapters were cut out of the second edition because there wasn’t room. More than three years and two major versions of Spring have passed since the second edition was printed. Spring covers more ground than ever before and it would take several volumes to comprehensively cover the entire Spring portfolio. It’s not possible to cram everything there is to know about Spring into a single book. So I’m not going to even try. Often books get thicker with each successive edition. But you’ve probably noticed by now that this third edition of Spring in Action has fewer pages than the second edition. That’s possible for a couple of reasons. Licensed to Christian Cederquist <chrisman@kaus.dk>