Author: limpbizkit
Date: Tue Jan 20 12:03:45 2009
New Revision: 807
Modified:
wiki/FrequentlyAskedQuestions.wiki
Log:
Edited wiki page through web user interface.
Modified: wiki/FrequentlyAskedQuestions.wiki
==============================================================================
--- wiki/FrequentlyAskedQuestions.wiki (original)
+++ wiki/FrequentlyAskedQuestions.wiki Tue Jan 20 12:03:45 2009
@@ -133,5 +133,24 @@
}
}}}
+===How can I inject optional parameters into a constructor?==
+Neither constructors nor `...@provides` methods support optional injection.
To work-around this, you can create an inner class that holds the optional
value:
+{{{
+class Car {
+ private Engine engine;
+ private AirConditioner airConditioner;
+
+ public Car(Engine engine, AirConditionerHolder airConditionerHolder) {
+ this.engine = engine;
+ this.airConditioner = airConditionerHolder.value;
+ }
+
+ static class AirConditionerHolder {
+ @Inject(optional=true) AirConditioner value = new NoOpAirconditioner();
+ }
+}
+}}}
+This also allows a default value for the optional parameter.
+
===How can I get other questions answered?===
Please post to the [http://groups.google.com/group/google-guice
google-guice] discussion group.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---