项目作者: DanielMehlber

项目描述 :
Simple and lightweight property and configuration loading using elegant annotations in Java
高级语言: Java
项目地址: git://github.com/DanielMehlber/jprops-annotations.git
创建时间: 2021-06-24T20:31:00Z
项目社区:https://github.com/DanielMehlber/jprops-annotations

开源协议:

下载


jprops-annotations

Simple and lightweight property and configuration loading using elegant annotations in Java

  1. import jprops.configuration.InsufficientConfigurationException;
  2. import jprops.configuration.PropertyLoader;
  3. import jprops.configuration.annotations.LoadProperty;
  4. class ConfigurationExample {
  5. /*
  6. * jprops-annotations injects values from a .properties file into fields.
  7. * Default is 'config.properties' in classpath
  8. */
  9. // Load property value by name
  10. @LoadProperty( "someString" )
  11. public static String someStringValue;
  12. // Define type of property value for conversion (default is String)
  13. @LoadProperty( value="intValue", type=ConfigProperty.Type.INT )
  14. private static int someIntValue;
  15. // Define required properties. An Exception will be thrown, in case it's not provided.
  16. @LoadProperty( value="required", required=true )
  17. protected static String requiredValue;
  18. // Define the URI or location of your properties file (default is 'config.properties')
  19. @LoadProperty( value="fromAnotherFile", res="application.properties" )
  20. public static String fromAnotherFile;
  21. // Use default values in case a property is not provided
  22. @LoadProperty( value="defaultValue", defaultValue="none", required=false )
  23. private static String defaultValue;
  24. // Or any other function in the programs lifecycle
  25. public static void main(String[] args) {
  26. // Load properties into class fields by passing those classes to the processor
  27. try {
  28. ConfigurationLoader.loadProperties(ConfigurationExample.class, AnotherExample.class, ...);
  29. } catch (...) {}
  30. }
  31. }

The Annotation

Annotated fields must be static. Their visibility is irrelevant.

  1. @LoadProperty(
  2. value="...", // name of property
  3. defaultValue="...", // default value in case property is not provided
  4. required=true|false, // whether or not the property must be provided (will throw Exception if violated)
  5. res="...", // URI or location of .properties file (relative to classpath)
  6. type=ConfigProperty.Type.* // Type to which the String value will be converted to
  7. )
  8. (public | private | protected) static (String | int | float | double | boolean) value;