jodd.petite
Class PetiteContainer

java.lang.Object
  extended by jodd.petite.PetiteContainer

public class PetiteContainer
extends java.lang.Object

Petite Container.


Field Summary
protected  java.util.Map<java.lang.Class,java.lang.Object[]> beanCtors
          Cache for constructors per type.
protected  java.util.Map<java.lang.String,jodd.petite.BeanDef> beanNames
          Map of all beans definitions.
protected  java.util.Map<java.lang.Class,java.lang.reflect.Field[]> beanReferences
          Cache of reference (i.e. annotated) fields for one type.
protected  java.lang.Class<? extends Scope> defaultScope
           
protected  java.util.Map<java.lang.Class<? extends Scope>,Scope> scopes
          Map of all bean scopes.
protected  boolean trackBeanWiring
           
 
Constructor Summary
PetiteContainer()
           
 
Method Summary
<E> E
acquireBean(java.lang.Class<E> type)
          Returns Petite bean instance.
 java.util.Iterator<java.lang.Class> classIterator()
          Returns iterator over all registered classes.
 java.lang.Object getBean(java.lang.String name)
          Returns Petite bean instance.
protected  java.lang.Object getBean(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> acquiredBeans)
          Returns petite bean instance.
 java.lang.Class<? extends Scope> getDefaultScope()
          Returns default scope type.
 int getTotalBeans()
          Returns total number of registered beans.
 int getTotalScopes()
          Return total number of used scopes.
 boolean isTrackBeanWiring()
           
protected  java.lang.Object newBeanInstance(java.lang.String name, java.lang.Class type, java.util.Map<java.lang.String,java.lang.Object> acquiredBeans)
          Creates new petite bean or type instance and performs constructor injection.
 void register(java.lang.Class type)
          Registers petite bean class.
 void register(java.lang.Class type, java.lang.Class<? extends Scope> scopeType)
          Registers petite bean class within specified scope.
 void register(java.lang.String name, java.lang.Class type)
          Registers any class as petite bean class with default scope.
 void register(java.lang.String name, java.lang.Class type, java.lang.Class<? extends Scope> scopeType)
          Registers any class as petite bean class.
protected  void registerBean(java.lang.String name, java.lang.Class type, java.lang.Class<? extends Scope> scopeType)
          Single point of Petite bean registration.
 boolean remove(java.lang.Class type)
          Removes petite bean from the container.
 boolean remove(java.lang.String name)
          Removes bean definition from the container.
 void replace(java.lang.Class newType)
          Replaces a class in the container.
protected  java.lang.String resolveBeanName(java.lang.Class type)
          Resolves bean name from bean annotation or class name.
 void setDefaultScope(java.lang.Class<? extends Scope> defaultScope)
          Sets default scope type.
 void setTrackBeanWiring(boolean trackBeanWiring)
          Stores additional information about targets where bean was wired in.
 void wire(java.lang.Object bean)
          Wires beans by injecting instances in properties marked with PetiteInject annotation.
protected  void wire(java.lang.Object bean, java.util.Map<java.lang.String,java.lang.Object> acquiredBeans)
          Wires beans by injecting instances in properties marked with PetiteInject annotation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

beanCtors

protected java.util.Map<java.lang.Class,java.lang.Object[]> beanCtors
Cache for constructors per type.


trackBeanWiring

protected boolean trackBeanWiring

beanReferences

protected java.util.Map<java.lang.Class,java.lang.reflect.Field[]> beanReferences
Cache of reference (i.e. annotated) fields for one type.


defaultScope

protected java.lang.Class<? extends Scope> defaultScope

beanNames

protected java.util.Map<java.lang.String,jodd.petite.BeanDef> beanNames
Map of all beans definitions.


scopes

protected java.util.Map<java.lang.Class<? extends Scope>,Scope> scopes
Map of all bean scopes.

Constructor Detail

PetiteContainer

public PetiteContainer()
Method Detail

getBean

public java.lang.Object getBean(java.lang.String name)
Returns Petite bean instance. Parameter is name of registered petite bean. Bean can be registered with explicit name using register(String, Class, Class) or by named generated from its class register(Class, Class) (from optional annotation or simple type name).

