官术网_书友最值得收藏!

The @ConfigProperty annotation

The @ConfigProperty annotation can be used to inject configuration values in Java fields or method parameters using CDI, as shown:

@Inject
@ConfigProperty(name="my.url")
private URL myURL;

The @ConfigProperty annotation can have defaultValue, which is used to configure the field if the configuration property is not found in the underlying Config:

@Inject
@ConfigProperty(name="my.url", defaultValue="http://localhost/")
private URL myURL;

If defaultValue is not set and no property is found, the application will throw DeploymentException as it cannot be properly configured.

It is possible to use Optional if a configuration property might not be present, as shown in the following code block:

@Inject
@ConfigProperty(name="my.url")
private Optional<URL> someUrl; // will be set to Optional.empty if the
                               // property `my.url` cannot be found

After reading the configuration, we need to provide source configuration sources, which will be covered in the next section.

主站蜘蛛池模板: 绍兴县| 陆川县| 阿克陶县| 安丘市| 东平县| 轮台县| 正镶白旗| 珲春市| 双牌县| 鲁甸县| 日土县| 涟源市| 九江市| 阳谷县| 西平县| 枞阳县| 翁牛特旗| 景谷| 隆子县| 澎湖县| 昭通市| 邳州市| 板桥市| 大方县| 微山县| 保山市| 长春市| 庆阳市| 福清市| 花莲市| 宝应县| 青川县| 阳高县| 麻城市| 武城县| 涪陵区| 祁连县| 宝清县| 都昌县| 宿松县| 崇阳县|