MentaContainer

Compare Revisions

Ignore whitespace Rev 94 → Rev 95

/trunk/src/test/java/org/mentacontainer/impl/GenericFactoryTest.java
19,7 → 19,7
}
@Test
public void testGenericComponent() throws Exception {
public void testGenericFactory() throws Exception {
// in real life this will be a SessionFactory, a ConnectionPool or any factory...
SomeFactory factory = new SomeFactory();
/trunk/src/test/java/org/mentacontainer/impl/MentaContainerTest.java
137,7 → 137,7
// if you don't provide a scope the Scope is NONE
// meaning a new instance is created on every
// request for the component
// request for the bean
c.ioc("myString", String.class).addInitValue("saoj");
274,7 → 274,7
"connection" /* the source inside the container */);
c.autowireBySetter("myDep" /* notice this is the setter of the Connection class */,
"aDependency" /* again this is the name of the bean/component inside the container */);
"aDependency" /* again this is the name of the factory inside the container */);
return c;
}
354,7 → 354,7
public boolean isCleared() { return cleared; }
}
private static class SomeComponent implements Factory, Interceptable<SomeObject> {
private static class SomeFactory implements Factory, Interceptable<SomeObject> {
@Override
public <T> T getInstance() {
386,7 → 386,7
Container c = new MentaContainer();
c.ioc("o", new SomeComponent(), Scope.SINGLETON);
c.ioc("o", new SomeFactory(), Scope.SINGLETON);
SomeObject o = c.get("o");
394,7 → 394,7
Assert.assertEquals(true, o.isCleared());
c.ioc("o", new SomeComponent(), Scope.THREAD);
c.ioc("o", new SomeFactory(), Scope.THREAD);
o = c.get("o");
406,7 → 406,7
Assert.assertEquals(true, o.isCleared());
c.ioc("o", new SomeComponent(), Scope.NONE);
c.ioc("o", new SomeFactory(), Scope.NONE);
o = c.get("o");
414,7 → 414,7
Assert.assertNull(o);
c.ioc("o", new SomeComponent(), Scope.THREAD);
c.ioc("o", new SomeFactory(), Scope.THREAD);
o = c.clear("o");
/trunk/src/test/java/org/mentacontainer/impl/WrapperFactoryTest.java
9,7 → 9,7
public class WrapperFactoryTest {
@Test
public void testInstanceComponent() throws Exception {
public void testInstanceFactory() throws Exception {
String s = new String("saoj");
/trunk/src/main/java/org/mentacontainer/Interceptable.java
1,13 → 1,13
package org.mentacontainer;
 
/**
* Some components can also implement this interface to perform some cleanup
* Some factories can also implement this interface to perform some cleanup
* when the instance is cleared. For example, a connection pool will want
* to know when the connection instance is cleared so it can return it to
* the pool.
*
* It makes more sense to use this interface for components that will be placed
* in the THREAD scope, but you can also use it with components in the SINGLETON
* It makes more sense to use this interface for factories that will be placed
* in the THREAD scope, but you can also use it with factories in the SINGLETON
* scope.
*
* This is particular useful for the THREAD scope for dealing with thread pools, so
/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
396,9 → 396,9
}
@Override
public Factory ioc(String key, Factory component) {
public Factory ioc(String key, Factory factory) {
return ioc(key, component, Scope.NONE);
return ioc(key, factory, Scope.NONE);
}
@Override
/trunk/src/main/java/org/mentacontainer/Container.java
36,8 → 36,8
*
* @param key The key representing the bean to return. The name of the bean in the container.
* @param klass The class used to instantiate the bean, in other words, its implementation.
* @param scope The scope of the component.
* @return The component created as a ConfigurableComponent. (Fluent API)
* @param scope The scope of the factory.
* @return The factory created as a Configurablefactory. (Fluent API)
* @see Scope
*/
public ConfigurableFactory ioc(String key, Class<? extends Object> klass, Scope scope);
48,7 → 48,7
*
* @param key
* @param klass
* @return The component created as a ConfigurableComponent. (Fluent API)
* @return The factory created as a Configurablefactory. (Fluent API)
* @see Scope
*/
public ConfigurableFactory ioc(String key, Class<?extends Object> klass);
57,23 → 57,23
* Set up IoC based on the factory passed. The scope assumed is NONE.
*
* @param key The key representing the bean to return. The name of the bean in the container.
* @param factory The component for the IoC.
* @return The component passed as a parameter.
* @param factory The factory for the IoC.
* @return The factory passed as a parameter.
* @see Factory
*/
public Factory ioc(String key, Factory factory);
/**
* Set up IoC based on the component passed. Specify the scope of the component.
* Set up IoC based on the factory passed. Specify the scope of the factory.
*
* @param key The key representing the bean to return. The name of the bean in the container.
* @param component The component for the IoC.
* @param scope The scope used by the component.
* @return The component passed as a parameter.
* @param factory The factory for the IoC.
* @param scope The scope used by the factory.
* @return The factory passed as a parameter.
* @see Factory
* @see Scope
*/
public Factory ioc(String key, Factory component, Scope scope);
public Factory ioc(String key, Factory factory, Scope scope);
/**
* Configure a bean dependency to be auto-wired by the container. In general you want the
/trunk/src/main/java/org/mentacontainer/Factory.java
1,7 → 1,7
package org.mentacontainer;
 
/**
* An IoC component that knows how to create instances of itself.
* An IoC factory that knows how to create instances of itself.
*
* @author sergio.oliveira.jr@gmail.com
*/
/trunk/src/main/java/org/mentacontainer/ConfigurableFactory.java
1,7 → 1,7
package org.mentacontainer;
 
/**
* An IoC component that knows how to create instances of itself and
* An IoC factory that knows how to create instances of itself and
* can be configured by accepting values for its constructor
* and properties for its setters. It can also be intercepted right
* after it is created/wired and right before it is destroyed through
16,18 → 16,18
* use constructors with more than one argument.
*
* @param value A parameter value to be used by a constructor.
* @return The component itself. (Fluent API)
* @return The factory itself. (Fluent API)
*/
public ConfigurableFactory addInitValue(Object value);
public ConfigurableFactory addInitPrimitive(Object value);
/**
* Add a property to be injected through a setter when the component is instantiated.
* Add a property to be injected through a setter when the factory is instantiated.
*
* @param name The property name.
* @param value The property value.
* @return The component itself. (Fluent API)
* @return The factory itself. (Fluent API)
*/
public ConfigurableFactory addPropertyValue(String name, Object value);
/trunk/src/main/java/org/mentacontainer/Scope.java
3,17 → 3,17
public enum Scope {
/**
* The container calls component.get() on every request for the component, returning always new instances.
* The container calls factory.getInstance on every request for the bean, returning always new instances.
*/
NONE,
/**
* The container calls component.get() only once and caches the value, returning always the same instance.
* The container calls factory.getInstance only once and caches the value, returning always the same instance.
*/
SINGLETON,
/**
* The container calls component.get() and caches the value on a thread local, so the same thread will always get the same instance.
* The container calls factory.getInstance and caches the value on a thread local, so the same thread will always get the same instance.
*/
THREAD
}