我有这个application.yaml文件,它有下面的标记,这是值得关注的:
hardware:
sensor:
enable: false
type: 'sensor'
interface:
enable: false
type: 'interface-pcb'
printer:
enable: false
type: mock #or bixolon or epson
camera:
enable: true
type: mock #or joyusing
fingerprint:
enable: true
type: mock #or secugen
document:
enable: true
type: mock #or wentone
barcode-scanner:
enable: true
type: mock #or honeywell
timeout: 25 #must not be greater than 30我希望能够动态地获得每个硬件的类型。我想用地图来处理这个案子,我可以用它。
我想要的地图样本:
"sensor": enable: true
: type: mock如何使用Spring和Java 8实现这一点?
发布于 2022-01-06 11:42:16
创建一个保存属性的类,并使用@ConfigurationProperties注释映射application.yml中的属性
@Component
@ConfigurationProperties
public class ConfProperties {
private Map<String, Hardware> hardware;
public Map<String, Hardware> getHardware() {
return hardware;
}
public void setHardware(Map<String, Hardware> hardware) {
this.hardware = hardware;
}
public static class Hardware{
private boolean enable;
private String type;
private int timeout;
public boolean isEnable() {
return enable;
}
public void setEnable(boolean enable) {
this.enable = enable;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
}
}使用以下配置:
@Autowired
ConfProperties confProperties;https://stackoverflow.com/questions/70606252
复制相似问题