-
Notifications
You must be signed in to change notification settings - Fork 6
modules
There are two kinds of modules in Fathom, fathom.Module
and fathom.ServletsModule
.
Fathom modules are discovered by a Java ServiceLoader.
Subclass this module type to provide injectable services & components to your Fathom application.
Create a Java ServiceLoader definition file for your module...
META-INF/services/fathom.Module
com.package.MyModule
... and then define it.
public class MyModule extends Module {
@Override
protected void setup() {
// register MyService
bind(MyService.class);
}
}
Subclass this module type to provide injectable services & components, Filters, and Servlets.
Create a Java ServiceLoader definition file for your module...
META-INF/services/fathom.ServletsModule
com.package.MyServletsModule
... and then define it.
public class MyServletsModule extends ServletsModule {
@Override
protected void setup() {
// serve all requests with MyServlet
serve("/*").with(MyServlet.class);
// add a debug filter for dev mode
if (getSettings().isDev()) {
filter("/*").through(MyFilter.class);
}
// register MyService
bind(MyService.class);
}
}
Your module may need one or more settings to function and you may specify them as annotated requirements.
Each required setting must be present in the runtime profile configuration and must have a non-empty value otherwise the module will not be loaded.
@RequireSetting("myService.url")
@RequireSetting("myService.username")
@RequireSetting("myService.password")
public class MyModule extends Module {
@Override
protected void init() {
// register MyService
bind(MyService.class);
}
}
You might only want to load your module in a particular runtime mode. This is easily accomplished by using one or more of the mode-specific annotations: @DEV
, @TEST
, and @PROD
.
@DEV @TEST
public class DebugModule extends Module {
@Override
protected void init() {
// register DebugService
bind(DebugService.class);
}
}
Add your module to the classpath of your Fathom application.