用这样的application.properties
application:
api:
clients:
api1:
url: http://url1
api2:
url: http://url2
basicAuth:
username: user2
password: password2
I can do
@Configuration
@ConfigurationProperties(prefix = "application.api")
@Data
public class ApiProperties {
private Map<String, Client> clients;
@Data
public static class Client {
private String url;
private BasicAuth basicAuth;
}
@Data
public static class BasicAuth {
private String username;
private String password;
}
}
它正在工作。
但是是否有一种方法可以只检索一个客户端?在
的位置private Map<String, Client> clients;
我想要一些像
@Value("${application.api.clients['api1']}")
private Client client1;
我尝试了多种写法但我总是用Could not resolve placeholder...
有解决办法吗?
看起来这对于嵌套属性或层次属性是不可能的。
@Value("${application.api.clients['api1']}")
private Client client1;
但是如果您只想检索一个客户端,您可以尝试这样做。
@Configuration
@ConfigurationProperties(prefix = "application.api.clients")
@Data
public class ApiProperties {
private Client api1;
@Data
public static class Client {
private String url;
private BasicAuth basicAuth;
}
@Data
public static class BasicAuth {
private String username;
private String password;
}
}