Martin Larsson
2009-03-18 10:50:19 UTC
I'm just starting out with PicoContainer, so this might be a somewhat
confused question...
I tried searching the archives, but the search is apparently broken,
no matter what I typed
in, the result set was empty, even 'container' which is displayed with
a large font on the
archive page.
Anyways.
I have a class MyClass that depends on another Dependency. Adding them both to a
DefaultPicoContainer works nicely, and objects of MyClass is created easily.
In some special cases, I don't want Dependency, but rather it's subclass
SpecialDependency. So, I create a child container of my main
container, and add this
new class. However, asking the child container for a MyClass-object
still links it to
Dependency. To link it to SpecialDependency, I have to add MyClass to the child
container as well.
Is that really necessary? After all, I am asking the child container
for the object,
and it has all the new dependencies defined.
M.
Code fragments (all code attached):
public class MyClass {
private Dependency d;
public MyClass(Dependency d) {
this.d = d;
}
public String toString() {
return getClass().getName() + " - " + d.getClass().getName();
}
}
public class Start {
private void go() {
MutablePicoContainer pico;
MutablePicoContainer special_pico;
pico = new DefaultPicoContainer();
pico.addComponent(Dependency.class);
pico.addComponent(MyClass.class);
special_pico = pico.makeChildContainer();
special_pico.addComponent(Dependency.class, SpecialDependency.class);
// Should this be necessary?
//special_pico.addComponent(MyClass.class);
// This prints 'picotest.MyClass - picotest.Dependency' as expected
MyClass my = pico.getComponent(MyClass.class);
System.out.println(my);
// This only prints 'picotest.MyClass - picotest.SpecialDependency'
// when special_pico.addComponent(MyClass.class); above is
// uncommented.
my = special_pico.getComponent(MyClass.class);
System.out.println(my);
}
}
confused question...
I tried searching the archives, but the search is apparently broken,
no matter what I typed
in, the result set was empty, even 'container' which is displayed with
a large font on the
archive page.
Anyways.
I have a class MyClass that depends on another Dependency. Adding them both to a
DefaultPicoContainer works nicely, and objects of MyClass is created easily.
In some special cases, I don't want Dependency, but rather it's subclass
SpecialDependency. So, I create a child container of my main
container, and add this
new class. However, asking the child container for a MyClass-object
still links it to
Dependency. To link it to SpecialDependency, I have to add MyClass to the child
container as well.
Is that really necessary? After all, I am asking the child container
for the object,
and it has all the new dependencies defined.
M.
Code fragments (all code attached):
public class MyClass {
private Dependency d;
public MyClass(Dependency d) {
this.d = d;
}
public String toString() {
return getClass().getName() + " - " + d.getClass().getName();
}
}
public class Start {
private void go() {
MutablePicoContainer pico;
MutablePicoContainer special_pico;
pico = new DefaultPicoContainer();
pico.addComponent(Dependency.class);
pico.addComponent(MyClass.class);
special_pico = pico.makeChildContainer();
special_pico.addComponent(Dependency.class, SpecialDependency.class);
// Should this be necessary?
//special_pico.addComponent(MyClass.class);
// This prints 'picotest.MyClass - picotest.Dependency' as expected
MyClass my = pico.getComponent(MyClass.class);
System.out.println(my);
// This only prints 'picotest.MyClass - picotest.SpecialDependency'
// when special_pico.addComponent(MyClass.class); above is
// uncommented.
my = special_pico.getComponent(MyClass.class);
System.out.println(my);
}
}