24 | * Users and frameworks may provide custom {@link org.eclipse.microprofile.config.spi.ConfigSource} and 25 | * {@link org.eclipse.microprofile.config.spi.Converter} instances. Configuration instances may be set up and created 26 | * using the {@link org.eclipse.microprofile.config.spi.ConfigBuilder} API. 27 | *
28 | * The package also contains the class {@link org.eclipse.microprofile.config.spi.ConfigProviderResolver}, which is used
29 | * to implement the specification itself.
30 | *
31 | * @author Emily Jiang
32 | * @author Mark Struberg
33 | *
34 | */
35 | @org.osgi.annotation.versioning.Version("3.0")
36 | package org.eclipse.microprofile.config.spi;
37 |
--------------------------------------------------------------------------------
/tck/src/main/java/org/eclipse/microprofile/config/tck/base/AbstractTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation
3 | *
4 | * See the NOTICE file(s) distributed with this work for additional
5 | * information regarding copyright ownership.
6 | *
7 | * Licensed under the Apache License, Version 2.0 (the "License");
8 | * You may not use this file except in compliance with the License.
9 | * You may obtain a copy of the License at
10 | *
11 | * http://www.apache.org/licenses/LICENSE-2.0
12 | *
13 | * Unless required by applicable law or agreed to in writing, software
14 | * distributed under the License is distributed on an "AS IS" BASIS,
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | * See the License for the specific language governing permissions and
17 | * limitations under the License.
18 | *
19 | */
20 | package org.eclipse.microprofile.config.tck.base;
21 |
22 | import org.jboss.arquillian.testng.Arquillian;
23 | import org.jboss.shrinkwrap.api.asset.UrlAsset;
24 | import org.jboss.shrinkwrap.api.container.ResourceContainer;
25 |
26 | /**
27 | * @author Mark Struberg
28 | */
29 | public class AbstractTest extends Arquillian {
30 |
31 | public static void addFile(ResourceContainer> archive, String originalPath) {
32 | archive.addAsResource(
33 | new UrlAsset(Thread.currentThread().getContextClassLoader().getResource("internal/" + originalPath)),
34 | originalPath);
35 | }
36 |
37 | public static void addFile(ResourceContainer> archive, String originalFile, String targetFile) {
38 | archive.addAsResource(new UrlAsset(Thread.currentThread().getContextClassLoader().getResource(originalFile)),
39 | targetFile);
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/tck/src/main/java/org/eclipse/microprofile/config/tck/broken/ConfigObserver.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2016-2019 Contributors to the Eclipse Foundation
3 | *
4 | * See the NOTICE file(s) distributed with this work for additional
5 | * information regarding copyright ownership.
6 | *
7 | * Licensed under the Apache License, Version 2.0 (the "License");
8 | * You may not use this file except in compliance with the License.
9 | * You may obtain a copy of the License at
10 | *
11 | * http://www.apache.org/licenses/LICENSE-2.0
12 | *
13 | * Unless required by applicable law or agreed to in writing, software
14 | * distributed under the License is distributed on an "AS IS" BASIS,
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | * See the License for the specific language governing permissions and
17 | * limitations under the License.
18 | *
19 | */
20 | package org.eclipse.microprofile.config.tck.broken;
21 |
22 | import org.eclipse.microprofile.config.inject.ConfigProperty;
23 |
24 | import jakarta.enterprise.context.ApplicationScoped;
25 | import jakarta.enterprise.context.Initialized;
26 | import jakarta.enterprise.event.Observes;
27 |
28 | /**
29 | * A bean supporting the {@link MissingValueOnObserverMethodInjectionTest} test that injects a non-existent
30 | * configuration property in a container lifecycle event observer method.
31 | *
32 | * @author Laird Nelson
33 | *
34 | * @see MissingValueOnObserverMethodInjectionTest
35 | */
36 | @ApplicationScoped
37 | public class ConfigObserver {
38 |
39 | public void onStartup(@Observes @Initialized(ApplicationScoped.class) final Object event,
40 | @ConfigProperty(name = "this.property.does.not.exist") final String nonExistentConfigurationPropertyValue) {
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/api/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
16 |
24 | * MicroProfile Config also supports injection via a JSR-330 DI container:
25 | *
26 | *
33 | * The following types can be injected:
34 | *
35 | *
24 | * Holds information about the configuration property name, configuration value, the
25 | * {@link org.eclipse.microprofile.config.spi.ConfigSource} name from where the configuration property was loaded and
26 | * the ordinal of the {@link org.eclipse.microprofile.config.spi.ConfigSource}.
27 | *
28 | * This is used together with {@link Config} to expose the configuration property lookup metadata.
29 | *
30 | * @since 2.0
31 | * @author Roberto Cortez
32 | */
33 | public interface ConfigValue {
34 | /**
35 | * The name of the property.
36 | *
37 | * @return the name of the property.
38 | */
39 | String getName();
40 |
41 | /**
42 | * The value of the property lookup with transformations (expanded, etc).
43 | *
44 | * @return the value of the property lookup or {@code null} if the property could not be found
45 | */
46 | String getValue();
47 |
48 | /**
49 | * The value of the property lookup without any transformation (expanded , etc).
50 | *
51 | * @return the raw value of the property lookup or {@code null} if the property could not be found.
52 | */
53 | String getRawValue();
54 |
55 | /**
56 | * The {@link org.eclipse.microprofile.config.spi.ConfigSource} name that loaded the property lookup.
57 | *
58 | * @return the ConfigSource name that loaded the property lookup or {@code null} if the property could not be found
59 | */
60 | String getSourceName();
61 |
62 | /**
63 | * The {@link org.eclipse.microprofile.config.spi.ConfigSource} ordinal that loaded the property lookup.
64 | *
65 | * @return the ConfigSource ordinal that loaded the property lookup or {@code 0} if the property could not be found
66 | */
67 | int getSourceOrdinal();
68 | }
69 |
--------------------------------------------------------------------------------
/tck/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
16 |
32 | * Implementations of this interface may supply zero or more {@linkplain ConfigSource configuration source} instances
33 | * for a given application (as defined by the application's {@link ClassLoader}).
34 | *
35 | * Instances of this interface will be {@linkplain ConfigBuilder#addDiscoveredSources() discovered} via the
36 | * {@link java.util.ServiceLoader} mechanism and can be registered by providing a
37 | * {@code META-INF/services/org.eclipse.microprofile.config.spi.ConfigSourceProvider}
38 | * {@linkplain ClassLoader#getResource(String) resource} which contains the fully qualified class name of the custom
39 | * {@code ConfigSourceProvider} implementation.
40 | *
41 | * @author Mark Struberg
42 | * @author Gerhard Petracek
43 | * @author Emily Jiang
44 | */
45 | public interface ConfigSourceProvider {
46 | /**
47 | * Return the {@link ConfigSource} instances that are provided by this provider. The {@link java.lang.Iterable
48 | * Iterable} contains a fixed number of {@linkplain ConfigSource configuration sources}, determined at application
49 | * start time, and the config sources themselves may be static or dynamic. An empty {@link java.lang.Iterable
50 | * Iterable} may be returned if no sources are to be provided.
51 | *
52 | * @param forClassLoader
53 | * the class loader which should be used for discovery and resource loading purposes
54 | * @return the {@link ConfigSource} instances to register to the configuration
55 | */
56 | Iterable
26 | * For many project artifacts (e.g. WAR, EAR) it should be possible to build them only once and then install them at
27 | * different customers, stages, etc. They need to target those different execution environments without the necessity of
28 | * any repackaging. In other words: depending on the situation they need different configuration.
29 | *
30 | *
31 | * This is easily achievable by having a set of default configuration values inside the project artifact. But be able to
32 | * overwrite those default values from external.
33 | *
34 | *
37 | * A Configuration consists of the information collected from the registered
38 | * {@link org.eclipse.microprofile.config.spi.ConfigSource ConfigSources}. These {@code ConfigSources} get sorted
39 | * according to their ordinal. That way it is possible to overwrite configuration with lower importance from
40 | * outside.
41 | *
42 | *
43 | * By default there are 3 ConfigSources:
44 | *
45 | *
53 | * That means that one can put the default configuration in a {@code META-INF/microprofile-config.properties} anywhere
54 | * on the classpath and the Operations team can later simply e.g set a system property to change this default
55 | * configuration.
56 | *
57 | *
58 | * It is of course also possible to register own {@link org.eclipse.microprofile.config.spi.ConfigSource ConfigSources}.
59 | * A {@code ConfigSource} could e.g. read configuration values from a database table, a remote server, etc
60 | *
61 | *
64 | * The configuration of an application is represented by an instance of {@link org.eclipse.microprofile.config.Config}.
65 | * The {@link org.eclipse.microprofile.config.Config} can be accessed via the
66 | * {@link org.eclipse.microprofile.config.ConfigProvider}.
67 | *
68 | *
74 | * Injection via a JSR-330 DI container is also supported:
75 | *
76 | *
36 | * A {@link Config} provides access to the application's configuration. It may have been automatically discovered, or
37 | * manually created and registered.
38 | *
39 | * The default usage is to use {@link #getConfig()} to automatically pick up the configuration for the current
40 | * thread's {@linkplain Thread#getContextClassLoader() context class loader}.
41 | *
42 | * A configuration consists of information collected from the registered
43 | * {@linkplain org.eclipse.microprofile.config.spi.ConfigSource configuration sources}, combined with the set
44 | * of registered {@linkplain org.eclipse.microprofile.config.spi.Converter converters}. The configuration
45 | * sources get sorted according to their
46 | * {@linkplain org.eclipse.microprofile.config.spi.ConfigSource#getOrdinal() ordinal value}. Thus it is
47 | * possible to override a lower-priority configuration source with a higher-priority one.
48 | *
49 | * It is also possible to register custom configuration sources to flexibly extend the configuration mechanism.
50 | * For example, a configuration source could be provided which reads configuration values from a database table.
51 | *
52 | *
53 | * Example:
54 | *
55 | *
61 | * For more advanced use cases (e.g. registering a manually created {@link Config} instance), please see
62 | * {@link ConfigProviderResolver#registerConfig(Config, ClassLoader)} and {@link ConfigProviderResolver#getBuilder()}.
63 | *
64 | * @author Mark Struberg
65 | * @author Romain Manni-Bucau
66 | * @author Emily Jiang
67 | * @author Viktor Klang
68 | */
69 | public final class ConfigProvider {
70 | private ConfigProvider() {
71 | }
72 |
73 | /**
74 | * Get the {@linkplain Config configuration} corresponding to the current application, as defined by the calling
75 | * thread's {@linkplain Thread#getContextClassLoader() context class loader}.
76 | *
77 | * The {@link Config} instance will be created and registered to the context class loader if no such configuration
78 | * is already created and registered.
79 | *
80 | * Each class loader corresponds to exactly one configuration.
81 | *
82 | * @return the configuration instance for the thread context class loader
83 | */
84 | public static Config getConfig() {
85 | return ConfigProviderResolver.instance().getConfig();
86 | }
87 |
88 | /**
89 | * Get the {@linkplain Config configuration} for the application corresponding to the given class loader instance.
90 | *
91 | * The {@link Config} instance will be created and registered to the given class loader if no such configuration is
92 | * already created and registered.
93 | *
94 | * Each class loader corresponds to exactly one configuration.
95 | *
96 | * @param cl
97 | * the Classloader used to register the configuration instance
98 | * @return the configuration instance for the given class loader
99 | */
100 | public static Config getConfig(ClassLoader cl) {
101 | return ConfigProviderResolver.instance().getConfig(cl);
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/tck/src/main/java/org/eclipse/microprofile/config/tck/profile/ConfigPropertyFileProfileTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2020 Contributors to the Eclipse Foundation
3 | *
4 | * See the NOTICE file(s) distributed with this work for additional
5 | * information regarding copyright ownership.
6 | *
7 | * Licensed under the Apache License, Version 2.0 (the "License");
8 | * You may not use this file except in compliance with the License.
9 | * You may obtain a copy of the License at
10 | *
11 | * http://www.apache.org/licenses/LICENSE-2.0
12 | *
13 | * Unless required by applicable law or agreed to in writing, software
14 | * distributed under the License is distributed on an "AS IS" BASIS,
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | * See the License for the specific language governing permissions and
17 | * limitations under the License.
18 | */
19 | package org.eclipse.microprofile.config.tck.profile;
20 |
21 | import static org.hamcrest.MatcherAssert.assertThat;
22 | import static org.hamcrest.Matchers.equalTo;
23 | import static org.hamcrest.Matchers.is;
24 | import static org.testng.Assert.assertEquals;
25 | import static org.testng.Assert.assertFalse;
26 |
27 | import org.eclipse.microprofile.config.ConfigProvider;
28 | import org.eclipse.microprofile.config.inject.ConfigProperty;
29 | import org.jboss.arquillian.container.test.api.Deployment;
30 | import org.jboss.arquillian.testng.Arquillian;
31 | import org.jboss.shrinkwrap.api.ShrinkWrap;
32 | import org.jboss.shrinkwrap.api.asset.EmptyAsset;
33 | import org.jboss.shrinkwrap.api.asset.StringAsset;
34 | import org.jboss.shrinkwrap.api.spec.WebArchive;
35 | import org.testng.annotations.Test;
36 |
37 | import jakarta.enterprise.context.Dependent;
38 | import jakarta.enterprise.inject.spi.CDI;
39 | import jakarta.inject.Inject;
40 |
41 | /**
42 | * Test cases for Config profile
43 | *
44 | * @author Emily Jiang
45 | */
46 | public class ConfigPropertyFileProfileTest extends Arquillian {
47 | @Deployment
48 | public static WebArchive deployment() {
49 |
50 | WebArchive war = ShrinkWrap
51 | .create(WebArchive.class, "ConfigPropertyFileProfileTest.war")
52 | .addClasses(ConfigPropertyFileProfileTest.class, ProfilePropertyBean.class)
53 | .addAsResource(
54 | new StringAsset(
55 | "mp.config.profile=dev\n" +
56 | "vehicle.name=car\n" +
57 | "vehicle.colour=red"),
58 | "META-INF/microprofile-config.properties")
59 | .addAsResource(new StringAsset(
60 | "vehicle.name=bike\n" +
61 | "vehicle.owner=Bob"),
62 | "META-INF/microprofile-config-dev.properties")
63 | .addAsResource(new StringAsset(
64 | "vehicle.name=bike\n" +
65 | "vehicle.age=5"),
66 | "META-INF/microprofile-config-prod.properties")
67 | .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
68 |
69 | return war;
70 | }
71 |
72 | /**
73 | * Check both the file microprofile-config.properties and microprofile-config-dev.properties are loaded as config
74 | * sources but the file microprofile-config-prod.properties is ignored.
75 | */
76 | @Test
77 | public void testConfigProfileWithDev() {
78 | ProfilePropertyBean bean = CDI.current().select(ProfilePropertyBean.class).get();
79 | assertThat(bean.getName(), is(equalTo("bike")));
80 | assertThat(bean.getColour(), is(equalTo("red")));
81 | assertThat(bean.getOwner(), is(equalTo("Bob")));
82 | assertEquals(bean.getVehicleAge(), 10);
83 | assertThat(ConfigProvider.getConfig().getValue("vehicle.name", String.class), is(equalTo("bike")));
84 | assertThat(ConfigProvider.getConfig().getValue("vehicle.colour", String.class), is(equalTo("red")));
85 | assertThat(ConfigProvider.getConfig().getValue("vehicle.owner", String.class), is(equalTo("Bob")));
86 | assertFalse(ConfigProvider.getConfig().getOptionalValue("vehicle.age", Integer.class).isPresent());
87 | }
88 |
89 | @Dependent
90 | public static class ProfilePropertyBean {
91 | @Inject
92 | @ConfigProperty(name = "vehicle.name")
93 | private String name;
94 |
95 | @Inject
96 | @ConfigProperty(name = "vehicle.age", defaultValue = "10")
97 | private int age;
98 |
99 | @Inject
100 | @ConfigProperty(name = "vehicle.colour", defaultValue = "black")
101 | private String colour;
102 |
103 | @Inject
104 | @ConfigProperty(name = "vehicle.owner", defaultValue = "Jane")
105 | private String owner;
106 |
107 | public String getName() {
108 | return name;
109 | }
110 |
111 | public int getVehicleAge() {
112 | return age;
113 | }
114 |
115 | public String getColour() {
116 | return colour;
117 | }
118 |
119 | public String getOwner() {
120 | return owner;
121 | }
122 | }
123 |
124 | }
125 |
--------------------------------------------------------------------------------
/api/src/main/java/org/eclipse/microprofile/config/spi/ConfigBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation
3 | *
4 | * See the NOTICE file(s) distributed with this work for additional
5 | * information regarding copyright ownership.
6 | *
7 | * Licensed under the Apache License, Version 2.0 (the "License");
8 | * You may not use this file except in compliance with the License.
9 | * You may obtain a copy of the License at
10 | *
11 | * http://www.apache.org/licenses/LICENSE-2.0
12 | *
13 | * Unless required by applicable law or agreed to in writing, software
14 | * distributed under the License is distributed on an "AS IS" BASIS,
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | * See the License for the specific language governing permissions and
17 | * limitations under the License.
18 | *
19 | * Contributors:
20 | * 2016-07-20 - Romain Manni-Bucau
21 | * Initial ConfigBuilder PR 0945b23cbf9dadb75fb9
22 | * 2016-07-17 - Mark Struberg
23 | * Merged and JavaDoc c8525998a43fe798f367
24 | * 2016-11-14 - Emily Jiang / IBM
25 | * API improvements + JavaDoc f53258b8eca1253fee52
26 | */
27 | package org.eclipse.microprofile.config.spi;
28 |
29 | import org.eclipse.microprofile.config.Config;
30 |
31 | /**
32 | * A builder for manually creating a configuration instance.
33 | *
34 | * @see ConfigProviderResolver#getBuilder()
35 | *
36 | * @author Romain Manni-Bucau
37 | * @author Mark Struberg
38 | * @author Emily Jiang
39 | */
40 | @org.osgi.annotation.versioning.ProviderType
41 | public interface ConfigBuilder {
42 | /**
43 | * Add the default configuration sources to the
44 | * configuration being built.
45 | *
46 | * @return this configuration builder instance
47 | */
48 | ConfigBuilder addDefaultSources();
49 |
50 | /**
51 | * Add all configuration sources which can be discovered from this
52 | * configuration builder's {@linkplain #forClassLoader(ClassLoader) class loader}.
53 | *
54 | * @return this configuration builder instance
55 | */
56 | ConfigBuilder addDiscoveredSources();
57 |
58 | /**
59 | * Add all configuration converters which can be discovered from this
60 | * configuration builder's {@linkplain #forClassLoader(ClassLoader) class loader}.
61 | *
62 | * @return this configuration builder instance
63 | */
64 | ConfigBuilder addDiscoveredConverters();
65 |
66 | /**
67 | * Specify the class loader for which this configuration is being built.
68 | *
69 | * @param loader
70 | * the class loader
71 | * @return this configuration builder instance
72 | */
73 | ConfigBuilder forClassLoader(ClassLoader loader);
74 |
75 | /**
76 | * Add the specified {@link ConfigSource} instances to the configuration being built.
77 | *
78 | * @param sources
79 | * the configuration sources
80 | * @return this configuration builder instance
81 | */
82 | ConfigBuilder withSources(ConfigSource... sources);
83 |
84 | /**
85 | * Add the specified {@link Converter} instances to the configuration being built.
86 | *
87 | * The implementation may use reflection to determine the target type of the converter. If the type cannot be
88 | * determined reflectively, this method may fail with a runtime exception.
89 | *
90 | * When using lambda expressions for custom converters you should use the
91 | * {@link #withConverter(Class, int, Converter)} method and pass the target type explicitly, since lambda
92 | * expressions generally do not offer enough type information to the reflection API in order to determine the target
93 | * converter type.
94 | *
95 | * The added converters will be given a priority of {@code 100}.
96 | *
97 | * @param converters
98 | * the converters to add
99 | * @return this configuration builder instance
100 | */
101 | ConfigBuilder withConverters(Converter>... converters);
102 |
103 | /**
104 | * Add the specified {@link Converter} instance for the given type to the configuration being built.
105 | *
106 | * This method does not rely on reflection to determine the target type of the converter; therefore, lambda
107 | * expressions may be used for the converter instance.
108 | *
109 | * The priority value of custom converters defaults to {@code 100} if not specified.
110 | *
111 | * @param type
112 | * the class of the type to convert
113 | * @param priority
114 | * the priority of the converter
115 | * @param converter
116 | * the converter (can not be {@code null})
117 | * @param
27 | * @Inject
28 | * @ConfigProperty(name="myproject.some.endpoint.url");
29 | * private String restUrl;
30 | *
31 | *
32 | *
36 | *
43 | *
44 | * @author Mark Struberg
45 | * @author Emily Jiang
46 | */
47 | @org.osgi.annotation.versioning.Version("2.0")
48 | package org.eclipse.microprofile.config.inject;
49 |
--------------------------------------------------------------------------------
/tck/src/main/java/org/eclipse/microprofile/config/tck/configsources/CustomConfigSourceProvider.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation
3 | *
4 | * See the NOTICE file(s) distributed with this work for additional
5 | * information regarding copyright ownership.
6 | *
7 | * Licensed under the Apache License, Version 2.0 (the "License");
8 | * You may not use this file except in compliance with the License.
9 | * You may obtain a copy of the License at
10 | *
11 | * http://www.apache.org/licenses/LICENSE-2.0
12 | *
13 | * Unless required by applicable law or agreed to in writing, software
14 | * distributed under the License is distributed on an "AS IS" BASIS,
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | * See the License for the specific language governing permissions and
17 | * limitations under the License.
18 | *
19 | */
20 | package org.eclipse.microprofile.config.tck.configsources;
21 |
22 | import java.io.IOException;
23 | import java.net.URL;
24 | import java.util.ArrayList;
25 | import java.util.Enumeration;
26 | import java.util.List;
27 |
28 | import org.eclipse.microprofile.config.spi.ConfigSource;
29 | import org.eclipse.microprofile.config.spi.ConfigSourceProvider;
30 |
31 | /**
32 | * @author Mark Struberg
33 | */
34 | public class CustomConfigSourceProvider implements ConfigSourceProvider {
35 |
36 | @Override
37 | public IterableT where T is a Type where a {@link org.eclipse.microprofile.config.spi.Converter} exists and the
37 | * property must exist.Optional<T> where T is a Type where a {@link org.eclipse.microprofile.config.spi.Converter}
39 | * exists where the property may exist.Provider<T> where T is a Type where a {@link org.eclipse.microprofile.config.spi.Converter}
41 | * exists where the property may exist.Example
40 | *
41 | *
42 | * @ConfigProperties(prefix = "server")
43 | * @Dependent
44 | * public class MyServer {
45 | * public String host; // maps the property name server.host
46 | * public int port; // maps to the property name server.port
47 | * private String context; // maps to the property name server.context
48 | * @ConfigProperty(name = "old.location")
49 | * public String location; // maps to the property name server.old.location
50 | * public String getContext() {
51 | * return context;
52 | * }
53 | * }
54 | *
55 | *
56 | * @since 2.0
57 | * @author Emily Jiang
58 | */
59 | @Target({METHOD, FIELD, PARAMETER, TYPE})
60 | @Retention(RUNTIME)
61 | @Documented
62 | @Qualifier
63 | public @interface ConfigProperties {
64 | String UNCONFIGURED_PREFIX = "org.eclipse.microprofile.config.inject.configproperties.unconfiguredprefix";
65 |
66 | /**
67 | * The prefix of the configuration properties.
68 | *
69 | * @return the configuration property prefix
70 | */
71 | @Nonbinding
72 | String prefix() default UNCONFIGURED_PREFIX;
73 |
74 | /**
75 | * Support inline instantiation of the {@link ConfigProperties} qualifier.
76 | */
77 | final class Literal extends AnnotationLiteralRationale
24 | *
25 | * How it works
35 | *
36 | *
46 | *
51 | *
52 | * Accessing and Using the Configuration
62 | *
63 | *
69 | * Config config = ConfigProvider.getConfig();
70 | * String restUrl = config.getValue("myproject.some.endpoint.url", String.class);
71 | *
72 | *
73 | *
77 | * @Inject
78 | * @ConfigProperty(name="myproject.some.endpoint.url");
79 | * private String restUrl;
80 | *
81 | *
82 | * @author Emily Jiang
83 | * @author Mark Struberg
84 | */
85 | @org.osgi.annotation.versioning.Version("3.0.0")
86 | package org.eclipse.microprofile.config;
87 |
--------------------------------------------------------------------------------
/tck/src/main/java/org/eclipse/microprofile/config/tck/profile/OverrideConfigProfileTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2023 Contributors to the Eclipse Foundation
3 | *
4 | * See the NOTICE file(s) distributed with this work for additional
5 | * information regarding copyright ownership.
6 | *
7 | * Licensed under the Apache License, Version 2.0 (the "License");
8 | * You may not use this file except in compliance with the License.
9 | * You may obtain a copy of the License at
10 | *
11 | * http://www.apache.org/licenses/LICENSE-2.0
12 | *
13 | * Unless required by applicable law or agreed to in writing, software
14 | * distributed under the License is distributed on an "AS IS" BASIS,
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | * See the License for the specific language governing permissions and
17 | * limitations under the License.
18 | */
19 | package org.eclipse.microprofile.config.tck.profile;
20 |
21 | import static org.hamcrest.MatcherAssert.assertThat;
22 | import static org.hamcrest.Matchers.equalTo;
23 | import static org.hamcrest.Matchers.is;
24 |
25 | import org.eclipse.microprofile.config.ConfigProvider;
26 | import org.eclipse.microprofile.config.inject.ConfigProperty;
27 | import org.jboss.arquillian.container.test.api.Deployment;
28 | import org.jboss.arquillian.testng.Arquillian;
29 | import org.jboss.shrinkwrap.api.ShrinkWrap;
30 | import org.jboss.shrinkwrap.api.asset.EmptyAsset;
31 | import org.jboss.shrinkwrap.api.asset.StringAsset;
32 | import org.jboss.shrinkwrap.api.spec.WebArchive;
33 | import org.testng.annotations.Test;
34 |
35 | import jakarta.enterprise.context.Dependent;
36 | import jakarta.enterprise.inject.spi.CDI;
37 | import jakarta.inject.Inject;
38 |
39 | /**
40 | * Test cases for Config profile
41 | *
42 | * @author Oliver Bertuch
43 | */
44 | public class OverrideConfigProfileTest extends Arquillian {
45 | @Deployment
46 | public static WebArchive deployment() {
47 | WebArchive war = ShrinkWrap
48 | .create(WebArchive.class, "OverrideConfigProfileTest.war")
49 | .addClasses(OverrideConfigProfileTest.class, ProfilePropertyBean.class)
50 | .addAsResource(
51 | new StringAsset(
52 | "mp.config.profile=dev\n" +
53 | "%dev." + PROPERTY + "=foo\n" +
54 | PROPERTY + "=bar\n"),
55 | "META-INF/microprofile-config.properties")
56 | .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
57 |
58 | return war;
59 | }
60 |
61 | private static final String PROPERTY = "mp.tck.prop.dummy";
62 | private static final String EXPECTED = "dummy";
63 |
64 | /**
65 | * This test relies on the system property "mp.tck.prop.dummy" being set to "dummy" as described in the TCK README
66 | * as a requirement for runners. System properties are per the TCK requirements at ordinal 120, so shall override
67 | * the given properties in the microprofile-config.properties file (ordinal 100) included in the WAR above.
68 | */
69 | @Test
70 | public void testConfigProfileWithDevAndOverride() {
71 | assertThat(System.getProperty(PROPERTY), is(equalTo(EXPECTED)));
72 |
73 | ProfilePropertyBean bean = CDI.current().select(ProfilePropertyBean.class).get();
74 | assertThat(bean.getConfigProperty(), is(equalTo(EXPECTED)));
75 |
76 | assertThat(ConfigProvider.getConfig().getValue(PROPERTY, String.class), is(equalTo(EXPECTED)));
77 | }
78 |
79 | @Dependent
80 | public static class ProfilePropertyBean {
81 | @Inject
82 | @ConfigProperty(name = PROPERTY)
83 | private String stringProperty;
84 | public String getConfigProperty() {
85 | return stringProperty;
86 | }
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/tck/src/main/java/org/eclipse/microprofile/config/tck/OptionalValuesBean.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation
3 | *
4 | * See the NOTICE file(s) distributed with this work for additional
5 | * information regarding copyright ownership.
6 | *
7 | * Licensed under the Apache License, Version 2.0 (the "License");
8 | * You may not use this file except in compliance with the License.
9 | * You may obtain a copy of the License at
10 | *
11 | * http://www.apache.org/licenses/LICENSE-2.0
12 | *
13 | * Unless required by applicable law or agreed to in writing, software
14 | * distributed under the License is distributed on an "AS IS" BASIS,
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | * See the License for the specific language governing permissions and
17 | * limitations under the License.
18 | *
19 | */
20 | package org.eclipse.microprofile.config.tck;
21 |
22 | import java.util.Optional;
23 | import java.util.OptionalDouble;
24 | import java.util.OptionalInt;
25 | import java.util.OptionalLong;
26 |
27 | import org.eclipse.microprofile.config.inject.ConfigProperty;
28 |
29 | import jakarta.enterprise.context.Dependent;
30 | import jakarta.inject.Inject;
31 |
32 | /**
33 | * Declare a bean for config property injections.
34 | *
35 | * @author Mark Struberg
36 | * @author Emily Jiang
37 | */
38 | @Dependent
39 | public class OptionalValuesBean {
40 | @Inject
41 | @ConfigProperty(name = "my.optional.int.property")
42 | private Optional
56 | * String restUrl = ConfigProvider.getConfig().getValue("myproject.some.remote.service.url", String.class);
57 | * Integer port = ConfigProvider.getConfig().getValue("myproject.some.remote.service.port", Integer.class);
58 | *
59 | *
60 | *