Skip to content

Dependency Resolving

Mihail Kuznetsov edited this page Mar 16, 2015 · 2 revisions

Dependency resolving

Dependency resolving

Dependency injection is resolved by DependencySupplier. Under servlet container default imlementation of DependencySupplier is org.everrest.core.servlet.ServletContextDependencySupplier. It is sub-class of org.everrest.core.BaseDependencySupplier.

public abstract class BaseDependencySupplier implements DependencySupplier
{
   protected final Class<? extends Annotation> injectAnnotation;

   public BaseDependencySupplier(Class<? extends Annotation> injectAnnotation)
   {
      this.injectAnnotation = injectAnnotation;
   }

   public BaseDependencySupplier()
   {
      this(javax.jnject.Inject.class);
   }

   public final Object getComponent(Parameter parameter)
   {
      if (parameter instanceof FieldInjector)
      {
         for (Annotation a : parameter.getAnnotations())
         {
            if (a.annotationType() == injectAnnotation)
            {
               return getComponent(parameter.getParameterClass());
            }
         }
         return null;
      }
      return getComponent(parameter.getParameterClass());
   }
}

BaseDependencySupplier requires to field to has 'inject annotation' but not need to have such annotation for constructor parameter. By default inject annotation is @javax.jnject.Inject. You can easy to use own annotation for injection fields. Example:

EverrestProcessor processor = new EverrestProcessor(new ResourceBinderImpl(),
   new ApplicationProviderBinder(),
   new DependencySupplierImpl(MyInject.class),
   new EverrestConfiguration(),
   new Application() {
   ...
   });