You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

Intro

This module is used to set global configuration on application. This is achieved by generating class with configuration (GlobalConfiguration)

For generation, a special utility project was created with the name configuration-generator

https://github.com/lightbend/config


In version 0.0.9 added support for generating yaml files. Also retained support for the old format





Run from CLI 

To generate GlobalConfiguration class you need open a terminal and enter this command:

cmd/app generate cfg

All comands related to generation you can find in this file cmd/app




Flow

In resources folder are stored configuration files

Default file is a application.yaml/global.conf

During launching spring application according on all active profile all config/yaml files merge to GlobalConfiguration instance and save as a map to spring property source (it happens in PropertySourcesApplicationContextInitializer)

After that according on property source initialize GlobalConfiguration bean (GlobalConfigurationProvider)

Here is an example how the files are located:




Usage example 

First you need to fill in the configuration data

application.yaml
postgres:
  enabled: false
  name: "POSTGRES"
  driverClassName: "org.postgresql.Driver"
  jdbcUrl: "jdbc:postgresql://localhost:5432/playground_db"
  locationMigration: "classpath:db/migration/postgres"
  propertyPath: "postgres.jdbcUrl"
  users:
    runtime: "playground"
    flyway: "playground"
  passwords:
    runtime: "playground"
    flyway: "playground"
  hikari:
    connectionTimeout: 45000
    idleTimeout: 60000
    maxLifetime: 180000
    maximumPoolSize: 50
    minimumIdle: 5
    connectionInitSql: "SELECT 1"
    connectionTestQuery: "SELECT 1"
    poolName: "core-postgres-db-pool"
    autoCommit: true


After generation, this class with can be reused throughout the project

GlobalConfiguration
@Getter
@Setter
@ConfigurationProperties(prefix = "postgres")
public static class Postgres {
    private Users users;
    private Passwords passwords;
    private Hikari hikari;
    private Boolean enabled;
    private String name;
    private String driverClassName;
    private String jdbcUrl;
    private String locationMigration;
    private String propertyPath;

    public Users users() {
        return users;
    }

    public Passwords passwords() {
        return passwords;
    }

    public Hikari hikari() {
        return hikari;
    }

    public Boolean enabled() {
        return enabled;
    }

    public String name() {
        return name;
    }

    public String driverClassName() {
        return driverClassName;
    }

    public String jdbcUrl() {
        return jdbcUrl;
    }

    public String locationMigration() {
        return locationMigration;
    }

    public String propertyPath() {
        return propertyPath;
    }


Example of use in flyway configuration:

GlobalConfiguration.Postgres postgres = globalConfiguration.postgres();
FluentConfiguration fluentConfiguration = Flyway.configure()
        .dataSource(postgres.jdbcUrl(), postgres.users().flyway(), postgres.passwords().flyway())
        .sqlMigrationPrefix("")
        .locations(postgres.locationMigration())
        .baselineOnMigrate(true);
Flyway flyway = fluentConfiguration.load();
flyway.migrate();

Where we connect GlobalConfiguration as a bean and get a subclass postgres() with all the setting for the database

  • No labels