- ISBN13: 978-1-59059-997-6
- ISBN10: 1-59059-997-7
- 192 pp.
- Published Apr 2008
- Print Book Price: $19.99
- eBook Price: $13.99
Errata Submission
If you think that you've found an error in Google Guice: Agile Lightweight Dependency Injection Framework, please let us know about it. You will find any confirmed erratum below, so you can check if your concern has already been addressed.
Errata
| Issue | Author's Response |
|---|---|
| Hi, I found the following code snipped in your book: InputStream is = new Object(){}.getClass().getEnclosingClass().getResourceAsStream(name); Why don't use: InputStream resourceAsStream = ClassLoader.getSystemResourceAsStream(propertiesFile); Any comments are well appreciated, Richard |
ClassLoader.getSystemResourceAsStream uses the system classloader, which usually fails to load your resources when you're running in a container because of the ClassLoader hierarchy (as you know parents can't look "down" the hierarchy). That said, the code in the book is uses the getEnclosingClass() hack for maximum copy and paste compatibility. Because it is a static method, getClass() can't be used so I would have had to reference the enclosing class with its name. I probably could have also used Thread.currentThread().getContextClassLoader().getResourceAsStream(). Hope this helps, and thanks for reading the book! |
| Chapter 6:Page 95 Repeated book title: Tip: To learn about Struts 2, I recommend that you read a book like Struts 2 In Action Struts 2 In Action... |
Thanks! |
| Chapter: Appendix Page: 169 Hello Servlet Guice First paragraph, 2nd sentence. Extra word "to" This section gives to the source code to a simple “Hello, World!” This section gives the source code to a simple “Hello, World!” |
You're right, thanks for the report! |
| Chapter 3: Page 38 Technical error? "Note: Constant bindings to an Enum or a Class don’t require you to specify them as a String..." In my experience running this example, strings a required. For example, the binding: bindConstant().annotatedWith(Names.named("stage")).to(chapter3.constants.BigStage.class); Compiles Ok, but produces an error at runtime: Exception in thread "main" com.google.inject.ConfigurationException: Error at chapter3.constants.ConcertHall.stageType(ConcertHall.java:16) Binding to java.lang.Class<?> annotated with @com.google.inject.name.Named(value=stage) not found. No bindings to that type were found. Perhaps I have misunderstood, but it would be helpful to have a clarification. |
This is because of generic type erasure in Java, and there are a couple of workarounds. One solution is to remove the wildcard, like this: @Inject @Named("stage") private Class stageType; Another is to bind a TypeLiteral that captures the type (not using bindConstant) bind(new TypeLiteral<Class<?>>(){}).annotatedWith(Names.named("stage")).toInstance(BigStage.class); Recently, the Guice team has been able to hit type erasure in the face and it can now actually resolve type parameters (http://groups.google.com/group/google-guice/browse_thread/thread/8ded68ad96146188/). I'm not sure if they have addressed this particular problem, but it seems like they are making good progress. |
| Chapter 2: Page 13: Last paragraph. "... If you’re used accustomed to using Spring, ..." used accustomed - remove one. |
You're right, thanks! |
| Chapter 1: Page 8: 2nd Paragraph. "First, you put the Guice @Inject annotation at the injection point, ..." Change to: First, include guice,jar in your classpath and import com.google.inject.Guice. Then you put the Guice @Inject annotation at the injection point, ... |
Chapter 2 introduces Guice more formally. The first chapter is supposed to be a comparison between factories, dependency injection and where Guice can be placed in that landscape. I intentionally stayed away from mixing a high level overview with facts. |
| Chapter 1: Page 7: Listing 1-6 While there is nothing wrong with this listing per se, it could be changed to better relate to the existing example code: public class Chef { ... public static void main(String[] args) { Chef chef = new Chef(FortuneServiceFactory.getFortuneService()); ... } ... } |
The purpose of this code listing is to illustrate that trying to use inversion of control with factories will make you write more factories. So while I agree that I could have also listed a main method, the ChefFactory is what is key in this example. I hope this helps, thanks for the feedback! |
| Chapter 1: Page 3: Listing 1-1 Class FortuneServiceImpl is missing required imports import java.util.Arrays; import java.util.List; import java.util.Random; Omitting these for the sake of brevity is valid (as an IDE can easily insert them), but a mention of this as a requirement to compile the code would be nice. |
I would like to think of the chapter 1 examples as a way to convince people about a certain programming style, so in that philosophy I still think it was a good decision. But you're right I could have mentioned it. |
| In chapter 3, on page 39, in the last paragraph of "CONSTANT BINDING PITFALL", it talks about using guice’s toInstance(...) would let the compiler catch the mistake of assigning int to a long. But with guice 1.0, I did not find bindConstant().annotatedWith(Names.named("long")).toInstance(...) exists. | At the top of page 40, I present an example which illustrates what I mean: bind(Long.class) .annotatedWith(Names.named("long")) .toInstance(123L); The idea is that not using bindConstant() can help you avoid weird problems. |
