Class SimpleInjector<T>

java.lang.Object
ch.tocco.nice2.toolbox.api.inject.SimpleInjector<T>
All Implemented Interfaces:
Injector<T>

public class SimpleInjector<T> extends Object implements Injector<T>
When instantiating, the first constructor where every argument can be found is used, where the order is from the most parameters to the least.

When initializing, first all setter methods are tried. Again the setters are only invoked if there is a argument value for every parameter. After this, all fields (which still null) are set if a value can be found. By default the type of the field/parameter is used to get the value from the ValueProvider. This can be overriden specifying the type using the

invalid reference
javax.annotation.Resource
annotation.

At last the object to initialize is checked for implementing

invalid reference
Initializable
. If so, the
invalid reference
Initializable#afterInitialize()
method is invoked.
  • Constructor Details

    • SimpleInjector

      public SimpleInjector(Class<T> clazz)
    • SimpleInjector

      public SimpleInjector()
  • Method Details

    • instantiate

      public T instantiate(ValueProvider provider)
      Description copied from interface: Injector
      Creates a new instance by searching for a ctor that can be matched using values from the specified value provider. The best constructor is used, which is the one with the greatest number of arguments that can be retrieved using the specified ValueProvider.
      Specified by:
      instantiate in interface Injector<T>
    • initialize

      public void initialize(T object, ValueProvider provider)
      First it uses Introspector to invoke all setter methods. Then all fields are queried and each one is initialized if it is null. At last if the object implements
      invalid reference
      Initializable
      , its
      invalid reference
      Initializable#afterInitialize()
      is called.
      Specified by:
      initialize in interface Injector<T>
    • injectField

      protected boolean injectField(T object, Field field, FieldIterable.FieldIterator iter)
      Check whether to set a field value. This will return true for non-final fields that don't have a value yet and only non-private fields of superclass(es).
    • setFields

      protected void setFields(T object, ValueProvider provider)
    • invokeSetter

      protected void invokeSetter(T owner, ValueProvider provider, BeanInfo info)
    • invokeSetMethod

      protected void invokeSetMethod(T owner, ValueProvider provider, PropertyDescriptor pdesc, Object curval, Class<?> beanClass)
    • getFieldValue

      protected final Object getFieldValue(Object target, Field field)
    • setFieldValue

      protected final void setFieldValue(Object target, Field field, Object value)
    • toString

      public String toString()
      Overrides:
      toString in class Object