com.namics.oss.spring.support.configuration:spring-configuration-support-starter-sample

Spring-Configuration-Support is a library which allows to fetch environment-specific property values from a configured data-source.

License

License

Categories

Categories

Configuration Application Layer Libs config
GroupId

GroupId

com.namics.oss.spring.support.configuration
ArtifactId

ArtifactId

spring-configuration-support-starter-sample
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Spring-Configuration-Support is a library which allows to fetch environment-specific property values from a configured data-source.
Project Organization

Project Organization

Namics AG

Download spring-configuration-support-starter-sample

How to add to project

<!-- https://jarcasting.com/artifacts/com.namics.oss.spring.support.configuration/spring-configuration-support-starter-sample/ -->
<dependency>
    <groupId>com.namics.oss.spring.support.configuration</groupId>
    <artifactId>spring-configuration-support-starter-sample</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.namics.oss.spring.support.configuration/spring-configuration-support-starter-sample/
implementation 'com.namics.oss.spring.support.configuration:spring-configuration-support-starter-sample:1.1.0'
// https://jarcasting.com/artifacts/com.namics.oss.spring.support.configuration/spring-configuration-support-starter-sample/
implementation ("com.namics.oss.spring.support.configuration:spring-configuration-support-starter-sample:1.1.0")
'com.namics.oss.spring.support.configuration:spring-configuration-support-starter-sample:jar:1.1.0'
<dependency org="com.namics.oss.spring.support.configuration" name="spring-configuration-support-starter-sample" rev="1.1.0">
  <artifact name="spring-configuration-support-starter-sample" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.namics.oss.spring.support.configuration', module='spring-configuration-support-starter-sample', version='1.1.0')
)
libraryDependencies += "com.namics.oss.spring.support.configuration" % "spring-configuration-support-starter-sample" % "1.1.0"
[com.namics.oss.spring.support.configuration/spring-configuration-support-starter-sample "1.1.0"]

Dependencies

compile (7)

Group / Artifact Type Version
org.springframework.boot : spring-boot-autoconfigure jar 2.1.0.RELEASE
org.springframework.boot : spring-boot-starter-web jar
org.springframework.boot : spring-boot-starter-data-jpa jar
javax.inject : javax.inject jar 1
com.namics.oss.spring.support.configuration : spring-configuration-support-starter jar 1.1.0
com.github.ulisesbocchio : jasypt-spring-boot-starter jar 2.1.0
com.github.ulisesbocchio : jasypt-spring-boot jar 2.1.0

runtime (1)

Group / Artifact Type Version
com.h2database : h2 jar

Project Modules

There are no modules declared in this project.

Spring-Configuration-Support

System Status
CI master Build Status
CI develop Build Status
Dependency Maven Central

Spring-Configuration-Support allows to fetch environment-specific property values from a configured data-source. Additionally, a default user-interface for management of these properties is provided as well. The configuration of a PropertySourcesPlaceholderConfigurer, which depends on the provided source, allows to resolve the specified property placeholders in your application.

Usage

You have the choice of either configuring the library yourself (via Java-Config) or use the provided Spring Boot starter dependency.

Maven Dependency (Latest Version in pom.xml):

<dependency>
	<groupId>com.namics.oss.spring.support.configuration</groupId>
	<artifactId>spring-configuration-support</artifactId>
	<version>1.0.0</version>
</dependency>

Spring Boot Starter (Latest Version in pom.xml):

<dependency>
	<groupId>com.namics.oss.spring.support.configuration</groupId>
	<artifactId>spring-configuration-support-starter</artifactId>
	<version>1.0.0</version>
</dependency>

Requirements

Java: JDK 8

Configuration

Spring Boot Starter Auto-Configuration

Requirements: Configured DataSource Bean.

The auto-configuration relies on a pre-configured DataSource Bean. If no different table or column names are specified, the following structure of the table named "nmx_configuration" is required:

configuration_env | configuration_key | configuration_value
----------------------------------------------------------------------------
DEV               | app.base.url      | http://localhost:8080/myapp
PROD              | app.base.url      | http://prod.customer.com/myapp

The default environment is "*". The table name, the column names and the default environment can be specified via the following properties in your application.properties file:

