Registering a provider of some resource R does not load R when module
is started. It will be loaded when you inject R, when you inject
something which depends on R or when you inject Provider<R> and invoke
its #get method.
I agree; it would be really nice to be able to write a @Provides
method which has the binding annotation instance as a parameter; so
that you can write a single method which extracts values from the
annotation instance & uses those to create the required object to
inject - rather than having to explicitly bind every permutation of
values of the annotation to instances/providers.
> That being said, perhaps someone knows a way to do this already? Is
> it possible to get access to the Annotation instance from within a
> provider? I've been trying to follow the discussions here, but
> haven't really been able to figure out what's supported and what
> isn't.
>
> http://code.google.com/p/google-guice/issues/detail?id=258
> http://code.google.com/p/google-guice/issues/detail?id=27
>
> Can I get this sort of capability with something like Guiceyfruit?
> (And if so, can I use Guiceyfruit without AOP since AOP's not
> supported on Android)
Unfortunately not in the example you show....
@Inject
public void setString( @StringResource(R.string.foobar) String foobar)
GuiceyFruit currently focusses on using an annotation to specify a
parameterised injection point (like @Resource) which only tends to
work on a field or property injection point - not on annotations on
parameters at a regular @Inject injection point.
If you did it more like this...
@StringResource(R.string.foobar)
public void setString( String foobar)
then you could bind the StringResource annotation as an injection
point like the way @Resource is supported; you can then write a kind
of provider which takes the StringResource annotation and uses its
values to create an object.
e.g.
for code sharing reasons much of the heavy lifting is actually in this
base class
So in GuiceyFruit you'd implement a class which extends
AnnotationMemberProviderSupport<StringResource> then you bind it in
your GuiceyFruitModule via the bindAnnotationInjector() method
http://code.google.com/p/guiceyfruit/source/browse/trunk/guiceyfruit-core/src/main/java/org/guiceyfruit/jsr250/Jsr250Module.java
--
James
-------
http://macstrac.blogspot.com/
Open Source Integration
http://fusesource.com/
It seems like I've been reading a lot of "yeah that would be nice"
posts about being able to read attribute values from within
providers. It certainly would help me for Android and other
projects. Guice maintainers, any thoughts about incorporating this
into Guice?
Cheers,
Mike
Though InjectionListeners can only be used to create new injection
points not using @Inject. Its not possible to use them with @Inject
injection points such as this
public class Foo {
@Inject
public Foo(@StringResource("blah") String xyz) {
...
2009/7/31 Dhanji R. Prasanna <dha...@gmail.com>:
> Yea, we considered this very early on, with providing the InjectionPointThough InjectionListeners can only be used to create new injection
> itself to a provider. But it didn't materialize then and there hasn't been
> much demand since (InjectionListeners also solve the problem orthogonally,
> as you note).
points not using @Inject. Its not possible to use them with @Inject
injection points such as this