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

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.

主站蜘蛛池模板: 林周县| 嘉荫县| 鄂托克前旗| 裕民县| 漾濞| 钟山县| 阳东县| 东至县| 贡嘎县| 泰来县| 康保县| 绥阳县| 南通市| 固始县| 藁城市| 竹溪县| 建水县| 拜泉县| 岳阳市| 财经| 达拉特旗| 于都县| 芒康县| 绥芬河市| 广东省| 通渭县| 西安市| 定边县| 惠州市| 宣武区| 昌吉市| 金平| 白玉县| 垫江县| 应城市| 饶河县| 五家渠市| 平南县| 江阴市| 彩票| 乡城县|