Daniel Hinojosa
2008-04-24 23:32:18 UTC
I wish to setup a previously developed Swing app using an IoC
container and I like PicoContainer more because of the ability to use
child containers.
In most of the examples on the website though, they are simple (which
is nice). The child container is always created right after the parent
container is created. The example doesn't cover what to do in case
you wish to create a child container based off of a root container
that has been configured in another class all while keeping everything
decoupled.
I wanted my solution to be simple, so I came up with the following
code, and it works GREAT! My question is, given my code, is anything
I did here an anti-pattern or can you foresee and issues?
public class MyApplication {
public MyApplication() {
}
public static void main(String[] args) {
MutablePicoContainer pico = new DefaultPicoContainer();
pico.addComponent("rootContainer", pico);
pico.addComponent(MyChildApp.class);
pico.addComponent(MyApplication.class);
MyChildApp app = pico.getComponent(MyChildApp.class);
app.doIt();
}
}
public class MyChildApp {
private MutablePicoContainer rootContainer;
@Inject
public void setRootContainer(MutablePicoContainer rootContainer) {
this.rootContainer = rootContainer;
}
public void doIt() {
System.out.println(rootContainer);
MutablePicoContainer childContainer =
rootContainer.makeChildContainer();
childContainer.addComponent("value", 14);
childContainer.addComponent(MyGrandchildApp.class);
MyGrandchildApp myGrandchildApp =
childContainer.getComponent(MyGrandchildApp.class);
myGrandchildApp.doIt();
}
}
public class MyGrandchildApp {
private MutablePicoContainer rootContainer;
private int value;
@Inject
public void setRootContainer(MutablePicoContainer rootContainer) {
this.rootContainer = rootContainer;
}
@Inject
public void setValue(int value) {
this.value = value;
}
public void doIt() {
System.out.println("rootContainer: " + rootContainer);
System.out.println("value: " + value);
}
}
Thanks for your help,
Danno
--
Daniel Hinojosa
Programmer, Instructor, and Consultant
2529 Wisconsin St. NE
Albuquerque, NM 87110
dhinojosa-sRlC7LJHFRkitjMjQMKR8gC/***@public.gmane.org
http://www.evolutionnext.com
http://www.abqjug.org
(505) 363-5832
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
container and I like PicoContainer more because of the ability to use
child containers.
In most of the examples on the website though, they are simple (which
is nice). The child container is always created right after the parent
container is created. The example doesn't cover what to do in case
you wish to create a child container based off of a root container
that has been configured in another class all while keeping everything
decoupled.
I wanted my solution to be simple, so I came up with the following
code, and it works GREAT! My question is, given my code, is anything
I did here an anti-pattern or can you foresee and issues?
public class MyApplication {
public MyApplication() {
}
public static void main(String[] args) {
MutablePicoContainer pico = new DefaultPicoContainer();
pico.addComponent("rootContainer", pico);
pico.addComponent(MyChildApp.class);
pico.addComponent(MyApplication.class);
MyChildApp app = pico.getComponent(MyChildApp.class);
app.doIt();
}
}
public class MyChildApp {
private MutablePicoContainer rootContainer;
@Inject
public void setRootContainer(MutablePicoContainer rootContainer) {
this.rootContainer = rootContainer;
}
public void doIt() {
System.out.println(rootContainer);
MutablePicoContainer childContainer =
rootContainer.makeChildContainer();
childContainer.addComponent("value", 14);
childContainer.addComponent(MyGrandchildApp.class);
MyGrandchildApp myGrandchildApp =
childContainer.getComponent(MyGrandchildApp.class);
myGrandchildApp.doIt();
}
}
public class MyGrandchildApp {
private MutablePicoContainer rootContainer;
private int value;
@Inject
public void setRootContainer(MutablePicoContainer rootContainer) {
this.rootContainer = rootContainer;
}
@Inject
public void setValue(int value) {
this.value = value;
}
public void doIt() {
System.out.println("rootContainer: " + rootContainer);
System.out.println("value: " + value);
}
}
Thanks for your help,
Danno
--
Daniel Hinojosa
Programmer, Instructor, and Consultant
2529 Wisconsin St. NE
Albuquerque, NM 87110
dhinojosa-sRlC7LJHFRkitjMjQMKR8gC/***@public.gmane.org
http://www.evolutionnext.com
http://www.abqjug.org
(505) 363-5832
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email