Rev 40 |
Rev 45 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
package org.mentacontainer;
/**
* A very simple container that provides:
* <ul>
* <li> Bean instantiation (duh!)</li>
* <li> Constructor injection for bean setup</li>
* <li> Setter injection for bean setup</li>
* <li> Auto-wiring based on name and type</li>
* <li> Singleton</li>
* <li> Wiring of external beans with the beans configured in this container</li>
* <li> Annotation and XML free (programmatic configuration is the way to go!)
* </ul>
*
* It does not get much simpler than that.
*
* @author sergio.oliveira.jr@gmail.com
*
*/
public interface Container {
/**
* Get an instance from the container.
*
* The instance will be fully initialized (constructor and/or setters) and fully wired.
*
* @param key The key representing the bean to return. The name of the bean in the container.
* @return The fully initialized and wired bean.
*/
public <T
> T get
(String key
);
/**
* Configure a bean to be returned with the given implementation when {@link #get(String)} is called.
*
* @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 singleton A boolean to indicate if this bean will be a singleton.
* @return The component created. (Fluent API)
*/
public Component ioc
(String key,
Class<? extends Object> klass,
boolean singleton
);
/**
* Same as {@link #ioc(String, Class, boolean)} except that it assumes
* singleton is false.
*
* @param key
* @param klass
* @return The component created. (Fluent API)
*/
public Component ioc
(String key,
Class<?extends Object> klass
);
/**
* Set up IoC based on the component passed.
*
* @param key The key representing the bean to return. The name of the bean in the container.
* @param component The component for the IoC.
* @return The component passed as a parameter. (Fluent API)
* @see Component
*/
public Component ioc
(String key,
Component component
);
/**
* Configure a bean dependency to be auto-wired by the container. In general you want the
* type of the dependency to be an interface, for loosely couple dependencies. It works like that:<br/><br/>
*
* Whenever the container returns a bean, it checks to see if it has a property named <i>property</i>
* and if the type of the property is <i>klass</i>. If it does, then it looks for a bean named
* <i>source</i> and injects it inside the first bean it is returning. This approach is recursive
* so all properties are checked up the class hierarchy, until it reaches Object.
*
* @param property a bean property that will require another bean, in other words, the required
* bean will be injected in the property of the bean been requested from the container. (auto-wiring by name)
* @param klass the type of the dependency, in other words, the type of the auto-wiring. (auto-wiring by type)
* @param source The dependency itself, coming from the container as well, in other words, the bean that will be injected in the original bean
* @return The container itself. (Fluent API)
*/
public Dependency autowire
(String property,
Class<? extends Object> klass,
String source
);
/**
* Same as {@link #autowire(String, Class, String)} except that it assumes that the property name will be the source name, in other words,
* the property name is the same as the bean name that will be injected as the dependency.
*
* @param property
* @param klass
* @return The container itself. (Fluent API)
*/
public Dependency autowire
(String property,
Class<? extends Object> klass
);
/**
* Setup a dependency.
*
* @param dependency The dependency to setup
* @return The dependency itself. (Fluent API)
* @see Dependency
*/
public Dependency autowire
(Dependency dependency
);
/**
* Take a given bean and populate its properties with other beans coming from this container. Perhaps you can call this auto-wiring.
* You basically checking properties of the given bean and looking for values (by name and type!) inside the container. And injecting
* in the given bean, in other words, populating it.
*
* @param bean The bean to be populated with other beans from the container.
* @return The container itself. (Fluent API)
*/
public Container populate
(Object bean
);
/**
* Check whether the container is configured to provide a bean with name 'key'.
*
* @param key The key representing the bean inside the container.
* @return true if the container contains this bean.
*/
public boolean contains
(String key
);
}