Author: limpbizkit
Date: Thu Jan 1 20:43:05 2009
New Revision: 784
Modified:
wiki/MinimizeMutability.wiki
Log:
Edited wiki page through web user interface.
Modified: wiki/MinimizeMutability.wiki
==============================================================================
--- wiki/MinimizeMutability.wiki (original)
+++ wiki/MinimizeMutability.wiki Thu Jan 1 20:43:05 2009
@@ -18,3 +18,12 @@
}}}
All fields of this class are final and initialized by a single
`...@inject`-annotated constructor.
[http://java.sun.com/docs/books/effective/toc.html Effective Java]
discusses other benefits of immutability.
+==Injecting methods and fields==
+*Constructor injection* has some limitations:
+ * Injected constructors may not be optional.
+ * It cannot be used unless objects are created by Guice. This is a
dealbreaker for certain frameworks.
+ * Subclasses must call `super()` with all dependencies. This makes
constructor injection cumbersome, especially as the injected base class
changes.
+
+*Method injection* is most useful when you need to initialize an instance
that is not constructed by Guice. Extensions like AssistedInject and
Multibinder use method injection to initialize bound objects.
+
+*Field injection* has the most compact syntax, so it shows up frequently
on slides and in examples. It is neither encapsulated nor testable. Never
inject [http://code.google.com/p/google-guice/issues/detail?id=245 final
fields]; the JVM doesn't guarantee that the injected value will be visible
to all threads.
\ No newline at end of file
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice-dev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-guice-dev?hl=en
-~----------~----~----~----~------~----~------~--~---