-
Notifications
You must be signed in to change notification settings - Fork 0
Dependency Injection
A dependency is a value identified by a unique identifier. In order to reduce the coupling between classes, it could be efficient to inject dependencies into a controller, especially since — as JavaFX users — we delegate the instanciation of controllers to the framework. Such dependencies can be :
- a context variable,
- a logger,
- etc.
With PlayFX, dependencies can be specified in three different ways. In order of priority :
- configuration files,
- command-line properties,
- factory.
The term "priority" means that, if different values are specified for a same dependency, PlayFX will try to inject them starting with the one associated with the highest priority until one has been successfuly injected.
As of now, PlayFX injects only controllers' fields.
In order to be injectable, a field must be annotated with fr.kazejiyu.playfx.injection.Inject
.
By default, the dependency to inject is identified thanks to the name of the field. However, if one needs a smoother behaviour, the dependency's identifier can be precised with the name
attribute of Inject
's annotation. In the later case, the field's name is irrevelant.
For instance, take the following code:
@Inject(name="user.name")
private String userName = "unknown";
The field userName
should be injected with a dependency called "userName". Nevertheless, thanks to the @Inject
annotation, it will be injected with the dependency "user.name".
In this case, "user.name" likely relates to the property describing the name of the user. If the property is unset in the running system, the field will keep its default value that is "unknown".
Configuration files are files called config.properties
and that rely in the same folders than JavaFX's controllers.
Primitive dependencies can be specified into standard properties files. Such files must be formatted as pairs key=value
.
For example, the following file defines three dependencies configuring the logging:
# Logging
log.level=DEBUG
log.output=/tmp/soft.log
log.format=[%level%] %date%: %message%
PlayFX can also inject values from environment properties. Such properties can be specified directly with the command line that launches the JavaFX application :
java -Dport=1234 -jar server-fx.jar
Finally, PlayFX can be supplied with a factory in the form of a function String -> Object
. Given a dependency's name, this function is responsible of generating the associated value. Typically, this method can be the get
of a Map
instance :
Map <String,Object> dependencies = new HashMap<>();
dependencies.put("user", new User());
dependencies.put("rand", new Random());
Play play = new Play(stage, dependencies::get);
A Function
is used instead of a Map
in order to make PlayFX more generic.
PlayFX uses an instance of java.util.Logger
to log its errors.
Hence, if a field cannot be injected a message will be sent to this logger. More informations can be found in the dedicated page.