Dependency injection: design patterns using spring and guice
β Scribed by Dhanji R. Prasanna
- Publisher
- Manning; Pearson Education [distributor]
- Year
- 2009
- Tongue
- English
- Leaves
- 354
- Edition
- 1
- Category
- Library
No coin nor oath required. For personal study only.
β¦ Synopsis
In object-oriented programming, a central program normally controls other objects in a module, library, or framework. With dependency injection, this pattern is invertedβa reference to a service is placed directly into the object which eases testing and modularity. Spring or Google Guice use dependency injection so you can focus on your core application and let the framework handle infrastructural concerns.
Dependency Injection explores the DI idiom in fine detail, with numerous practical examples that show you the payoffs. Youll apply key techniques in Spring and Guice and learn important pitfalls, corner-cases, and design patterns. Readers need a working knowledge of Java but no prior experience with DI is assumed.
β¦ Table of Contents
Front cover......Page 1
brief contents......Page 8
contents......Page 10
preface......Page 16
acknowledgments......Page 18
Roadmap......Page 20
Author Online......Page 21
about the cover illustration......Page 23
Dependency injection: whatβs all the hype?......Page 24
1.1.1 Seeing objects as services......Page 25
1.2 Pre-DI solutions......Page 27
1.2.1 Construction by hand......Page 28
1.2.2 The Factory pattern......Page 30
1.2.3 The Service Locator pattern......Page 35
1.3.1 The Hollywood Principle......Page 36
1.3.2 Inversion of Control vs. dependency injection......Page 38
1.4.1 Java......Page 40
1.5 Summary......Page 42
Time for injection......Page 44
2.1 Bootstrapping the injector......Page 45
2.2 Constructing objects with dependency injection......Page 46
2.3 Metadata and injector configuration......Page 49
2.3.1 XML injection in Spring......Page 50
2.3.2 From XML to in-code configuration......Page 53
2.3.3 Injection in PicoContainer......Page 54
2.3.4 Revisiting Spring and autowiring......Page 57
2.4 Identifying dependencies for injection......Page 59
2.4.1 Identifying by string keys......Page 60
2.4.2 Limitations of string keys......Page 65
2.4.3 Identifying by type......Page 67
2.4.4 Limitations of identifying by type......Page 69
2.4.5 Combinatorial keys: a comprehensive solution......Page 70
2.5 Separating infrastructure and application logic......Page 74
2.6 Summary......Page 75
Investigating DI......Page 77
3.1.1 Constructor injection......Page 78
3.1.2 Setter injection......Page 79
3.1.3 Interface injection......Page 83
3.1.4 Method decoration (or AOP injection)......Page 85
3.2 Choosing an injection idiom......Page 88
3.2.1 Constructor vs. setter injection......Page 89
3.2.2 The constructor pyramid problem......Page 92
3.2.3 The circular reference problem......Page 94
3.2.4 The in-construction problem......Page 98
3.2.5 Constructor injection and object validity......Page 101
3.3.1 The reinjection problem......Page 104
3.3.2 Reinjection with the Provider pattern......Page 105
3.3.3 The contextual injection problem......Page 107
3.3.4 Contextual injection with the Assisted Injection pattern......Page 109
3.3.5 Flexible partial injection with the Builder pattern......Page 111
3.4 Injecting objects in sealed code......Page 115
3.4.1 Injecting with externalized metadata......Page 116
3.4.2 Using the Adapter pattern......Page 118
3.5 Summary......Page 119
Building modular applications......Page 122
4.1 Understanding the role of an object......Page 123
4.2 Separation of concerns (my pants are too tight!)......Page 124
4.2.1 Perils of tight coupling......Page 125
4.2.2 Refactoring impacts of tight coupling......Page 128
4.2.3 Programming to contract......Page 131
4.2.4 Loose coupling with dependency injection......Page 134
4.3 Testing components......Page 135
4.3.1 Out-of-container (unit) testing......Page 136
4.3.2 I really need my dependencies!......Page 137
4.3.3 More on mocking dependencies......Page 138
4.3.4 Integration testing......Page 139
4.4.1 Rebinding dependencies......Page 141
4.4.2 Mutability with the Adapter pattern......Page 142
4.5 Summary......Page 144
Scope: a fresh breath of state......Page 146
5.1 What is scope?......Page 147
5.2 The no scope (or default scope)......Page 148
5.3 The singleton scope......Page 151
5.3.1 Singletons in practice......Page 154
5.3.2 The singleton anti-pattern......Page 158
5.4 Domain-specific scopes: the web......Page 162
5.4.1 HTTP request scope......Page 164
5.4.2 HTTP session scope......Page 172
5.5 Summary......Page 177
More use cases in scoping......Page 179
6.1.1 A quick primer on transactions......Page 180
6.1.2 Creating a custom transaction scope......Page 181
6.1.3 A custom scope in Guice......Page 183
6.1.4 A custom scope in Spring......Page 187
6.2 Pitfalls and corner cases in scoping......Page 189
6.2.1 Singletons must be thread-safe......Page 190
6.2.2 Perils of scope-widening injection......Page 192
6.3 Leveraging the power of scopes......Page 203
6.3.2 Grid scope......Page 204
6.3.3 Transparent grid computing with DI......Page 206
6.4 Summary......Page 207
From birth to death: object lifecycle......Page 209
7.1.1 Object creation......Page 210
7.1.2 Object destruction (or finalization)......Page 212
7.2.1 Contrasting lifecycle scenarios: servlets vs. database connections......Page 214
7.2.2 The Destructor anti-pattern......Page 219
7.2.3 Using Javaβs Closeable interface......Page 220
7.3 A real-world lifecycle scenario: stateful EJBs......Page 221
7.4 Lifecycle and lazy instantiation......Page 224
7.5 Customizing lifecycle with postprocessing......Page 225
7.6 Customizing lifecycle with multicasting......Page 228
7.7 Summary......Page 230
Managing an objectβs behavior......Page 233
8.1 Intercepting methods and AOP......Page 234
8.1.1 A tracing interceptor with Guice......Page 235
8.1.2 A tracing interceptor with Spring......Page 237
8.1.3 How proxying works......Page 239
8.1.4 Too much advice can be dangerous!......Page 242
8.2 Enterprise use cases for interception......Page 244
8.2.1 Transactional methods with warp-persist......Page 245
8.2.2 Securing methods with Spring Security......Page 247
8.3.1 Sameness tests are unreliable......Page 251
8.3.2 Static methods cannot be intercepted......Page 253
8.3.3 Neither can private methods......Page 254
8.3.4 And certainly not final methods!......Page 256
8.3.5 Fields are off limits......Page 257
8.3.6 Unit tests and interception......Page 259
8.4 Summary......Page 261
Best practices in code design......Page 263
9.1 Objects and visibility......Page 264
9.1.1 Safe publication......Page 267
9.1.2 Safe wiring......Page 268
9.2.1 On data and services......Page 270
9.2.2 On better encapsulation......Page 275
9.3 Objects and concurrency......Page 280
9.3.1 More on mutability......Page 281
9.3.2 Synchronization vs. concurrency......Page 284
9.4 Summary......Page 287
Integrating with third-party frameworks......Page 289
10.1 Fragmentation of DI solutions......Page 290
10.2 Lessons for framework designers......Page 293
10.2.1 Rigid configuration anti-patterns......Page 294
10.2.2 Black box anti-patterns......Page 299
10.3.1 Case study: JSR-303......Page 303
10.4 Summary......Page 309
Dependency injection in action!......Page 312
11.2 Setting up the application......Page 313
11.3 Configuring Google Sitebricks......Page 317
11.4 Crosstalkβs modularity and service coupling......Page 318
11.5 The presentation layer......Page 319
11.5.1 The HomePage template......Page 321
11.5.2 The Tweet domain object......Page 324
11.5.3 Users and sessions......Page 325
11.5.4 Logging in and out......Page 327
11.6 The persistence layer......Page 331
11.6.1 Configuring the persistence layer......Page 333
11.7 The security layer......Page 334
11.8 Tying up to the web lifecycle......Page 335
11.9 Finally: up and running!......Page 336
11.10 Summary......Page 337
A.2.1 The DSL basics......Page 338
A.2.2 The Factory chain......Page 339
A.2.3 Contextual injection via input parameters......Page 340
A.2.4 Reinjection via factory injection......Page 341
B.1.1 Injection annotations......Page 343
B.2.1 But how do I kickstart the whole thing?......Page 344
B......Page 346
D......Page 347
G......Page 348
J......Page 349
O......Page 350
S......Page 351
T......Page 352
Z......Page 353
Back cover......Page 354
π SIMILAR VOLUMES
Dependency Injection by Dhanji Prasanna is essential reading for anyone wanting to understand how to organise and structure modern Java codebases using Dependency Injection (DI) techniques. Now that JSR-330 (Dependency Injection for Java) is part of the JDK, DI will become an important design tec
Dependency Injection Principles, Practices, and Patterns teaches you to use DI to reduce hard-coded dependencies between application components. You'll start by learning what DI is and what types of applications will benefit from it. Then, you'll work through concrete scenarios using C# and the .NET
Dependency Injection Principles, Practices, and Patterns teaches you to use DI to reduce hard-coded dependencies between application components. You'll start by learning what DI is and what types of applications will benefit from it. Then, you'll work through concrete scenarios using C# and the .NET
Dependency Injection Principles, Practices, and Patterns teaches you to use DI to reduce hard-coded dependencies between application components. You'll start by learning what DI is and what types of applications will benefit from it. Then, you'll work through concrete scenarios using C# and the .NET