public class DefaultControllerFactory extends Object
This class supports a basic field injection using @Inject
annotation. It can be used to initialize and
configure controller, model or service instances, and bind them together.
For every call of createController(Class)
method - a new controller instance is created, but all the
injected dependencies (models, services, etc) are instantiated only once, stored in cache and used as singletons.
The factory first makes an attempt to match field to be injected with System properties and then with optional
properties provider
. The matching is done using @Named
annotation if present, otherwise using field's name.
If no matching property is found, the factory consults dependencies cache trying find a match with the field by
exact type. Otherwise it requests instance provider
to supply a new
instance of given type, injects all its dependencies and puts into the cache.
For primitive and enum
fields, the class makes a conversion when necessary e.g. when an integer field's
value is injected from a System property (String).
Example usage:
class PersonModel { @Inject @Named("person.age.visible") boolean defaultShowAge; @Inject Side personDetailsPaneSide; } class PersonService { Person findByName(String name) { // ... } } class PersonController { @Inject PersonModel model; @Inject PersonService service; @FXML void initialize() { // Initialize GUI with data from service, bind controls with model, etc. } }
The class is not synchronized. If multiple threads access it concurrently, it must be synchronized externally.
Constructor and Description |
---|
DefaultControllerFactory() |
Modifier and Type | Method and Description |
---|---|
void |
clearDependencies()
Clears dependencies cache.
|
<T> T |
createController(Class<T> controllerClass)
Creates a new instance of the given controller class.
|
Function<Class<?>,Object> |
getInstanceProvider()
Returns the currently used instance provider.
|
Function<String,Object> |
getPropertiesProvider()
Returns currently used properties provider.
|
void |
setDependency(Class<?> type,
Object dependency)
Stores given dependency in a cache.
|
void |
setInstanceProvider(Function<Class<?>,Object> instanceProvider)
Sets the specified instance provider to be used by the factory to retrieve instances of necessary types.
|
void |
setLogger(Consumer<String> logger)
Installs logger receiving debug information.
|
void |
setPropertiesProvider(Function<String,Object> propertiesProvider)
Sets properties provider to be used.
|
public <T> T createController(Class<T> controllerClass)
T
- controller typecontrollerClass
- class of the FXML controller used to locate the FXML filepublic void setDependency(Class<?> type, Object dependency)
type
- class of the dependency used for matching with injectable fieldsdependency
- the instance to be injected into fields of specified typeNullPointerException
- if the type
is null
public void clearDependencies()
public Function<Class<?>,Object> getInstanceProvider()
public void setInstanceProvider(Function<Class<?>,Object> instanceProvider)
By default initialized to a provider creating new instances using default constructor.
instanceProvider
- the instance providerNullPointerException
- if given provider is null
public Function<String,Object> getPropertiesProvider()
public void setPropertiesProvider(Function<String,Object> propertiesProvider)
Example:
Map<String, Object> props = new HashMap<>();
props.put("pool.size", 5);
setPropertiesProvider(props::get);
propertiesProvider
- provider of properties (to be injected)Copyright © 2019 CERN. All rights reserved.