Skip to content

io microsphere spring config ConfigurationPropertyRepository

github-actions[bot] edited this page Jun 18, 2026 · 18 revisions

ConfigurationPropertyRepository

Type: Class | Module: microsphere-spring-context | Package: io.microsphere.spring.config | Since: 1.0.0

Source: microsphere-spring-context/src/main/java/io/microsphere/spring/config/ConfigurationPropertyRepository.java

Overview

A repository for managing ConfigurationProperty instances with support for configuration via Spring's Environment.

This class provides methods to add, remove, retrieve, and manage configuration properties. It also allows integration with Spring's lifecycle management through the InitializingBean and DisposableBean interfaces, as well as environment-based property configuration through the EnvironmentAware interface.

Configuration Properties

- `#MAX_SIZE_PROPERTY_NAME`: Sets the maximum number of properties that can be stored in the repository.
    Defaults to `#DEFAULT_MAX_SIZE` if not specified.

Example Usage

`// Create a new repository instance
ConfigurationPropertyRepository repository = new ConfigurationPropertyRepository();

// Set the environment to load max size from configuration
ConfigurableEnvironment environment = new StandardEnvironment();
repository.setEnvironment(environment);

// Initialize the repository (usually done automatically by Spring)
repository.afterPropertiesSet();

// Add a configuration property
ConfigurationProperty property = new ConfigurationProperty("my.property.name");
property.setValue("exampleValue");
repository.add(property);

// Retrieve the property
ConfigurationProperty retrieved = repository.get("my.property.name");
System.out.println(retrieved.getValue()); // Output: exampleValue

// Clean up resources when done
repository.destroy();
`

Declaration

public class ConfigurationPropertyRepository implements EnvironmentAware, InitializingBean, DisposableBean

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.2.27-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

// Create a new repository instance
ConfigurationPropertyRepository repository = new ConfigurationPropertyRepository();

// Set the environment to load max size from configuration
ConfigurableEnvironment environment = new StandardEnvironment();
repository.setEnvironment(environment);

// Initialize the repository (usually done automatically by Spring)
repository.afterPropertiesSet();

// Add a configuration property
ConfigurationProperty property = new ConfigurationProperty("my.property.name");
property.setValue("exampleValue");
repository.add(property);

// Retrieve the property
ConfigurationProperty retrieved = repository.get("my.property.name");
System.out.println(retrieved.getValue()); // Output: exampleValue

// Clean up resources when done
repository.destroy();

Method Examples

setEnvironment

ConfigurationPropertyRepository repository = new ConfigurationPropertyRepository();
  // Assume the environment has "microsphere.spring.config-property-repository.max-size=3"
  repository.setEnvironment(environment);
  repository.afterPropertiesSet();
  assertEquals(3, repository.getMaxSize());

afterPropertiesSet

ConfigurationPropertyRepository repository = new ConfigurationPropertyRepository();
  repository.setEnvironment(environment);
  repository.afterPropertiesSet();
  // The repository is now ready to store ConfigurationProperty instances
  repository.add(new ConfigurationProperty("my.property"));
  assertTrue(repository.contains("my.property"));

destroy

ConfigurationPropertyRepository repository = new ConfigurationPropertyRepository();
  repository.setEnvironment(applicationContext.getEnvironment());
  repository.afterPropertiesSet();
  repository.add(configurationProperty);
  // When shutting down
  repository.destroy(); // clears all stored properties

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-spring-context</artifactId>
    <version>${microsphere-spring.version}</version>
</dependency>

Tip: Use the BOM (microsphere-spring-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.spring.config.ConfigurationPropertyRepository;

API Reference

Public Methods

Method Description
add The default max size of ConfigurationPropertyRepository
remove Remove a ConfigurationProperty instance by name
get Get a ConfigurationProperty instance by name
contains Determine whether the repository contains the specified name
createIfAbsent Create a ConfigurationProperty instance if absent
getAll Get all ConfigurationProperty instances
getMaxSize Get the max size of the repository
setEnvironment Sets the Environment and reads the maximum repository size from the
afterPropertiesSet Initializes the internal repository map with a capacity derived from the configured
destroy clear the repository

Method Details

add

public void add(ConfigurationProperty configurationProperty)

The default max size of ConfigurationPropertyRepository / public static final int DEFAULT_MAX_SIZE_PROPERTY_VALUE = parseInt(DEFAULT_MAX_SIZE);

/** The max size of ConfigurationPropertyRepository /

remove

public ConfigurationProperty remove(String name)

Remove a ConfigurationProperty instance by name

get

public ConfigurationProperty get(String name)

Get a ConfigurationProperty instance by name

contains

public boolean contains(String name)

Determine whether the repository contains the specified name

createIfAbsent

public ConfigurationProperty createIfAbsent(String name)

Create a ConfigurationProperty instance if absent

setEnvironment

public void setEnvironment(Environment environment)

Sets the Environment and reads the maximum repository size from the #MAX_SIZE_PROPERTY_NAME configuration property. Falls back to #DEFAULT_MAX_SIZE_PROPERTY_VALUE if the property is not set.

Example Usage

`ConfigurationPropertyRepository repository = new ConfigurationPropertyRepository();
  // Assume the environment has "microsphere.spring.config-property-repository.max-size=3"
  repository.setEnvironment(environment);
  repository.afterPropertiesSet();
  assertEquals(3, repository.getMaxSize());
`

afterPropertiesSet

public void afterPropertiesSet()

Initializes the internal repository map with a capacity derived from the configured #getMaxSize() max size. This method is typically called automatically by the Spring container after all properties have been set.

Example Usage

`ConfigurationPropertyRepository repository = new ConfigurationPropertyRepository();
  repository.setEnvironment(environment);
  repository.afterPropertiesSet();
  // The repository is now ready to store ConfigurationProperty instances
  repository.add(new ConfigurationProperty("my.property"));
  assertTrue(repository.contains("my.property"));
`

destroy

public void destroy()

clear the repository

Example Usage

`ConfigurationPropertyRepository repository = new ConfigurationPropertyRepository();
  repository.setEnvironment(applicationContext.getEnvironment());
  repository.afterPropertiesSet();
  repository.add(configurationProperty);
  // When shutting down
  repository.destroy(); // clears all stored properties
`

See Also

  • ConfigurationProperty

This documentation was auto-generated from the source code of microsphere-spring.

Home

spring-context

spring-guice

spring-jdbc

spring-test

spring-web

spring-webflux

spring-webmvc

Clone this wiki locally