Petite container will find the bean in corresponding scope and all its dependencies, either by constructor or property injection. When using constructor injection, cyclic dependencies can not be prevented, but they are at least detected.

See Also:
acquireBean(Class)

getBean

protected java.lang.Object getBean(java.lang.String name,
                                   java.util.Map<java.lang.String,java.lang.Object> acquiredBeans)
Returns petite bean instance.

See Also:
acquireBean(Class)

acquireBean

public <E> E acquireBean(java.lang.Class<E> type)
Returns Petite bean instance. If class is not annotated, it creates a new instance of specified type and wires it with the container. This new instance is not registered and it is not attached to any scope (what is important for cyclic dependencies cases, where the same type is acquired later in the bean-graph).

Otherwise, when type is annotated, getBean(String) will be invoked.


newBeanInstance

protected java.lang.Object newBeanInstance(java.lang.String name,
                                           java.lang.Class type,
                                           java.util.Map<java.lang.String,java.lang.Object> acquiredBeans)
Creates new petite bean or type instance and performs constructor injection. This method may be called from acquireBean(Class) and getBean(String), therefore the first argument (bean name) also indicates if the bean is inside the container (when is not null).

After creating a bean, it will be added to acquired beans (if it is from the container).

Parameters:
name - optional bean name, if set then it is a name under which petite bean was registered
type - petite bean type
acquiredBeans - acquired beans until this moment

isTrackBeanWiring

public boolean isTrackBeanWiring()

setTrackBeanWiring

public void setTrackBeanWiring(boolean trackBeanWiring)
Stores additional information about targets where bean was wired in. This will lower performance and raise memory usage. This data are needed for run-time bean replacing.


wire

public void wire(java.lang.Object bean)
Wires beans by injecting instances in properties marked with PetiteInject annotation.

See Also:
wire(Object, java.util.Map)

wire

protected void wire(java.lang.Object bean,
                    java.util.Map<java.lang.String,java.lang.Object> acquiredBeans)
Wires beans by injecting instances in properties marked with PetiteInject annotation.


getDefaultScope

public java.lang.Class<? extends Scope> getDefaultScope()
Returns default scope type.


setDefaultScope

public void setDefaultScope(java.lang.Class<? extends Scope> defaultScope)
Sets default scope type.


register

public void register(java.lang.Class type)
Registers petite bean class. If class is not annotated, it will be registered with default scope.

See Also:
register(Class, Class), register(String, Class, Class)

register

public void register(java.lang.Class type,
                     java.lang.Class<? extends Scope> scopeType)
Registers petite bean class within specified scope. Class may be annotated, but this is not required. If annotation is omitted, bean will be registered with uncapitalized simple type name and defaults.

See Also:
register(String, Class, Class)

register

public void register(java.lang.String name,
                     java.lang.Class type)
Registers any class as petite bean class with default scope.

See Also:
register(String, Class, Class)

register

public void register(java.lang.String name,
                     java.lang.Class type,
                     java.lang.Class<? extends Scope> scopeType)
Registers any class as petite bean class. PetiteBean annotation is not required and is ignored during this registration. Bean names must be unique.


registerBean

protected void registerBean(java.lang.String name,
                            java.lang.Class type,
                            java.lang.Class<? extends Scope> scopeType)
Single point of Petite bean registration.


remove

public boolean remove(java.lang.String name)
Removes bean definition from the container. Returns true if bean specified by provided name was removed from the container, otherwise it returns false.

See Also:
remove(Class)

remove

public boolean remove(java.lang.Class type)
Removes petite bean from the container. Returns true if bean specified by provided name was removed from the container, otherwise it returns false.

See Also:
remove(String)

resolveBeanName

protected java.lang.String resolveBeanName(java.lang.Class type)
Resolves bean name from bean annotation or class name.


replace

public void replace(java.lang.Class newType)
Replaces a class in the container. It will find all targets where original class has been injected and will replace existing instances, while storing them in appropriate scopes.


getTotalBeans

public int getTotalBeans()
Returns total number of registered beans.


getTotalScopes

public int getTotalScopes()
Return total number of used scopes.


classIterator

public java.util.Iterator<java.lang.Class> classIterator()
Returns iterator over all registered classes.



Copyright ©2008 Jodd Team