Guice Agile Lightweight Dependency Injection Framework.pdf

(1868 KB) Pobierz
373687686 UNPDF
Available as a
PDF Electronic Book
or Print On Demand
Guice
Agile Lightweight Dependency
Injection Framework
180
PAgeS
Robbie Vanbrabant
Google
373687686.022.png 373687686.023.png 373687686.024.png 373687686.025.png 373687686.001.png 373687686.002.png 373687686.003.png 373687686.004.png 373687686.005.png 373687686.006.png 373687686.007.png 373687686.008.png 373687686.009.png 373687686.010.png 373687686.011.png 373687686.012.png 373687686.013.png 373687686.014.png 373687686.015.png
About firstPress
Apress's firstPress series is your source for understanding cutting-edge technology. Short,
highly focused, and written by experts, Apress's firstPress books save you time and effort. They
contain the information you could get based on intensive research yourself or if you were to
attend a conference every other week—if only you had the time. They cover the concepts and
techniques that will keep you ahead of the technology curve. Apress's firstPress books are real
books, in your choice of electronic or print-on-demand format, with no rough edges even when
the technology itself is still rough. You can't afford to be without them.
Google Guice: Agile Lightweight
Dependency Injection Framework
Dear Reader,
This book welcomes you to the world of Google Guice (pronounced “juice”), the latest and
greatest dependency injection framework that fully exploits all modern Java™ features. One
step at a time, this book will help turn any XML-spitting programmer into a true Guice master,
or a “Bob,” as I like to say.
I’m particularly proud of this book’s coverage. To make sure that nobody gets left behind, I
start out with an introduction to dependency injection concepts. From there, you will learn how
Guice makes your life easier, and you’ll gradually become a Guice expert. Beyond that, I also
devoted two chapters to web application development with Struts 2 and Wicket, including
content on how to organize your application and how to use Warp Persist, a popular Guice
extension, to access your data using the Java Persistence API and Hibernate.
Writing this book was not an easy task, so in addition to Apress, I’d like to thank some people
in particular. First, I would like to thank Dhanji R. Prasanna for referring me to Apress to write
this book. He has also done a fantastic job as my technical reviewer. I’d also like to thank Bob
Lee, the inventor of Guice, who kindly answered all of my questions and inspired much of the
content in Chapter 8. Finally, a big thank you goes out to my friends, my parents, my brother,
and anyone who believes in me; you are the people who put the smile on my face.
Now, put on your Batman or Catwoman costume; open up a bottle of wine; pick up this book;
and put your feet up. Be a Java hero, and may Guice help you on your way. Just don’t forget to
send me a picture of you in that getup.
Robbie Vanbrabant
Contents
Chapter 1: Setting the Stage ......................................................................................... 1
The Problem ................................................................................................................................... 1
A Fortunate Example .................................................................................................................... 3
Dependency Injection .................................................................................................................... 5
DI, Guice Style .............................................................................................................................. 8
Summary ...................................................................................................................................... 10
Chapter 2: Enter Guice .............................................................................................. 11
Getting Guice ............................................................................................................................... 11
Preparing the Code ...................................................................................................................... 12
Specifying an Implementation .................................................................................................... 15
Bootstrapping ............................................................................................................................... 15
Choosing Between Implementations .......................................................................................... 17
Implicit Bindings ......................................................................................................................... 21
Scoping ......................................................................................................................................... 22
Debunking Myths ........................................................................................................................ 23
Summary ...................................................................................................................................... 27
Chapter 3: From Journeyman to Bob ......................................................................... 29
Providers ...................................................................................................................................... 29
@Named ...................................................................................................................................... 32
Binding Constants ....................................................................................................................... 34
Binding Generic Types ............................................................................................................... 38
Properties ..................................................................................................................................... 42
Static Injection ............................................................................................................................. 44
Google Guice: Agile Lightweight Dependency Injection Framework
i
373687686.016.png 373687686.017.png 373687686.018.png
Custom Scopes ............................................................................................................................ 45
Web Scopes .................................................................................................................................. 49
Organizing Modules .................................................................................................................... 51
The Binding EDSL ...................................................................................................................... 53
How Guice Resolves Dependencies ........................................................................................... 56
Summary ...................................................................................................................................... 57
Chapter 4: Aspect-Oriented Programming ................................................................. 59
AOP for Mere Mortals ................................................................................................................ 60
How Guice AOP Works .............................................................................................................. 60
Method Interception .................................................................................................................... 61
Phoning Home ............................................................................................................................. 64
Summary ...................................................................................................................................... 69
Chapter 5: Integrating with the Web ........................................................................... 71
The Integration Challenge ........................................................................................................... 71
Bootstrapping ............................................................................................................................... 72
Inviting Servlets to the Club ....................................................................................................... 73
Configuration Discovery ............................................................................................................. 74
Struts 2 ......................................................................................................................................... 76
Wicket .......................................................................................................................................... 78
Where Are the Web Scopes? ...................................................................................................... 85
Warp Servlet ................................................................................................................................ 86
Summary ...................................................................................................................................... 92
Chapter 6: Practical Guice ......................................................................................... 93
Requirements ............................................................................................................................... 93
The Big Picture ............................................................................................................................ 95
Project Structure .......................................................................................................................... 99
Setting Up Struts 2 .................................................................................................................... 103
Getting Guiced ........................................................................................................................... 104
76a2626bcfb21763948a3d635f6fe985
ii Google Guice: Agile Lightweight Dependency Injection Framework
373687686.019.png 373687686.020.png
Defining the Model ................................................................................................................... 105
Database Access with Warp Persist ......................................................................................... 109
Implementing the Data Access Layer ...................................................................................... 111
The Home Screen ...................................................................................................................... 118
The Create and Edit Screens ..................................................................................................... 121
Unit Testing ............................................................................................................................... 123
Summary .................................................................................................................................... 125
Chapter 7: Guice Recipes ......................................................................................... 127
Sharing Singletons ..................................................................................................................... 127
Binding Collections ................................................................................................................... 129
Designing Libraries and Limiting Visibility ............................................................................ 136
Viral Annotations ...................................................................................................................... 138
Mixing Scopes ........................................................................................................................... 139
Integrating Spring ...................................................................................................................... 142
Logging ...................................................................................................................................... 145
Integrating JNDI ........................................................................................................................ 146
Using JMX ................................................................................................................................. 147
Summary .................................................................................................................................... 150
Chapter 8: The Future .............................................................................................. 153
The Grand Plan .......................................................................................................................... 153
Growing an Extensible Platform .............................................................................................. 154
Better Up-Front Checking ......................................................................................................... 155
Keeping Guice Simple and Making It Simpler ....................................................................... 156
Improved Tooling Support ........................................................................................................ 158
Addressing DI Shortcomings .................................................................................................... 160
Standardization .......................................................................................................................... 164
Summary .................................................................................................................................... 166
Google Guice: Agile Lightweight Dependency Injection Framework
iii
373687686.021.png
Zgłoś jeśli naruszono regulamin