ebook img

Spring in action : [covers Sping 2.0]/ von Craig Walls PDF

736 Pages·2008·6.387 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Spring in action : [covers Sping 2.0]/ von Craig Walls

Spring in Action Second Edition CRAIG WALLS with Ryan Breidenbach MANNING Greenwich (74° w. long.) For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. Sound View Court 3B Fax: (609) 877-8256 Greenwick, CT 06830 Email: [email protected] ©2008 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Manning Publications Co. Copyeditor: Liz Welch Sound View Court 3B Typesetter: Dottie Marsico Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN 1-933988-13-4 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 13 12 11 10 09 08 07 PART 1 CORE SPRING................................................ 1 1 Springing into action 3 1.1 What is Spring? 5 Spring modules 6 1.2 A Spring jump start 11 1.3 Understanding dependency injection 14 Injecting dependencies 14 ■ Dependency injection in action 15 Dependency injection in enterprise applications 21 1.4 Applying aspect-oriented programming 24 Introducing AOP 24 ■ AOP in action 26 1.5 Summary 30 2 Basic bean wiring 31 2.1 Containing your beans 33 Introducing the BeanFactory 34 ■ Working with an application context 35 ■ A bean’s life 37 2.2 Creating beans 40 Declaring a simple bean 40 ■ Injecting through constructors 42 2.3 Injecting into bean properties 46 Injecting simple values 47 ■ Referencing other beans 48 Wiring collections 52 ■ Wiring nothing (null) 58 2.4 Autowiring 58 The four types of autowiring 59 ■ Mixing auto with explicit wiring 63 ■ To autowire or not to autowire 63 2.5 Controlling bean creation 64 Bean scoping 65 ■ Creating beans from factory methods 66 Initializing and destroying beans 68 2.6 Summary 71 3 Advanced bean wiring 72 3.1 Declaring parent and child beans 73 Abstracting a base bean type 74 ■ Abstracting common properties 76 3.2 Applying method injection 79 Basic method replacement 80 ■ Using getter injection 83 3.3 Injecting non-Spring beans 85 3.4 Registering custom property editors 88 3.5 Working with Spring’s special beans 92 Postprocessing beans 93 ■ Postprocessing the bean factory 95 Externalizing configuration properties 96 ■ Resolving text messages 99 ■ Decoupling with application events 101 Making beans aware 103 3.6 Scripting beans 106 Putting the lime in the coconut 107 ■ Scripting a bean 108 Injecting properties of scripted beans 111 ■ Refreshing scripted beans 112 ■ Writing scripted beans inline 113 3.7 Summary 114 4 Advising beans 116 4.1 Introducing AOP 118 Defining AOP terminology 119 ■ Spring’s AOP support 122 4.2 Creating classic Spring aspects 125 Creating advice 127 ■ Defining pointcuts and advisors 132 Using ProxyFactoryBean 136 4.3 Autoproxying 139 Creating autoproxies for Spring aspects 140 ■ Autoproxying @AspectJ aspects 141 4.4 Declaring pure-POJO aspects 145 4.5 Injecting AspectJ aspects 149 4.6 Summary 152 PART 2 ENTERPRISE SPRING ................................. 153 5 Hitting the database 155 5.1 Learning Spring’s data access philosophy 157 Getting to know Spring’s data access exception hierarchy 158 Templating data access 161 ■ Using DAO support classes 163 5.2 Configuring a data source 165 Using JNDI data sources 165 ■ Using a pooled data source 167 JDBC driver-based data source 168 5.3 Using JDBC with Spring 170 Tackling runaway JDBC code 170 ■ Working with JDBC templates 173 ■ Using Spring’s DAO support classes for JDBC 180 5.4 Integrating Hibernate with Spring 183 Choosing a version of Hibernate 185 ■ Using Hibernate templates 186 ■ Building Hibernate-backed DAOs 190 Using Hibernate 3 contextual sessions 192 5.5 Spring and the Java Persistence API 194 Using JPA templates 194 ■ Configuring an entity manager factory 197 ■ Building a JPA-backed DAO 202 5.6 Spring and iBATIS 203 Configuring an iBATIS client template 204 ■ Building an iBATIS-backed DAO 207 5.7 Caching 208 Configuring a caching solution 210 ■ Proxying beans for caching 215 ■ Annotation-driven caching 217 5.8 Summary 218 6 Managing transactions 220 6.1 Understanding transactions 222 Explaining transactions in only four words 223 Understanding Spring’s transaction management support 224 6.2 Choosing a transaction manager 225 JDBC transactions 226 ■ Hibernate transactions 227 Java Persistence API transactions 227 ■ Java Data Objects transactions 228 ■ Java Transaction API transactions 229 6.3 Programming transactions in Spring 229 6.4 Declaring transactions 232 Defining transaction attributes 233 ■ Proxying transactions 238 ■ Declaring transactions in Spring 2.0 241 ■ Defining annotation-driven transactions 243 6.5 Summary 245 7 Securing Spring 247 7.1 Introducing Spring Security 248 7.2 Authenticating users 252 Configuring a provider manager 253 ■ Authenticating against a database 256 ■ Authenticating against an LDAP repository 264 7.3 Controlling access 271 Voting access decisions 272 ■ Casting an access decision vote 273 ■ Handling voter abstinence 275 7.4 Securing web applications 275 Proxying Spring Security’s filters 278 ■ Handling the security context 285 ■ Prompting the user to log in 286 ■ Handling security exceptions 291 ■ Enforcing web security 293 ■ Ensuring a secure channel 294 7.5 View-layer security 297 Conditionally rendering content 298 ■ Displaying user authentication information 299 7.6 Securing method invocations 300 Creating a security aspect 301 ■ Securing methods using metadata 303 7.7 Summary 304 8 Spring and POJO-based remote services 305 8.1 An overview of Spring remoting 306 8.2 Working with RMI 309 Wiring RMI services 310 ■ Exporting RMI services 312 8.3 Remoting with Hessian and Burlap 316 Accessing Hessian/Burlap services 317 ■ Exposing bean functionality with Hessian/Burlap 318 8.4 Using Spring’s HttpInvoker 322 Accessing services via HTTP 323 ■ Exposing beans as HTTP Services 324 8.5 Spring and web services 326 Exporting beans as web services using XFire 326 Declaring web services with JSR-181 annotations 330 Consuming web services 333 ■ Proxying web services with an XFire client 340 8.6 Summary 341 9 Building contract-first web services in Spring 343 9.1 Introducing Spring-WS 345 9.2 Defining the contract (first!) 347 Creating sample XML messages 348 9.3 Handling messages with service endpoints 353 Building a JDOM-based message endpoint 355 ■ Marshaling message payloads 358 9.4 Wiring it all together 361 Spring-WS: The big picture 361 ■ Mapping messages to endpoints 363 ■ Wiring the service endpoint 364 Configuring a message marshaler 364 ■ Handling endpoint exceptions 367 ■ Serving WSDL files 369 ■ Deploying the service 373 9.5 Consuming Spring-WS web services 373 Working with web service templates 374 ■ Using web service gateway support 381 9.6 Summary 382 10 Spring messaging 384 10.1 A brief introduction to JMS 386 Architecting JMS 387 ■ Assessing the benefits of JMS 390 Setting up ActiveMQ in Spring 392 10.2 Using JMS with Spring 393 Tackling runaway JMS code 393 ■ Working with JMS templates 395 ■ Converting messages 402 ■ Using Spring’s gateway support classes for JMS 405 10.3 Creating message-driven POJOs 407 Creating a message listener 408 ■ Writing pure-POJO MDPs 412 10.4 Using message-based RPC 416 Introducing Lingo 417 ■ Exporting the service 418 Proxying JMS 420 10.5 Summary 422 11 Spring and Enterprise JavaBeans 423 11.1 Wiring EJBs in Spring 425 Proxying session beans (EJB 2.x) 426 ■ Wiring EJBs into Spring beans 430 11.2 Developing Spring-enabled EJBs (EJB 2.x) 431 11.3 Spring and EJB3 434 Introducing Pitchfork 435 ■ Getting started with Pitchfork 436 Injecting resources by annotation 437 ■ Declaring interceptors using annotations 438 11.4 Summary 440 12 Accessing enterprise services 441 12.1 Wiring objects from JNDI 442 Working with conventional JNDI 443 ■ Injecting JNDI objects 446 ■ Wiring JNDI objects in Spring 2 449 12.2 Sending email 450 Configuring a mail sender 451 ■ Constructing the email 453 12.3 Scheduling tasks 456 Scheduling with Java’s Timer 457 ■ Using the Quartz scheduler 460 ■ Invoking methods on a schedule 464 12.4 Managing Spring beans with JMX 466 Exporting Spring beans as MBeans 467 ■ Remoting MBeans 477 ■ Handling notifications 482 12.5 Summary 485 PART 3 CLIENT-SIDE SPRING.................................. 487 13 Handling web requests 489 13.1 Getting started with Spring MVC 490 A day in the life of a request 491 ■ Configuring DispatcherServlet 492 ■ Spring MVC in a nutshell 495 13.2 Mapping requests to controllers 502 Using SimpleUrlHandlerMapping 503 ■ Using ControllerClassNameHandlerMapping 504 ■ Using metadata to map controllers 505 ■ Working with multiple handler mappings 505 13.3 Handling requests with controllers 506 Processing commands 509 ■ Processing form submissions 512 Processing complex forms with wizards 520 ■ Working with throwaway controllers 528 13.4 Handling exceptions 531 13.5 Summary 532 14 Rendering web views 533 14.1 Resolving views 534 Using template views 535 ■ Resolving view beans 537 Choosing a view resolver 540 14.2 Using JSP templates 542 Binding form data 542 ■ Rendering externalized messages 544 Displaying errors 547 14.3 Laying out pages with Tiles 549 Tile views 550 ■ Creating Tile controllers 554 14.4 Working with JSP alternatives 556 Using Velocity templates 557 ■ Working with FreeMarker 564 14.5 Generating non-HTML output 569 Producing Excel spreadsheets 570 ■ Generating PDF documents 573 ■ Developing custom views 576 14.6 Summary 578 15 Using Spring Web Flow 580 15.1 Getting started with Spring Web Flow 582 Installing Spring Web Flow 584 ■ Spring Web Flow essentials 589 ■ Creating a flow 591 15.2 Laying the flow groundwork 591 Flow variables 591 ■ Start and end states 593 ■ Gathering customer information 594 ■ Building a pizza order 601 Completing the order 605 ■ A few finishing touches 608 15.3 Advanced web flow techniques 611 Using decision states 612 ■ Extracting subflows and using substates 614 15.4 Integrating Spring Web Flow with other frameworks 619 Jakarta Struts 619 ■ JavaServer Faces 620 15.5 Summary 622

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.