Discussion:
Reinjection with existing instance
Christopher Oezbek
2009-01-23 11:40:26 UTC
Permalink
Hi Guys,
I would like to inject dependencies for an instance that was created
outside of PicoContainer.[1] Of course, constructor injection cannot work,
since the object is already there, but can I ask PicoContainer to inject
all annotated fields or setters?

I have looked at the page about reinjection, but my usecase seems
different:

public interface Foo {
public B getB();
}

public class B { }

public class A implements Foo {

@Inject
B b;

public B getB() {
return b;
}
}

somewhere:

A a = new A(); // A is created outside our scope, we cannot use
PicoContainer to created it

container.addComponent(B.class);
container.addComponent(Foo.class, a);

// Reinjection here

Foo foo = container.getComponent(Foo.class);

assert foo.getB() != null;

Any ideas for how to achieve this reinjection?

Many thanks,
Christopher

[1] This question was asked in a similar way in October
http://markmail.org/message/s4gqdtzpio4su7nw

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

http://xircles.codehaus.org/manage_email
Paul Hammant
2009-01-23 12:03:06 UTC
Permalink
Reinjection really wants to go in via a single method. At least the
way it is now.

We could recode to allow annotated fields or setters if you really
felt strongly about it ?

You know it would not be thread-safe right ?

Regards,

- Paul
Post by Christopher Oezbek
Hi Guys,
I would like to inject dependencies for an instance that was
created outside of PicoContainer.[1] Of course, constructor
injection cannot work, since the object is already there, but can I
ask PicoContainer to inject all annotated fields or setters?
I have looked at the page about reinjection, but my usecase seems
public interface Foo {
public B getB();
}
public class B { }
public class A implements Foo {
@Inject
B b;
public B getB() {
return b;
}
}
A a = new A(); // A is created outside our scope, we cannot use
PicoContainer to created it
container.addComponent(B.class);
container.addComponent(Foo.class, a);
// Reinjection here
Foo foo = container.getComponent(Foo.class);
assert foo.getB() != null;
Any ideas for how to achieve this reinjection?
Many thanks,
Christopher
[1] This question was asked in a similar way in October http://markmail.org/message/s4gqdtzpio4su7nw
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Christopher Oezbek
2009-01-23 13:56:49 UTC
Permalink
Hi Paul,
I played with the code in the meantime and it seems to just work. Given
the same start:

A a = new A(); // A is created outside our scope, we cannot use
// PicoContainer to create it

container.addComponent(B.class);
container.addComponent(Foo.class, a);

// Perform reinjection
Reinjector injection = new Reinjector(container);
injection.reinject(A.class, new AnnotatedFieldInjection());

Foo foo = container.getComponent(Foo.class);

// Works!
assert foo.getB() != null;

So it seems my problem is solved or am I missing something?
Post by Paul Hammant
You know it would not be thread-safe right ?
Yes, but I would do all this while assembling the container and only once
it has been done, allow users to getComponents().

Thanks for the fast reply,
Christopher
Post by Paul Hammant
Reinjection really wants to go in via a single method. At least the way
it is now.
We could recode to allow annotated fields or setters if you really felt
strongly about it ?
You know it would not be thread-safe right ?
Regards,
- Paul
Post by Christopher Oezbek
Hi Guys,
I would like to inject dependencies for an instance that was created
outside of PicoContainer.[1] Of course, constructor injection cannot
work, since the object is already there, but can I ask PicoContainer to
inject all annotated fields or setters?
I have looked at the page about reinjection, but my usecase seems
public interface Foo {
public B getB();
}
public class B { }
public class A implements Foo {
@Inject
B b;
public B getB() {
return b;
}
}
A a = new A(); // A is created outside our scope, we cannot use
PicoContainer to create it
container.addComponent(B.class);
container.addComponent(Foo.class, a);
// Reinjection here
Foo foo = container.getComponent(Foo.class);
assert foo.getB() != null;
Any ideas for how to achieve this reinjection?
Many thanks,
Christopher
[1] This question was asked in a similar way in October
http://markmail.org/message/s4gqdtzpio4su7nw
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Loading...