I think @Named is not a good fit for that, for precisely the reasons you specify. Guice has to know the names of things that will be injected ahead of time. There are ways I can think of that you could torture Guice into doing what you're after (if you really want I can describe them), but there's no getting around telling Guice the names of every parameter you're ever going to want to consume; then you could do a custom scope with Providers for all of them.
I did something that achieves the same end in my Acteur web framework,which sits on top of Guice+Netty: http://timboudreau.com/builds/job/mastfrog-parent/lastSuccessfulBuild/artifact/acteur-modules/acteur-parent/acteur/target/apidocs/index.html?com/mastfrog/acteur/HttpEvent.html Basically you create an interface with one method for each parameter that might be present; the framework will implement it as a dynamic proxy and make it available for injection. That gets you something naturally null-tolerant and with at least minimal validation that number parameters are actually numbers and so forth. You might try something like that - it's both a more OOP-like approach, and avoids weirdness like having to make sure there are no name clashes between URL parameters and anything else you're using @Named for. -Tim On Friday, June 27, 2014 4:02:11 PM UTC-4, Edgar Espina wrote: > > Hi, > > I'm using Guice to inject HTTP Request parameters (I'm not using > guice-servlet) and I will like to do things like: > > public class Command { >> public Command(@Named("name") String name) { >> } >> } > > > Here "name" is an HTTP parameter and I'm able to bind it when present. > > The problem (of course) is when that parameter isn't present or sent and > want to have a default value for it. > > I know isn't possible to inject something without a binding and it's OK. > So what I'm asking here is feedback on how to deal with this? and/or an > alternate solution? > > Thanks, > > edgar > -- You received this message because you are subscribed to the Google Groups "google-guice" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/google-guice. To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/ab66302c-bc22-43e5-964e-7e35698f89ab%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
