context-dependent/custom constructor injection

107 views
Skip to first unread message

Oleg Estekhin

unread,
Aug 30, 2011, 8:43:46 AM8/30/11
to google...@googlegroups.com
Hi

Is there some way to inject some constructor parameters in a
context-dependent way?

For example, there is way to inject instance fields depending on the context
(both class and instance) using TypeListener's. Taking the
http://code.google.com/p/google-guice/wiki/CustomInjections as example - it
there a way to custom-inject the Logger into the constructor? Something like
the PaymentService class from the example will have a constructor
"PaymentService( @SomeCustomAnnotationPerhaps Logger logger )", and some
type listener or whatever will be asked to provide the Logger instance, but
it will know the class it is providing it for.

The use-case would be almost the same as for custom field/method injections,
but in the form of custom constructor injection with the ability to use the
injected value in the constructor.

It can be partially done with @Named constructor parameters and
corresponding @Named bindings, or with custom binding annotations, but they
still require an explicit pairing between usage declaration and binding
declaration and do not allow to create/configure an object to be injected
into the constructor of injectee class on the fly depending on the injectee'
class.


Oleg

Sam Berlin

unread,
Aug 30, 2011, 9:06:20 AM8/30/11
to google...@googlegroups.com
There's a new thing called ProvisionListener in Guice SVN, but not in a released version.  ProvisionListener can give you access to the dependency hierarchy of why something is being provisioned, as well as the Key that is being provisioned, both before & after the provisioning happens.  You can't (by design) change the injected dependencies or the return value of the provision, though.  You can manually do things to the provisioned object, though.

Guice explicitly does not allow support for context-dependent injections, because it would make maintenance a nightmare and debugging even harder than it already is.

sam

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.


Noctarius

unread,
Aug 31, 2011, 4:47:25 AM8/31/11
to google-guice
You can take a look here:
https://bitbucket.org/noctarius/guiceidentityinjection/src/014f6cb8fcc0/src/main/java/com/google/inject/identityinjection/IdentityProviderFactory.java

It takes a mixture of PrivisionListener, ThreadLocal and Factory.

On 30 Aug., 14:43, "Oleg Estekhin" <oleg.estek...@gmail.com> wrote:
> Hi
>
> Is there some way to inject some constructor parameters in a
> context-dependent way?
>
> For example, there is way to inject instance fields depending on the context
> (both class and instance) using TypeListener's. Taking thehttp://code.google.com/p/google-guice/wiki/CustomInjectionsas example - it
Reply all
Reply to author
Forward
0 new messages