forked from eclipse/microprofile-config
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs: eclipse#384 backport spec wording fixes and enhancements from C…
…onfigJSR All changes originally by me, except 1 smallish fixes by Tomas Langer.
- Loading branch information
Showing
8 changed files
with
159 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// Copyright (c) 2016-2018 Contributors to the Eclipse Foundation | ||
// | ||
// See the NOTICE file(s) distributed with this work for additional | ||
// information regarding copyright ownership. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// Contributors: | ||
// Mark Struberg | ||
|
||
[[configaccessor]] | ||
== ConfigAccessor | ||
|
||
|
||
The `ConfigAccessor` API is intended for typed configuration values and precise control over resolution. | ||
|
||
=== ConfigAccessor Usage | ||
|
||
The simplest usage of the API is resolution of a String property, equivalent to a call to `Config.getValue(propertyKey, String.class)`. | ||
This can also be handled via `ConfigAccessor` as shown in the following example: | ||
|
||
.Simple example of ConfigAccessor | ||
[source,java] | ||
----------------------------------------------------------------- | ||
String userName = config.access("user.name").getValue(); | ||
----------------------------------------------------------------- | ||
|
||
Additional to this the `ConfigAccessor` also allows for far more complex control over the access to configured values. | ||
|
||
The call to `config.access(..)` returns a `ConfigAccessor`. | ||
This is basically a builder which has methods to refine the resolution, including the following: | ||
|
||
* `as(Class<N> clazz)` -- defines the return type of the property | ||
* `asList()` -- Declare the ConfigAccessor to return a List of the given Type. | ||
* `evaluateVariables(boolean evaluateVariables)` | ||
* `useConverter(Converter<T> converter)` -- Defines a specific {@link Converter} to be used instead of applying the default Converter resolving logic. | ||
* `withLookupSuffix(String postfixName)` -- sets a parameter for the resolution of a 'postfix' for the resolution | ||
* `withLookupSuffix(ConfigAccessor)` -- you can also use a `ConfigAccessor` to determine the 'postfix' for the resolution | ||
* `withDefault(T value)` -- sets the default value, used in case the resolution returns `null` | ||
* `getValue()` -- returns the resolved value with the appropriate type | ||
|
||
.A more complete example of ConfigAccessor | ||
[source,java] | ||
----------------------------------------------------------------- | ||
Config config = ConfigProvider.getConfig(); | ||
ConfigAccessor<Integer> dbPortCfg | ||
= config.access("db.port") | ||
.as(Integer.class) | ||
.addLookupSuffix(config.access("project.projectStage").cacheFor(12, TimeUnit.HOURS)) | ||
.addLookupSuffix("database.vendor") | ||
.withDefault(3306); | ||
... | ||
Integer port = dbPortCfg.getValue(); | ||
----------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters