| |
| |
Preface | |
| |
| |
Acknowledgments | |
| |
| |
About this book | |
| |
| |
About the cover illustration | |
| |
| |
| |
Dependency injection: what's all the hype? | |
| |
| |
| |
Every solution needs a problem | |
| |
| |
Seeing objects as services | |
| |
| |
| |
Pre-DI solutions | |
| |
| |
Construction by hand | |
| |
| |
The Factory Pattern | |
| |
| |
The Service Locator pattern | |
| |
| |
| |
Embracing dependency injection | |
| |
| |
The Hollywood Principle | |
| |
| |
Inversion of Control vs. dependency injection | |
| |
| |
| |
Dependency injection in the real world | |
| |
| |
Java | |
| |
| |
DI in other languages and libraries | |
| |
| |
| |
Summary | |
| |
| |
| |
Time for injection | |
| |
| |
| |
Bootstrapping the injector | |
| |
| |
| |
Constructing objects with dependency injection | |
| |
| |
| |
Metadata and injector configuration | |
| |
| |
XML injection in Spring | |
| |
| |
From XML to in-code configuration | |
| |
| |
Injection in PicoContainer | |
| |
| |
Revisiting Spring and autowiring | |
| |
| |
| |
Identifying dependencies for injection | |
| |
| |
Identifying by string keys | |
| |
| |
Limitations of string keys | |
| |
| |
Identifying by type | |
| |
| |
Limitations of identifying by type | |
| |
| |
Combinatorial keys: a comprehensive solution | |
| |
| |
| |
Separating infrastructure and application logic | |
| |
| |
| |
Summary | |
| |
| |
| |
Investigating DI | |
| |
| |
| |
Injection idioms | |
| |
| |
Constructor injection | |
| |
| |
Setter injection | |
| |
| |
Interface injection | |
| |
| |
Method decoration (or AOP injection) | |
| |
| |
| |
Choosing an injection idiom | |
| |
| |
Constructor vs. Setter injection | |
| |
| |
The constructor pyramid problem | |
| |
| |
The circular reference problem | |
| |
| |
The in-construction problem | |
| |
| |
Constructor injection and object validity | |
| |
| |
| |
Not all at once: partial injection | |
| |
| |
The reinfection problem | |
| |
| |
Reinjection with the Provider pattern | |
| |
| |
The contextual injection problem | |
| |
| |
Contextual injection with the Assisted Injection pattern | |
| |
| |
Flexible partial injection with the Builder pattern | |
| |
| |
| |
Injecting Objects in sealed code | |
| |
| |
Injecting with externalized metadata | |
| |
| |
Using the Adapter pattern | |
| |
| |
| |
Summary | |
| |
| |
| |
Building modular applications | |
| |
| |
| |
Understanding the role of an object | |
| |
| |
| |
Separation of concerns (my pants are too tight!) | |
| |
| |
Perils of tight coupling | |
| |
| |
Refactoring impacts of tight coupling | |
| |
| |
Programming to contract | |
| |
| |
Loose coupling with dependency injection | |
| |
| |
| |
Testing components | |
| |
| |
Out-of-container (unit) testing | |
| |
| |
I really need my dependencies! | |
| |
| |
More on mocking dependencies | |
| |
| |
Integration testing | |
| |
| |
| |
Different deployment profiles | |
| |
| |
Rebinding dependencies | |
| |
| |
Mutability with the Adapter Pattern | |
| |
| |
| |
Summary | |
| |
| |
| |
Scope: a fresh breath of state | |
| |
| |
| |
What is scope? | |
| |
| |
| |
The no scope (or default scope) | |
| |
| |
| |
The singleton scope | |
| |
| |
Singletons in practice | |
| |
| |
The singleton anti-pattern | |
| |
| |
| |
Domain-specific scopes: the web | |
| |
| |
HTTP request scope | |
| |
| |
HTTP session scope | |
| |
| |
| |
Summary | |
| |
| |
| |
More use cases in scoping | |
| |
| |
| |
Defining a custom scope | |
| |
| |
A quick primer on transactions | |
| |
| |
Creating a custom transaction scope | |
| |
| |
A custom scope in Guice | |
| |
| |
A custom scope in Spring | |
| |
| |
| |
Pitfalls and corner cases in scoping | |
| |
| |
Singletons must be thread-safe | |
| |
| |
Perils of Scope-widening injection | |
| |
| |
| |
Leveraging the Power of scopes | |
| |
| |
Cache scope | |
| |
| |
Grid Scope | |
| |
| |
Transparent grid computing with DI | |
| |
| |
| |
Summary | |
| |
| |
| |
From birth to death: object lifecycle | |
| |
| |
| |
Significant events in the life of objects | |
| |
| |
Object creation | |
| |
| |
Object destruction (or finalization) | |
| |
| |
| |
One size doesn't fit all (domain-Specific lifecycle) | |
| |
| |
Contrasting lifecycle scenarios: servlets vs. database connections | |
| |
| |
The Destructor anti-pattern | |
| |
| |
Using Java's Closeable interface | |
| |
| |
| |
A real-world lifecycle scenario: stateful EJBs | |
| |
| |
| |
Lifecycle and lazy instantiation | |
| |
| |
| |
Customizing lifecycle with postprocessing | |
| |
| |
| |
Customizing lifecycle with multicasting | |
| |
| |
| |
Summary | |
| |
| |
| |
Managing an object's behavior | |
| |
| |
| |
Intercepting methods and AOP | |
| |
| |
A tracing interceptor with Guice | |
| |
| |
A tracing interceptor with Spring | |
| |
| |
How proxying works | |
| |
| |
Too much advice can be dangerous! | |
| |
| |
| |
Enterprise use cases for interception | |
| |
| |
Transactional methods with warp-persist | |
| |
| |
Securing methods with Spring Security | |
| |
| |
| |
Pitfalls and assumptions about interception and proxying | |
| |
| |
Sameness tests are unreliable | |
| |
| |
Static methods cannot be intercepted | |
| |
| |
Neither can private methods | |
| |
| |
And certainly not final methods! | |
| |
| |
Fields are off limits | |
| |
| |
Unit tests and interception | |
| |
| |
| |
Summary | |
| |
| |
| |
Best practices in code design | |
| |
| |
| |
Objects and visibility | |
| |
| |
Safe publication | |
| |
| |
Safe wiring | |
| |
| |
| |
Objects and design | |
| |
| |
On data and services | |
| |
| |
On Better encapsulation | |
| |
| |
| |
Objects and concurrency | |
| |
| |
More on mutability | |
| |
| |
Synchronization vs. concurrency | |
| |
| |
| |
Summary | |
| |
| |
| |
Integrating with third-party frameworks | |
| |
| |
| |
Fragmentation of DI solutions | |
| |
| |
| |
Lessons for framework designers | |
| |
| |
Rigid configuration anti-patterns | |
| |
| |
Black box anti-patterns | |
| |
| |
| |
Programmatic configuration to the rescue | |
| |
| |
Case study: JSR-303 | |
| |
| |
| |
Summary | |
| |
| |
| |
Dependency injection in action! | |
| |
| |
| |
Crosstalk: a Twitter Clone! | |
| |
| |
Crosstalk's requirements | |
| |
| |
| |
Setting up the application | |
| |
| |
| |
Configuring Google Sitebricks | |
| |
| |
| |
Crosstalk's modularity and service coupling | |
| |
| |
| |
The presentation layer | |
| |
| |
The HomePage template | |
| |
| |
The Tweet domain object | |
| |
| |
Users and sessions | |
| |
| |
Logging in and out | |
| |
| |
| |
The persistence layer | |
| |
| |
Configuring the Persistence layer | |
| |
| |
| |
The security layer | |
| |
| |
| |
Tying up to the web lifecycle | |
| |
| |
| |
Finally: up and running! | |
| |
| |
| |
Summary | |
| |
| |
| |
The Butterfly Container | |
| |
| |
| |
Smarty Pants for Adobe Flex | |
| |
| |
index | |