bineanzhou / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

Allow to read parameters from Injection annotations #191

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
 It would be fine if Provider could read passed parameters in Inject
related annotations. Example:

class Foo {
    @Inject(Foo.class.getName()) Logger logger;

}

So in Module.configure(Binder binder) I would have:

Porivder pp = new Porivder() {
Logger get() {
 return Logger.getLogger(getAnnotation(Inject.class).value()); //
getAnnotation(..) would return instance of Inject annotation and I have
made asumption that Inject annotation has value()
 // or maybe?
 return Logger.getLogger(getInstancianisedClass().getName());
// then in Foo class we have simple @Inject Logger logger;
}
}

bind(Logger.class).toProvider(pp).in(SingletonForInstancianisedClass.class);

The same would be used for injecting WebServices:

@Inject @WebService("ClockServiceLocation") IClockService clockService;

So in Module.configure(Binder binder) I would have:

   final Properites prop = new Properties();

prop.load(getClass().getClassLoader().getResourceAsStream("myconfig.properites")
);

Porivder pp2 = new Porivder() {
IClockService get() {
   return new ExampleDynamicClientWebServiceStub(   prop.get(            
getAnnotation(WebService.class).value()                ).toString()        );
}
}

bind(IClockService.class).annotatedWith(WebService.class).toProvider(pp2);

Original issue reported on code.google.com by aleksand...@gmail.com on 16 Apr 2008 at 9:30

GoogleCodeExporter commented 9 years ago
Sorry that this is posted as Defect but was unable to find where to change type 
of issue.

Original comment by aleksand...@gmail.com on 16 Apr 2008 at 9:33

GoogleCodeExporter commented 9 years ago
Well in both cases it is not possible to do that in Java (static types checking 
is
too restrict) but concept of:

- <T> T getAnnotation(Class<T> class) in Provider
- SingletonForInstancianisedClass as a Scope
- Class getInstancianisedClass() in Provider

is nice

Original comment by aleksand...@gmail.com on 16 Apr 2008 at 9:44

GoogleCodeExporter commented 9 years ago
Duplicate of issue 27. That issue suggests we inject the injection point to 
address this problem.

Original comment by limpbizkit on 9 Jun 2008 at 6:34