Usage Manual
Using The Container As a Factory, And No More "new" Statements!
In this first example we learn how to obtain class instances from a Gamba Container, and how it
decouples your component
... [More]
implementation from each others.
Everybody knows that hardcoding is a bad practice... but whenever you place a new statement in your
code, you are really taking a concrete class implementation in a hardcoded way! For example, supposing
that you wants for a java.util.List implementation instance, and you choose a java.util.ArrayList:
List list = (List) new ArrayList();Well, but if you want to choose another implementation for a List, you will have to change all your
classes that news it. Lechuga solves this problem, since the concrete implementation class is
uniquely declared in a context file as is:
After declaring the class implementation, you can obtain an instance of this, calling the
Gamba Container:
GambaContext gc = GambaContainer.getContext("tips-context.xml");
List myList = (List) gc.getBean("myList");
Assert.assertTrue(myList instanceof List);
Now, if you want to choose another implementation for a List (i.e. LinkedList), you only must have
to change the class specified in a context file, and all your classes that instantiates it will obtain
the new implementation:
Dependency Injection
Dependency Injection means to inject recursively into your requested bean instance his declared dependencies.
This feature allows you to obtain a desired bean instance (obtained by a Factory, in fact), fully configured
and ready to use, altought his object-dependencies are being injected.
Setter Injection
For example, suppose that you have in hands:
In the above example, a "b" instance are being injected to an "a" instance using setter injection.
When you asks the container for an "a" instance, the container works for you and performs reflect operations
to do the same as the following piece of code:
A a = new A();
B b = new B();
a.setB(b);
return a;
Constructor Injection
A constructor injection is another kind of injection. For example the definition:
will cause that when a "b" instance is requested, container performs:
A a = new A();
B b = new B(a);
return b;Gamba Container Don't Works Lazily, just Eagerly
In order to prevent unexpected exception throwing at run-time, all bean definitions are parsed and loaded
in context loading-time (that is, when you calls GambaContainer.getContext(...)), performing a check
of all defined beans, thinking about her future life-cycle.
--> [Less]