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

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.

主站蜘蛛池模板: 遂宁市| 石林| 新和县| 农安县| 黄石市| 婺源县| 永济市| 苗栗县| 彭泽县| 巴塘县| 曲阜市| 杭州市| 中西区| 岳阳市| 桐庐县| 新宾| 东港市| 大英县| 含山县| 虎林市| 崇州市| 东乡| 宜兰县| 固镇县| 玛多县| 西充县| 灯塔市| 侯马市| 美姑县| 台安县| 荣成市| 庆元县| 海兴县| 长泰县| 南雄市| 资兴市| 通山县| 湟源县| 泽普县| 本溪市| 平邑县|