The Config object
The org.eclipse.microprofile.config.Config interface is the entry point to retrieve configuration in a Java application.
There are two ways to get an instance of Config:
The first (and preferred) way is to use CDI to inject it into the code:
@Inject private Config config;
- The second way is to call the static method, org.eclipse.microprofile.config.ConfigProvider#getConfig(), to obtain an instance of Config:
Config config = ConfigProvider.getConfig();
The Config interface provides two methods to retrieve properties:
getValue(String propertyName, Class propertyType): This method throws a runtime exception if the property is not present in the configuration. This method must be used only for mandatory configuration (the application would not be able to function properly in its absence).
getOptionalValue(String propertyName, Class propertyType): This method returns a java.util.Optional object that is empty if the property is not present in the configuration. This method is used for optional configuration.
Both methods will also throw exceptions if the property value, retrieved as String from the configuration, cannot be converted into the expected Java type passed as the second argument (converters are described in a later section).
In both methods, you need to specify the Java type you are expecting from the property. For example, if the property corresponds to a URL, you can get its value as java.net.URL directly:
URL myURL = config.getValue("my.url", URL.class);
The Config interface also defines methods to list config sources and all of the properties:
- Iterable<String>getPropertyNames() returns the names of the properties from all of the sources of configuration provided by the Config object.
- Iterable<ConfigSource>getConfigSources() returns all of the sources of configurations provided by the Config object.
- 物聯(lián)網(wǎng)與北斗應(yīng)用
- Building Django 2.0 Web Applications
- 從區(qū)塊鏈到Web3:構(gòu)建未來互聯(lián)網(wǎng)生態(tài)
- OpenLayers Cookbook
- Go Web Scraping Quick Start Guide
- 數(shù)字通信同步技術(shù)的MATLAB與FPGA實(shí)現(xiàn):Altera/Verilog版(第2版)
- 新手易學(xué):新手學(xué)淘寶開店
- 光纖通信系統(tǒng)與網(wǎng)絡(luò)(修訂版)
- 移動(dòng)物聯(lián)網(wǎng):商業(yè)模式+案例分析+應(yīng)用實(shí)戰(zhàn)
- 5G非正交多址接入技術(shù):理論、算法與實(shí)現(xiàn)
- 全聯(lián)網(wǎng)標(biāo)識(shí)服務(wù)
- 新媒體交互藝術(shù)
- 趣話通信:6G的前世、今生和未來
- 從實(shí)踐中學(xué)習(xí)Kali Linux網(wǎng)絡(luò)掃描
- Learning Dart