Class ProvidesListener

java.lang.Object
org.glassfish.hk2.extras.provides.ProvidesListener
All Implemented Interfaces:
org.glassfish.hk2.api.DynamicConfigurationListener

@Singleton public class ProvidesListener extends Object implements org.glassfish.hk2.api.DynamicConfigurationListener
Enables the Provides annotation.

Only one ProvidesListener instance may be registered with each ServiceLocator.

  • Constructor Details

    • ProvidesListener

      @Inject public ProvidesListener(org.glassfish.hk2.api.ServiceLocator locator)
  • Method Details

    • getFilter

      protected org.glassfish.hk2.api.Filter getFilter()
      Matches services whose implementation classes are scanned for Provides annotations.

      The default filter matches all services. Override this method to permit a smaller subset of services. For example:

         @Singleton
         public class FilteredProvidesListener extends ProvidesListener {
           @Inject
           public FilteredProvidesListener(ServiceLocator locator) {
             super(locator);
           }
      
           @Override
           protected Filter getFilter() {
             return d -> d.getImplementation().startsWith("com.example.");
           }
         }
       
    • onInvalidProvidesAnnotation

      protected void onInvalidProvidesAnnotation(org.glassfish.hk2.api.ActiveDescriptor<?> providerDescriptor, Provides providesAnnotation, AnnotatedElement annotatedElement, String message)
      Called when an invalid Provides annotation is found.

      The default implementation of this method does nothing. Override this method to consume the reported information. For example:

         @Singleton
         public class LoggingProvidesListener extends ProvidesListener {
           private final Logger logger = LoggerFactory.getLogger(getClass());
      
           @Inject
           public LoggingProvidesListener(ServiceLocator locator) {
             super(locator);
           }
      
           @Override
           protected void onInvalidProvidesAnnotation(
               ActiveDescriptor<?> providerDescriptor,
               Provides providesAnnotation,
               AnnotatedElement annotatedElement,
               String message) {
      
             logger.error(message);
           }
         }
       

      If this method does not throw an exception, then after calling this method, this listener will ignore this Provides annotation — registering no new ActiveDescriptor on its behalf — and this listener will continue to scan this provider and other providers for other Provides annotations.

      If this method does throw an exception, then the behavior of this listener from this point forward is undefined.

      Parameters:
      providerDescriptor - the descriptor of the service that contains this Provides annotation
      providesAnnotation - the Provides annotation on the method or field
      annotatedElement - the method or field that is annotated with Provides
      message - a message explaining why this Provides annotation is invalid
    • configurationChanged

      public void configurationChanged()
      Specified by:
      configurationChanged in interface org.glassfish.hk2.api.DynamicConfigurationListener