Reading Unscoped Configuration Values from a Component

Follow these steps to read SYSTEM scoped or unscoped configuration values from a Component that isn’t part of a portlet:

  1. 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")
    
  2. 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 method ConfigurableUtil.createConfigurable() to convert a map of the configuration’s properties to a typed class, which is easier to handle. The configuration is stored in a volatile field. Don’t forget to make it volatile to prevent thread safety problems.

  3. 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 the order 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.

« Reading Unscoped Configuration Values from an MVC PortletCustomizing the Configuration User Interface »
¿Fue útil este artículo?
Usuarios a los que les pareció útil: 0 de 0