Skip to content

Customized injection

Daan van Yperen edited this page May 30, 2019 · 7 revisions

Customized injection

This article describes how to extend/override how dependency injection works. If you just want to inject your own object, see Injecting-non-artemis-dependencies instead.

By default, the World injects dependencies during construction. This is done by an Injector registered with the WorldConfiguration. If greater control is needed, you can provide your own Injector with a customized FieldHandler.

Example

The below code creates a FieldHandler which will contain two field FieldResolvers: ArtemisFieldResolver and CustomFieldsResolver. In this case, the WiredFieldResolver is not part of the world configuration, so unless the CustomFieldsResolver handles fields annotated with @Wire, these fields will not be injected. Systems, Managers, ComponentMappers & EntityFactories will still be injected.

FieldHandler fieldHandler = new FieldHandler(new InjectionCache());
fieldHandler.addFieldResolver(new CustomFieldsResolver());

Injector injector = new CachedInjector().setFieldHandler(fieldHandler);
World world = new World(new WorldConfiguration().setInjector(injector));
Clone this wiki locally