public class AndHow extends Object implements PropertyConfiguration, ValidatedValues
AndHow.findConfig().setCmdLineArgs(myCmdLineArgs).build();
findConfig() finds the AndHowConfiguration that
would be used if AndHow.instance() was called. build()
then causes the AndHow instance to be built with that modified configuration.
The code above (or any method of AndHow initiation) can only be executed once
during the life of the application.| Modifier and Type | Class and Description |
|---|---|
static class |
AndHow.Initialization
Encapsulate when and where AndHow was initialized.
|
| Modifier and Type | Field and Description |
|---|---|
static String |
ANDHOW_INLINE_NAME |
static String |
ANDHOW_NAME |
static String |
ANDHOW_TAG_LINE |
static String |
ANDHOW_URL |
| Modifier and Type | Method and Description |
|---|---|
Stream<PropertyExport> |
export(Class<?>... exportClasses)
Export Property's to a collection for use with frameworks that take configuration as
key-value maps or similar.
|
static AndHowConfiguration<? extends AndHowConfiguration> |
findConfig()
Prior to AndHow initialization, this method finds the configuration that will be used.
|
List<EffectiveName> |
getAliases(Property<?> property)
All the effective 'in' & 'out' aliases for this property, not including the canonical name.
|
String |
getCanonicalName(Property<?> prop)
The canonical name of a
Property. |
<T> T |
getExplicitValue(Property<T> prop)
The value found and loaded for this value by a Loader.
|
static StackTraceElement[] |
getInitializationTrace()
Get the stacktrace of where AndHow was initialized.
|
NamingStrategy |
getNamingStrategy()
Defines how names are created for Properties.
|
<T> T |
getValue(Property<T> prop)
The effective value, similar to Property.getValue, but specifically for
the context of this ValueMap.
|
static AndHow |
instance()
Returns the singleton instance of AndHow, initializing a new instance if one doesn't exist.
|
boolean |
isExplicitlySet(Property<?> prop)
True if the Property's value is explicitly set to a non-null value via one of the loaders.
|
static boolean |
isInitialized()
Determine if AndHow is initialized or not w/out forcing AndHow to load.
|
static void |
setConfig(AndHowConfiguration<? extends AndHowConfiguration> config)
Prior to AndHow initialization, this method explicitly sets the configuration to be used.
|
public static final String ANDHOW_INLINE_NAME
public static final String ANDHOW_NAME
public static final String ANDHOW_URL
public static final String ANDHOW_TAG_LINE
public static AndHowConfiguration<? extends AndHowConfiguration> findConfig() throws AppFatalException
AndHowConfiguration instance will be
created. Later calls to this method prior to AndHow initialization will return that
same instance. After AndHow is initialized this method will throw a fatal exception, since no
modification to the configuration can be made after AndHow has initialized.
The new AndHowConfiguration is created in one of two ways:
AndHowInit interface on the classpath,
it will be discovered and that class' AndHowInit.getConfiguration() method will be
called to construct the instance. This is an easy configuration point to configure AndHow
for production or test.
AndHowConfiguration is created
and returned.
This method provides a way to 'grab' the configuration before AndHow initialization and make additions or customizations. Common reasons for doing this would be:
main(String[] args) method to add the command line arguments to
AndHow (see code example below)
Example usage in a main method:
public static void main(String[] args) {
AndHow.findConfig().setCmdLineArgs(myCmdLineArgs);
//Do other stuff - AndHow initializes as soon as the first Property access happens.
//...
//AndHow.instance(); //Forces initialization, but isn't required
}
In the above example, AndHow will initialize itself as soon as the first Property value
is accessed, e.g. MyProperty.getValue(). In nearly all cases this is acceptable.
If, however, you are worried your application has thread contention at startup or the
application doesn't access property values until some later event happens, you can
force initialization by calling AndHow.instance(). Initialization will force
validation of all property values and will block other attempts to initialize AndHow,
perhaps helping to pinpoint startup contention.
Note: There was a behaviour change from v1.4.1 to v1.4.2: Beginning with v1.4.2, this method returns the same instance each time until initialization, then it throws a fatal exception. In v1.4.1 and earlier, a newly created instance was returned for each call. This is a large behaviour change, but its really blocking an unsafe application operation or an unexpected application 'no-op' where the caller might expect to keep working on the same configuration and actually be starting over.
AppFatalException - A fatal exception if called after AndHow has initialized.public static void setConfig(AndHowConfiguration<? extends AndHowConfiguration> config) throws AppFatalException
It is easier and safer to use findConfig(), which will a auto-discover
configuration, however, during testing or some advanced use cases,
it can be useful to ignore the normal configuration discovery mechanism.
If this method is used, configuration created by AndHowInit.getConfiguration()
and possibly later modified by calls to findConfig() up to this point
will be replaced by the configuration set here. Later calls to findConfig() will
return this new value.
config - The new configuration which must not be null.AppFatalException - If AndHow is already initialized or the passed config is null.public static AndHow instance() throws AppFatalException
In production, use of this method is optional at startup. See findConfig()
for an example of how to access and modify configuration, and initialize AndHow.
AppFatalException - If AndHow is mis-configuredpublic static boolean isInitialized()
public static StackTraceElement[] getInitializationTrace()
public Stream<PropertyExport> export(Class<?>... exportClasses) throws IllegalAccessException
Simple example usage that results in a Map<String, String> of names and values:
//UI_CONFIG & SERVICE_CONFIG contain AndHow Properties
Map<String, String> export =
AndHow.instance().export(UI_CONFIG.class, SERVICE_CONFIG.class)
.collect(ExportCollector.stringMap());
Property export is not allowed by default and is enabled by annotating a class containing
Properties with ManualExportAllowed.
ExportCollector can collect to Maps or Properties, as well as exporting
String or Object values. Many variations are possible: e.g. export object values
(instead of Strings) and prepend 'aaa_' to each export name:
Map<String, Object> export =
AndHow.instance().export(SERVICE_CONFIG.class)
.map(p -> p.mapNames(
p.getExportNames().stream().map(n -> "aaa_" + n).collect(Collectors.toList())
))
.collect(ExportCollector.objectMap());
export() returns a Stream of PropertyExport's, one PropertyExport per
Property. Each PropertyExport has a list of export names for the Property. Names can include
the Property's canonical name and any 'out' aliases for the Property. Which names are included
is controlled by options on the ManualExportAllowed annotation.
The export names and the values can be remapped via PropertyExport 'map' methods
(see 'mapNames' in example above). See PropertyExport for more mapping examples.
The ExportCollector creates an entry in the final collection for each name, mapping
it to the Property's Object or String value. Thus, the ExportCollector 'flattens'
PropertyExports by expanding the name list and collects them into the final collection.
Exports via the export() method are 'Manual' - the app must invoke them. There are
also 'Auto' exports - See the GroupExport annotation.
exportClasses - The classes to have their contained Properties exported.PropertyExport which can be converted into a collection of
key-value pairs via one of the ExportCollectors.IllegalAccessException - If any of the exported classes are not annotated to allow export.public boolean isExplicitlySet(Property<?> prop)
ValidatedValuesisExplicitlySet in interface ValidatedValuesprop - The property to checkpublic <T> T getExplicitValue(Property<T> prop)
ValidatedValuesgetExplicitValue in interface ValidatedValuesT - The return type of the Property.prop - The property to get the value forpublic <T> T getValue(Property<T> prop)
ValidatedValuesgetValue in interface ValidatedValuesT - The return type of the Property.prop - The property to get the value for.public List<EffectiveName> getAliases(Property<?> property)
PropertyConfigurationThe returned aliases may differ from the original requested aliases in two ways:
NamingStrategy may modify 'In' aliases, e.g. convert them to uppercase for
case-insensitive matching. This method returns the modified 'In' aliases.getAliases in interface PropertyConfigurationproperty - The property to fetch naming information forEffectiveNames for this Property.Property.getInAliases(),
Property.getOutAliases(),
Property.getRequestedAliases()public String getCanonicalName(Property<?> prop)
PropertyConfigurationProperty.
Canonical Property names are the full Java classname of the class containing the Property, plus
the Property name, e.g. org.acme.myapp.MyClass.MyProperty. Properties contained in
inner classes and interfaces continue the same naming structure, e.g.
org.acme.myapp.MyClass.MyInnerClass.MyInnerInterface.MyProperty.
getCanonicalName in interface PropertyConfigurationprop - The Property to get the canonical name for.public NamingStrategy getNamingStrategy()
PropertyConfigurationgetNamingStrategy in interface PropertyConfigurationCopyright © 2022. All rights reserved.