Discussion:
executing code after creation and DI
Martin Fuzzey
2008-06-18 15:54:51 UTC
Permalink
Hi,
I would like to be able to execute arbitary code using objects created
by picocontainer after DI has been performed.
The code is not in the objects themselves (so lifecycle methods wont't do)
I would like to do this based on object type / key.

For example suppose I have fruit classes (Apple, Pear, ...) and animal
classes (Cow, Tiger, ...) managed by picocontainer.
When an animal is created by pico and after its dependencies have been
resolved I would like to call a method on my Zoo class passing the
animal as a parameter.
However I don't want to pass fruits to my zoo.

This seems a bit like behaviours except for the class specific thing.
[ of course I could create a behaviour and test the object type but
that doesn't seem right! ]

I think I could do this using addAdapter() rather than addComponent()
but that would mean I would have to duplicate some of the code in
addComponent().

Ideas appreciated,

Martin

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Paul Hammant
2008-06-18 16:03:17 UTC
Permalink
Look at the http://picocontainer.org/interception.html page

Not cos I'm suggesting AOP, but because if shows the Adapter chaining
being used for custom behaviors.

It's findAdapterOfType(..) that's doing the magic.

You should be able to make a behavior, have it in the chain for the
whole container, add your components as normal, then post process to
invoke a 'reinject' method on your custom behavior.

- Paul
Post by Martin Fuzzey
Hi,
I would like to be able to execute arbitary code using objects created
by picocontainer after DI has been performed.
The code is not in the objects themselves (so lifecycle methods wont't do)
I would like to do this based on object type / key.
For example suppose I have fruit classes (Apple, Pear, ...) and animal
classes (Cow, Tiger, ...) managed by picocontainer.
When an animal is created by pico and after its dependencies have been
resolved I would like to call a method on my Zoo class passing the
animal as a parameter.
However I don't want to pass fruits to my zoo.
This seems a bit like behaviours except for the class specific thing.
[ of course I could create a behaviour and test the object type but
that doesn't seem right! ]
I think I could do this using addAdapter() rather than addComponent()
but that would mean I would have to duplicate some of the code in
addComponent().
Ideas appreciated,
Martin
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
Paul Hammant
2008-06-18 16:23:18 UTC
Permalink
The other way is to specify a CompositeInjection .. It will leverage
multiple Injection types to make a component complete. The first
should be ConstructorInjection, follow that with womething that makes
sense to you - AnnotatedMethodInjection (with a custom annotation ) or
MethodInjection (with a custom method name)

- Paul
Post by Paul Hammant
Look at the http://picocontainer.org/interception.html page
Not cos I'm suggesting AOP, but because if shows the Adapter
chaining being used for custom behaviors.
It's findAdapterOfType(..) that's doing the magic.
You should be able to make a behavior, have it in the chain for the
whole container, add your components as normal, then post process
to invoke a 'reinject' method on your custom behavior.
- Paul
Post by Martin Fuzzey
Hi,
I would like to be able to execute arbitary code using objects created
by picocontainer after DI has been performed.
The code is not in the objects themselves (so lifecycle methods wont't do)
I would like to do this based on object type / key.
For example suppose I have fruit classes (Apple, Pear, ...) and animal
classes (Cow, Tiger, ...) managed by picocontainer.
When an animal is created by pico and after its dependencies have been
resolved I would like to call a method on my Zoo class passing the
animal as a parameter.
However I don't want to pass fruits to my zoo.
This seems a bit like behaviours except for the class specific thing.
[ of course I could create a behaviour and test the object type but
that doesn't seem right! ]
I think I could do this using addAdapter() rather than addComponent()
but that would mean I would have to duplicate some of the code in
addComponent().
Ideas appreciated,
Martin
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
Jörg Schaible
2008-06-18 16:54:56 UTC
Permalink
Post by Martin Fuzzey
Hi,
I would like to be able to execute arbitary code using objects created
by picocontainer after DI has been performed.
The code is not in the objects themselves (so lifecycle methods wont't do)
I would like to do this based on object type / key.
For example suppose I have fruit classes (Apple, Pear, ...) and animal
classes (Cow, Tiger, ...) managed by picocontainer.
When an animal is created by pico and after its dependencies have been
resolved I would like to call a method on my Zoo class passing the
animal as a parameter.
However I don't want to pass fruits to my zoo.
This seems a bit like behaviours except for the class specific thing.
[ of course I could create a behaviour and test the object type but
that doesn't seem right! ]
I think I could do this using addAdapter() rather than addComponent()
but that would mean I would have to duplicate some of the code in
addComponent().
Ideas appreciated,
As alternative to Paul's suggestions ... you could define you Zoo ctor like:

class Zoo {
Zoo(Animal[] animals) {
...
}
}

All animals will be provided automatically to the zoo. See
http://docs.codehaus.org/display/PICO/Arrays,+Collections+and+Maps

- Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Continue reading on narkive:
Loading...