<commit_before>//
// Copyright (c) 2016-2017 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:
// <NAME>
// <NAME>

[[configexamples]]
== Configuration Usage Examples


A configuration object can be obtained programmatically via the `ConfigProvider` or automatically via `@Inject Config`. An application can then access its configured values via a `Config` instance.

=== Simple Programmatic Example

[source, java]
----
public class ConfigUsageSample {

    public void useTheConfig() {
        // get access to the Config instance
        Config config = ConfigProvider.getConfig();

        String serverUrl = config.getValue("acme.myprj.some.url", String.class);

        callToServer(serverUrl);
    }
}
----

If you need to access a different server then you can e.g. change the configuration via a `-D` system property:

[source, text]
----
$> java -jar some.jar -Dacme.myprj.some.url=http://other.server/other/endpoint
----

Note that this is only one example how to possibly configure your application.
Another example is to register <<custom_configsources, Custom ConfigSources>> to e.g. pick up values from a database table, etc.

If a config value is a `,` separated string, this value can be automatically converted to a multiple element array with `\` as the escape character.
When specifying the property `myPets=dog,cat,dog\\,cat` in a config source, the following code snippet can be used to obtain an array.
----
 String[] myPets = config.getValue("myPets", String[].class); 
 //myPets = {"dog", "cat", "dog,cat"}
----


=== Simple Dependency Injection Example

Microprofile-Config also provides ways to inject configured values into your beans using the `@Inject` and the `@ConfigProperty` qualifier.

[source, java]
----
@ApplicationScoped
public class InjectedConfigUsageSample {

    @Inject
    private Config config;

    //The property myprj.some.url must exist in one of the configsources, otherwise a
    //DeploymentException will be thrown.
    @Inject
    @ConfigProperty(name="myprj.some.url")
    private String someUrl;
    //The following code injects an Optional value of myprj.some.port property. 
    //Contrary to natively injecting the configured value this will not lead to a 
    //DeploymentException if the configured value is missing. 
    @Inject
    @ConfigProperty(name="myprj.some.port")
    private Optional<Integer> somePort;
    //Injects a Provider for the value of myprj.some.dynamic.timeout property to 
    //resolve the property dynamically. Each invocation to Provider#get() will 
    //resolve the latest value from underlying Config.
    //The existence of configured values will get checked during startup. 
    //Instances of Provider<T> are guaranteed to be Serializable. 
    @Inject
    @ConfigProperty(name="myprj.some.dynamic.timeout", defaultValue="100")
    private javax.inject.Provider<Long> timeout;
    //The following code injects an Array, List or Set for the `myPets` property, 
    //where its value is a comma separated value ( myPets=dog,cat,dog\\,cat)
    @Inject @ConfigProperty(name="myPets") private String[] myArrayPets;
    @Inject @ConfigProperty(name="myPets") private List<String> myListPets; 
    @Inject @ConfigProperty(name="myPets") private Set<String> mySetPets;
}
----
<commit_msg>Make it more clear we talk about comma separated list of values. It is not very clear when viewed in PDF. Comma is displayed red and when fast reading somebody might miss the idea.<commit_after>//
// Copyright (c) 2016-2017 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:
// <NAME>
// <NAME>

[[configexamples]]
== Configuration Usage Examples


A configuration object can be obtained programmatically via the `ConfigProvider` or automatically via `@Inject Config`. An application can then access its configured values via a `Config` instance.

=== Simple Programmatic Example

[source, java]
----
public class ConfigUsageSample {

    public void useTheConfig() {
        // get access to the Config instance
        Config config = ConfigProvider.getConfig();

        String serverUrl = config.getValue("acme.myprj.some.url", String.class);

        callToServer(serverUrl);
    }
}
----

If you need to access a different server then you can e.g. change the configuration via a `-D` system property:

[source, text]
----
$> java -jar some.jar -Dacme.myprj.some.url=http://other.server/other/endpoint
----

Note that this is only one example how to possibly configure your application.
Another example is to register <<custom_configsources, Custom ConfigSources>> to e.g. pick up values from a database table, etc.

If a config value is a comma(`,`) separated string, this value can be automatically converted to a multiple element array with `\` as the escape character.
When specifying the property `myPets=dog,cat,dog\\,cat` in a config source, the following code snippet can be used to obtain an array.
----
 String[] myPets = config.getValue("myPets", String[].class);
 //myPets = {"dog", "cat", "dog,cat"}
----


=== Simple Dependency Injection Example

Microprofile-Config also provides ways to inject configured values into your beans using the `@Inject` and the `@ConfigProperty` qualifier.

[source, java]
----
@ApplicationScoped
public class InjectedConfigUsageSample {

    @Inject
    private Config config;

    //The property myprj.some.url must exist in one of the configsources, otherwise a
    //DeploymentException will be thrown.
    @Inject
    @ConfigProperty(name="myprj.some.url")
    private String someUrl;
    //The following code injects an Optional value of myprj.some.port property.
    //Contrary to natively injecting the configured value this will not lead to a
    //DeploymentException if the configured value is missing.
    @Inject
    @ConfigProperty(name="myprj.some.port")
    private Optional<Integer> somePort;
    //Injects a Provider for the value of myprj.some.dynamic.timeout property to
    //resolve the property dynamically. Each invocation to Provider#get() will
    //resolve the latest value from underlying Config.
    //The existence of configured values will get checked during startup.
    //Instances of Provider<T> are guaranteed to be Serializable.
    @Inject
    @ConfigProperty(name="myprj.some.dynamic.timeout", defaultValue="100")
    private javax.inject.Provider<Long> timeout;
    //The following code injects an Array, List or Set for the `myPets` property,
    //where its value is a comma separated value ( myPets=dog,cat,dog\\,cat)
    @Inject @ConfigProperty(name="myPets") private String[] myArrayPets;
    @Inject @ConfigProperty(name="myPets") private List<String> myListPets;
    @Inject @ConfigProperty(name="myPets") private Set<String> mySetPets;
}
----

{'fraction_non_alphanumeric': 0.0957631318136769, 'fraction_numerical': 0.0037165510406342913, 'mean_word_length': 3.7293497363796133, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5099264', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2107, 'n_words': 922}