- Hands-On Enterprise Java Microservices with Eclipse MicroProfile
- Cesar Saavedra Heiko W. Rupp Jeff Mesnil Pavol Loffay Antoine Sabot Durand Scott Stark
- 306字
- 2021-06-24 12:56:22
Custom converters
If your application defines Java types that do not provide any of these three cases covered by the automatic converters, MicroProfile Config can still provide conversion using custom converters that extend the org.eclipse.microprofile.config.spi.Converter interface defined in the following:
public interface Converter<T> {
/**
* Configure the string value to a specified type
* @param value the string representation of a property value.
* @return the converted value or null
*
* @throws IllegalArgumentException if the value cannot be converted to
the specified type.
*/
T convert(String value);
You have to write an implementation of org.eclipse.microprofile.config.spi.Converter, then add its name to the /META-INF/services/org.eclipse.microprofile.config.spi.Converter file and put that file in your application archive. For your reference, here is an example of the implementation of a custom converter that supports a named number concept:
package io.packt.sample.config;
import org.eclipse.microprofile.config.spi.Converter;
public class NamedNumberConverter implements Converter<NamedNumber> {
/**
* Parses an assignment type of expression into a name and number value
* @param value name=Number expression
* @return NamedNumber instance
*/
@Override
public NamedNumber convert(String value) {
String[] parts = value.split("="); // 1
return new NamedNumber(parts[0], parts[1]);
}
}
package io.packt.sample.config;
public class NamedNumber {
private String name;
private Number number;
public NamedNumber(String name, Number number) {
this.name = name;
this.number = number;
}
...
}
The converter takes a string and splits it based on a comma separator to extract the name and corresponding value to build the NamedNumber instance.
You would then specify a named number in your configuration, as shown here:
# microprofile-config.properties NamedNumber example
injected.namedNumber=jdoe,2.0
The addition of NamedNumberConverter allows us to use the NamedNumber type as a configuration type that can be injected. Here is an example that would match the configuration setting shown previously:
@Inject
@ConfigProperty(name="injected.namedNumber")
NamedNumber configuredNumber;
With the base MicroProfile Config feature covered, let's move onto another feature, MicroProfile Fault Tolerance.
- Modern JavaScript Web Development Cookbook
- 物聯(lián)網(wǎng)安全與深度學(xué)習(xí)技術(shù)
- NB-IoT物聯(lián)網(wǎng)技術(shù)解析與案例詳解
- 物聯(lián)網(wǎng)與無線傳感器網(wǎng)絡(luò)
- 物聯(lián)網(wǎng)技術(shù)與應(yīng)用
- 新手易學(xué):新手學(xué)淘寶開店
- 語音信號(hào)處理及Blackfin DSP實(shí)現(xiàn)
- 工業(yè)互聯(lián)網(wǎng)創(chuàng)新實(shí)踐
- Learning Storm
- Practical Web Penetration Testing
- 現(xiàn)代通信系統(tǒng)(第5版)
- 園區(qū)網(wǎng)絡(luò)架構(gòu)與技術(shù)
- 物聯(lián)網(wǎng)M2M開發(fā)技術(shù):基于無線CPU-Q26XX
- 網(wǎng)絡(luò)基本通信約束下的系統(tǒng)性能極限分析與設(shè)計(jì)
- 物聯(lián)網(wǎng)工程應(yīng)用技術(shù)