You can bind constants using:
bindConstant().to("myconst"); // may be string, int, long, etc.
bindConstant() also can bind constants to annotations:
bindConstant().annotatedWith(Port.class).to(443); //binds a port const to
https value
You can also use the @Named built-in annotation, which allows you to save on
writing the boilerplate for constants:
bindConstant().annotatedWith(Names.named("ssl_port")).to(443);
... and this can be injected using the @Named annotation as follows:
@Inject @Named("ssl_port") int port;
You can use constants on your Group interface as a way to centralize the
String inside @Named.
Dhanji.
On Sat, Jul 4, 2009 at 9:11 PM, Sebastian Hoß <[email protected]> wrote:
>
> Btw. with the interface constant i could do something like this:
>
> if (group.getParent().equals(Group.ROOT)) { ... }
>
> How would i do something similar with guice?
>
> On Jul 4, 12:51 pm, Sebastian Hoß <[email protected]> wrote:
> > Hi there,
> >
> > i want guice to inject some constant values into some of my
> > interfaces. Is that possible? Are there any better ways of defining
> > and using a global value?
> >
> > In my case i defined a Group interface which holds the constant ROOT.
> > All Groups should be below this root group. The constructor for the
> > Group implementation already waits for a (parent) Group and has a
> > private field for his parent. Should i inject there? Can i configure
> > guice somehow so that i injects the root group if i don't supply the
> > parent myself and otherwise use my supplied parent?
> >
> > Greets!
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice" 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?hl=en
-~----------~----~----~----~------~----~------~--~---