Is there a way to override application properties programmatically?

Viewed 209

As mentioned in the Quarkus documentation, config values can be read using

String databaseName = ConfigProvider.getConfig().getValue("database.name", String.class);
Optional<String> maybeDatabaseName = ConfigProvider.getConfig().getOptionalValue("database.name", String.class);

Is there any possibility to set an application property during runtime?

I want to set quarkus.hibernate-orm.database.default-schema during the startup of the application. This should happen programmatically (in Java code), so without the definition of the property from outside.

1 Answers

Yes, it is possible.

You can for example add:

package org.acme.config;

import org.eclipse.microprofile.config.spi.ConfigSource;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class InMemoryConfigSource implements ConfigSource {
    private static final Map<String, String> configuration = new HashMap<>();

    static {
        configuration.put("my.prop", "1234");
    }

    @Override
    public int getOrdinal() {
        return 275;
    }

    @Override
    public Set<String> getPropertyNames() {
        return configuration.keySet();
    }

    @Override
    public String getValue(final String propertyName) {
        return configuration.get(propertyName);
    }

    @Override
    public String getName() {
        return InMemoryConfigSource.class.getSimpleName();
    }
}

in your code and make it known to Quarkus using Java's Service Loader mechanism, by adding the src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource file containing:

org.acme.config.InMemoryConfigSource.

See this guide for more details.

Related