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.
推薦閱讀
- Building Django 2.0 Web Applications
- Learning QGIS 2.0
- 無(wú)人機(jī)通信
- Drush User’s Guide
- 網(wǎng)絡(luò)互聯(lián)技術(shù)(實(shí)踐篇)
- 互聯(lián)網(wǎng)安全的40個(gè)智慧洞見:2014年中國(guó)互聯(lián)網(wǎng)安全大會(huì)文集
- 企業(yè)網(wǎng)絡(luò)安全管理
- Unity Artificial Intelligence Programming
- 通信十年:擁抱互聯(lián)網(wǎng)
- 云工廠:開啟中國(guó)制造云時(shí)代
- 5G智慧交通
- SEO攻略:搜索引擎優(yōu)化策略與實(shí)戰(zhàn)案例詳解
- 互聯(lián)網(wǎng)視覺設(shè)計(jì)(全彩慕課版)
- 5G新型多址技術(shù)
- CDN技術(shù)詳解