public class StdPropFileOnClasspathLoader extends PropFileOnClasspathLoader implements StandardLoader
.property file on the
classpath. By default, this loader will look for a file named
andhow.properties at the root of the classpath.
andhow.properties
at the root of the classpath via the StdPropFileOnClasspathLoader.
andhow.properties from the root of the classpath
java.util.Properties,
which silently ignores duplicate property entries
(i.e., the same key appearing multiple times). When there are duplicate
property keys in a properties file, only the last assigned value is used.
Full details on how Java parses properties files can be found in the
properties file specification.
Since resources on the test classpath override those on the main classpath,
its easy to have separate configurations for production and testing simply
by having two andhow.properties files, one on the main classpath
and one on test. There are many other options with classpath properties files.
To change the name of the properties file used for testing, a class like the example below can be added to your test classpath:
import org.yarnandtail.andhow.*;
import org.yarnandtail.andhow.property.StrProp;
public class UsePropertyFileOnClasspath implements AndHowTestInit {
{@literal @}Override
public AndHowConfiguration getConfiguration() {
return StdConfig.instance()
.setClasspathPropFilePath("/my_test_configuration_properties_file.prop");
}
}
If AndHow finds a AndHowTestInit implementation on the classpath,
it uses it in preference to other initiation points.
With this only on the test classpath, AndHow will still use the default
andhow.properties for production.
With slightly fancier configuration, something like /staging.properties
could be used in a staging environment and /production.properties
used in production. In a case like this we need the classpath to be
configurable, not hard-coded like the example above. To do that we need
a Property to configure it with, then we tell AndHow which property was
used.
Here is an example of a configurable classpath:
import org.yarnandtail.andhow.*;
import org.yarnandtail.andhow.property.StrProp;
public class UsePropertyFileOnClasspath implements AndHowInit {
public static final StrProp MY_CLASSPATH = StrProp.builder()
.desc("Path to a properties file on the classpath. "
+ "If the file is not present, it is not considered an error.").build();
{@literal @}Override
public AndHowConfiguration getConfiguration() {
return StdConfig.instance()
.setClasspathPropFilePath(MY_CLASSPATH);
}
}
The code above adds the property MY_CLASSPATH
(the name is arbitrary) which is used to configure the
StdPropFileOnClasspathLoader with a custom property file location.
When AndHow initializes, the StdPropFileOnClasspathLoader checks to
see if a value has been loaded for MY_CLASSPATH by any prior loader.
If a value is present, the loader tries to load from the configured classpath.
If no value is configured, the default classpath is assumed.
Properties file loaders are the last loaders, so configuration the classpath
can be done by virtually anything: environment vars, system properties,
command line args, JNDI...
StandardLoader's, this loader is intended to be auto-created
by AndHow. The set of standard loaders and their order can bet set
via the AndHowConfiguration.setStandardLoaders() methods.
Other loaders which don't implement the StandardLoader interface can
be inserted into the load order via the
AndHowConfiguration.insertLoaderBefore/After().| Modifier and Type | Field and Description |
|---|---|
static String |
DEFAULT_PROP_FILE |
missingFileAProblem, pathProp, pathStr, unknownPropertyAProblem| Constructor and Description |
|---|
StdPropFileOnClasspathLoader()
There is no reason to use the constructor in production application code
because AndHow creates a single instance on demand at runtime.
|
| Modifier and Type | Method and Description |
|---|---|
protected String |
getEffectivePath(ValidatedValuesWithContext existingValues)
Utility method to simplify finding the effective path.
|
getSpecificLoadDescription, load, loadgetConfigSamplePrinter, getInstanceConfig, getLoaderDialect, getLoaderType, isFlaggable, isMissingFileAProblem, isTrimmingRequiredForStringValues, isUnknownPropertyAProblem, loadInputStreamToProps, setFilePath, setFilePath, setMissingFileAProblem, setUnknownPropertyAProblemattemptToAdd, attemptToAdd, attemptToAdd, attemptToAddIfNotDuplicate, createValue, findDuplicateProperty, getClassConfig, mapNametoProperty, releaseResourcesclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetClassConfig, getConfigSamplePrinter, getInstanceConfig, getLoaderDialect, getLoaderType, getSpecificLoadDescription, isFlaggable, isTrimmingRequiredForStringValues, load, releaseResourcespublic static final String DEFAULT_PROP_FILE
public StdPropFileOnClasspathLoader()
protected String getEffectivePath(ValidatedValuesWithContext existingValues)
PropFileBaseLoadergetEffectivePath in class PropFileBaseLoaderCopyright © 2022. All rights reserved.