Interface InstancioServiceProvider
- generator mappings via
getGeneratorProvider()
- subtype mappings via
getTypeResolver()
- class instantiation logic via
getTypeInstantiator()
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 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
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
Provides customGenerator
classes.static interface
Provides custom instantiation logic.static interface
Resolves subtype based on a given class. -
Method Summary
Modifier and TypeMethodDescriptionReturns aGeneratorProvider
implementation.Returns aTypeInstantiator
implementation.Returns aTypeResolver
implementation.default void
init
(ServiceProviderContext context) An optional method that can be used by implementations that require access to the context information.
-
Method Details
-
init
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
Returns aGeneratorProvider
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 customPhone
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 theGeneratorProvider
. Once registered, thePerson
can be created without specifying the generator manually:Person person = Instancio.create(Person.class); // uses Phone generator
- Returns:
- a custom generator provider, or
null
if not required - Since:
- 2.11.0
-
getTypeResolver
Returns aTypeResolver
implementation.- Returns:
- a custom type resolver, or
null
if not required - Since:
- 2.11.0
-
getTypeInstantiator
Returns aTypeInstantiator
implementation.- Returns:
- a custom type instantiator, or
null
if not required - Since:
- 2.11.0
-