The Unbearable Lightness of Java

More wiring

More wiring topics.

Wiring external beans with container

All beans registered into the container will be wired. Wiring is lazy, i.e. it happens when some bean is requested (by bean name) for the first time in its scope. Petite then creates new bean instance and wires it.

All this happens for beans that are inside the container, i.e. registered. However, it is possible to wire any external object with the container context anytime during the runtime of the application.

PetiteContainer pc = ....
Foo foo = new Foo();
pc.wireBean(foo);

Petite will wire the Foo instance, but only using property and method injection (since bean is already created). Important is that Foo class is still not registered into the container. The only thing Petite stores is just some internal cache data, to speed up further injections for the same class.

It is possible to invoke init methods after wiring by setting second optional argument of wireBean() method to true.

Creating beans with container

Petite allows something more: to create the bean by container. This makes constructor injection possible, what was not available for simple wiring.

PetiteContainer pc = ....
Foo foo = pc.createBean(Foo.class);

Created beans are wired and init methods are invoked. However, created beans are not registered into the container.