Class ConfigurationPropertyRepository

  • All Implemented Interfaces:
    org.springframework.beans.factory.Aware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.context.EnvironmentAware

    public class ConfigurationPropertyRepository
    extends java.lang.Object
    implements org.springframework.context.EnvironmentAware, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean
    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

    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();
     
    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    ConfigurationProperty
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(io.microsphere.beans.ConfigurationProperty configurationProperty)
      Add a ConfigurationProperty instance
      void afterPropertiesSet()
      Initializes the internal repository map with a capacity derived from the configured max size.
      boolean contains​(java.lang.String name)
      Determine whether the repository contains the specified name
      io.microsphere.beans.ConfigurationProperty createIfAbsent​(java.lang.String name)
      Create a ConfigurationProperty instance if absent
      void destroy()
      clear the repository
      io.microsphere.beans.ConfigurationProperty get​(java.lang.String name)
      Get a ConfigurationProperty instance by name
      java.util.Collection<io.microsphere.beans.ConfigurationProperty> getAll()
      Get all ConfigurationProperty instances
      int getMaxSize()
      Get the max size of the repository
      protected java.util.Map<java.lang.String,​io.microsphere.beans.ConfigurationProperty> getRepository()
      Returns the internal repository map.
      io.microsphere.beans.ConfigurationProperty remove​(java.lang.String name)
      Remove a ConfigurationProperty instance by name
      void setEnvironment​(org.springframework.core.env.Environment environment)
      Sets the Environment and reads the maximum repository size from the "microsphere.spring.config-property-repository.max-size" configuration property.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PROPERTY_NAME_PREFIX

        public static final java.lang.String PROPERTY_NAME_PREFIX
        See Also:
        Constant Field Values
      • MAX_SIZE_PROPERTY_NAME

        @ConfigurationProperty(type=int.class,
                               description="The max size of the repository for ConfigurationProperty instances",
                               defaultValue="99999",
                               source="system-properties")
        public static final java.lang.String MAX_SIZE_PROPERTY_NAME
        See Also:
        Constant Field Values
    • Constructor Detail

      • ConfigurationPropertyRepository

        public ConfigurationPropertyRepository()
    • Method Detail

      • add

        public void add​(io.microsphere.beans.ConfigurationProperty configurationProperty)
        Add a ConfigurationProperty instance
        Parameters:
        configurationProperty - a ConfigurationProperty instance
      • remove

        public io.microsphere.beans.ConfigurationProperty remove​(java.lang.String name)
        Remove a ConfigurationProperty instance by name
        Parameters:
        name - the name of ConfigurationProperty
      • get

        public io.microsphere.beans.ConfigurationProperty get​(java.lang.String name)
        Get a ConfigurationProperty instance by name
        Parameters:
        name - the name of ConfigurationProperty
        Returns:
        null if not found
      • contains

        public boolean contains​(java.lang.String name)
        Determine whether the repository contains the specified name
        Parameters:
        name - the name of ConfigurationProperty
        Returns:
        true if contains, otherwise false
      • createIfAbsent

        public io.microsphere.beans.ConfigurationProperty createIfAbsent​(java.lang.String name)
        Create a ConfigurationProperty instance if absent
        Parameters:
        name - the name of ConfigurationProperty
        Returns:
        the ConfigurationProperty instance
      • getAll

        public java.util.Collection<io.microsphere.beans.ConfigurationProperty> getAll()
        Get all ConfigurationProperty instances
        Returns:
        never null
      • getMaxSize

        public int getMaxSize()
        Get the max size of the repository
        Returns:
        max size
      • setEnvironment

        public void setEnvironment​(org.springframework.core.env.Environment environment)
        Sets the Environment and reads the maximum repository size from the "microsphere.spring.config-property-repository.max-size" 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());
         
        Specified by:
        setEnvironment in interface org.springframework.context.EnvironmentAware
        Parameters:
        environment - the Spring Environment to read configuration from
      • afterPropertiesSet

        public void afterPropertiesSet()
        Initializes the internal repository map with a capacity derived from the configured 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"));
         
        Specified by:
        afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
      • destroy

        public void destroy()
                     throws java.lang.Exception
        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
         
        Specified by:
        destroy in interface org.springframework.beans.factory.DisposableBean
        Throws:
        java.lang.Exception - if an error occurs during cleanup
      • getRepository

        protected java.util.Map<java.lang.String,​io.microsphere.beans.ConfigurationProperty> getRepository()
        Returns the internal repository map. If the repository has not been initialized yet, afterPropertiesSet() is invoked to lazily initialize it.

        Example Usage

        
           // In a subclass overriding repository behavior:
           Map<String, ConfigurationProperty> repo = getRepository();
           repo.put("custom.property", new ConfigurationProperty("custom.property"));
           assertTrue(repo.containsKey("custom.property"));
         
        Returns:
        the internal map of property names to ConfigurationProperty instances