Follow these steps to read SYSTEM
scoped or unscoped configuration values from
a Component that isn’t part of a portlet:
-
First set the
configurationPid
Component property as the fully qualified class name of the configuration class:@Component(configurationPid = "com.liferay.dynamic.data.mapping.form.web.configuration.DDMFormWebConfiguration")
-
Then provide an
activate
method, annotated with@Activate
to ensure the method is invoked as soon as the Component is started, and@Modified
so it’s invoked whenever the configuration is modified.@Activate @Modified protected void activate(Map<String, Object> properties) { _formWebConfiguration = ConfigurableUtil.createConfigurable( DDMFormWebConfiguration.class, properties); } private volatile DDMFormWebConfiguration _formWebConfiguration;
The
activate()
method calls the methodConfigurableUtil.createConfigurable()
to convert a map of the configuration’s properties to a typed class, which is easier to handle. The configuration is stored in avolatile
field. Don’t forget to make itvolatile
to prevent thread safety problems. -
Once the activate method is set up, retrieve particular properties from the configuration wherever they’re needed:
public void orderCar(String model) { order("car", model, _configuration.favoriteColor()); }
This is dummy code: don’t try to find it in the Liferay source code. The String configuration value of
favoriteColor
is passed to theorder
method call, presumably so that whatever model car is ordered gets ordered in the configured favorite color.
With very few lines of code, you have a configurable application that dynamically changes its configuration, has an auto-generated UI, and uses a simple API to access the configuration.