com.namics.oss.spring.support.configuration.dataSource.tableName=my_configuration_table
com.namics.oss.spring.support.configuration.dataSource.keyColumnName=my_configuration_key
com.namics.oss.spring.support.configuration.dataSource.valueColumnName=my_configuration_value
com.namics.oss.spring.support.configuration.dataSource.environmentColumnName=my_configuration_env
com.namics.oss.spring.support.configuration.dataSource.defaultEnvironment=MY_DEFAULT_ENVIRONMENT

User Interface

The user-interface for managing the properties is located at the following url by default: http://localhost:8080/configuration/properties.html (for localhost)

The servlet-mapping as well as the servlet-name can be specified in your application.properties file as well:

com.namics.oss.spring.support.configuration.web.servletName=configurationServlet
com.namics.oss.spring.support.configuration.web.servletMapping=/configuration/*

There is also the possibility to exclude the user-interface from the auto-configuration by excluding the dependency spring-configuration-support-web:

<dependency>
	<groupId>com.namics.oss.spring.support.configuration</groupId>
	<artifactId>spring-configuration-support-starter</artifactId>
	<exclusions>
		<exclusion>
			<groupId>com.namics.oss.spring.support.configuration</groupId>
			<artifactId>spring-configuration-support-web</artifactId>
		</exclusion>
	</exclusions>
</dependency>

Configuration via Java-Config

The following example, based on an in-memory database, demonstrates the configuration of the module by using Spring Java-Config. Depending on the active profiles in your environment, the module adds a PropertiesPropertySource for every profile to your environment. The PropertiesPropertySource representing the default environment has the lowest priority.

@Configuration
public class PropertiesConfig {
    
	@Bean(name = "dbSource")
	public PropertiesPropertySource[] source(
			@Named("dbProps") OrderedProperties orderedProperties,
			ConfigurableEnvironment environment) throws Exception {

		PropertiesPropertySource[] propertiesPropertySources = orderedProperties.toPropertiesPropertySources();
		for(int i=(propertiesPropertySources.length-1);i>=0;i--){
			environment.getPropertySources().addFirst(propertiesPropertySources[i]);
		}
		return propertiesPropertySources;
	}

	@Bean(name = "dbProps")
	public DaoConfigurationPropertiesFactoryBean databaseConfigFactory(ConfigurationDao configurationDao, Environment environment) throws Exception {
		return new DaoConfigurationPropertiesFactoryBean(configurationDao, environment.getActiveProfiles(), "*");
	}

	@Bean
	public ConfigurationDaoJdbcImpl configurationDao(DataSource dataSource) {
		return ConfigurationDaoJdbcImpl.forDataSource(dataSource)
		                               .tableName("nmx_configuration")
		                               .environmentColumn("environment")
		                               .propertyKeyColumn("property_key")
		                               .valueColumn("property_value")
		                               .build();
	}
	
    @Bean
    public DataSource dataSource(){
        return new EmbeddedDatabaseBuilder()
                .continueOnError(true)
                .addScripts("classpath:/META-INF/db/schema.sql","classpath:/META-INF/db/data.sql")
                .build();
    }
}

You can also configure a Bean of type ConfigurationValueService to work with the configured properties. Note that you have to set a specific environment to operate on.

@Bean
public ConfigurationValueServiceImpl configurationValueService(ConfigurationDao configurationDao) {
	return new ConfigurationValueServiceImpl(configurationDao, new ConfigurationEnvironment("DEV"));
}

Configure a PropertySourcesPlaceholderConfigurer, which depends on the loaded property sources, to resolve the placeholders (e.g. @Value(...)) in your application.

@Bean
@DependsOn("dbSource")
public static PropertySourcesPlaceholderConfigurer databaseConfigurer() {
	return new PropertySourcesPlaceholderConfigurer();
}

Default values

Certain values remain the same for multiple environments (e.g. maximum password length). In this case, we configure the length for the default environment "*".

Example:

configuration_env | configuration_key | configuration_value
-----------------------------------------------------------
*                 | password.length   | 60
PROD              | password.length   | 50

Using the above configuration the property password.length will always resolve to 60 unless the profile "PROD" is active. If the profile "PROD" is active, all configuration values for the specified environment will be added as a PropertySource to the environment additionally. The property password.length will then resolve to the value 50.

A SQL-Script creating the default schema is located at: com/namics/oss/spring/support/configuration/default-schema.sql

com.namics.oss.spring.support.configuration

Namics AG

Versions

Version
1.1.0
1.0.1
1.0.0