Interface InstancioServiceProvider


public interface InstancioServiceProvider
Instancio Service Provider Interface for providing custom:

All of the above are default methods that return null. Implementations of this class can override one or more methods as needed. Instancio will invoke each of the above methods only once, therefore, these methods may contain initialisation logic.

This class uses the ServiceLoader mechanism. Therefore, implementations must be registered explicitly.

  • for a module using the Java module system by adding provides org.instancio.spi.InstancioServiceProvider with fully.qualified.ImplementationName;
  • otherwise in non-modular code or when read by non-modular code, by placing a file named org.instancio.spi.InstancioServiceProvider under /META-INF/services; The file must contain the fully-qualified name of the implementing class.

Note: only InstancioServiceProvider (and not the interfaces defined within it) can be registered via the ServiceLoader.

Since:
2.11.0
  • Method Details

    • init

      @ExperimentalApi default void init(ServiceProviderContext context)
      An optional method that can be used by implementations that require access to the context information.
      Parameters:
      context - containing additional information
      Since:
      2.12.0
    • getGeneratorProvider

      default InstancioServiceProvider.GeneratorProvider getGeneratorProvider()
      Returns a GeneratorProvider implementation.

      Instancio will automatically use generators configured using the provider. As an example, consider the following use-case where Person objects need to be created with valid phone numbers. To achieve this, a custom Phone generator is supplied:

      
         Generator<Phone> phoneGenerator = createPhoneGenerator();
      
         Person person = Instancio.of(Person.class)
            .supply(all(Phone.class), phoneGenerator)
            .create();
       

      To avoid specifying the generator manually, the Phone generator can be registered using the GeneratorProvider. Once registered, the Person can be created without specifying the generator explicitly:

      
         Person person = Instancio.create(Person.class); // uses Phone generator
       
      Returns:
      a custom generator provider, or null if not required
      Since:
      2.11.0
    • getTypeResolver

      default InstancioServiceProvider.TypeResolver getTypeResolver()
      Returns a TypeResolver implementation.
      Returns:
      a custom type resolver, or null if not required
      Since:
      2.11.0
    • getTypeInstantiator

      default InstancioServiceProvider.TypeInstantiator getTypeInstantiator()
      Returns a TypeInstantiator implementation.
      Returns:
      a custom type instantiator, or null if not required
      Since:
      2.11.0
    • getSetterMethodResolver

      Returns a SetterMethodResolver implementation.
      Returns:
      a custom setter method resolver, or null if not required
      Since:
      3.2.0
    • getAnnotationProcessor

      Returns an AnnotationProcessor implementation.
      Returns:
      a custom annotation processor, or null if not required
      Since:
      4.5.0