我已经为config创建了一个WebserviceCredentials类,如下所示,在@component类中使用@autowired for WebserviceCredentials它不起作用(显示空值),但是当在@restcontroller类中使用它时,它可以工作,感谢你的帮助
@Component
@ConfigurationProperties(prefix="webservice")
public class WebserviceCredentials {
@Value("${webservice.EndPoint}")
private String webserviceEndpoint;
@Value("${webservice.Username}")
private String username;
@Value("${webservice.Password}")
private String password;
public String getwebserviceEndpoint() {
return webserviceEndpoint;
}
public void setwebserviceEndpoint(String webserviceEndpoint) {
this.webserviceEndpoint = webserviceEndpoint;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
答案 0 :(得分:0)
设置并不是如何使用ConfigurationProperties
@ConfigurationProperties(prefix="webservice")
public class WebserviceCredentials {
private String endpoint;
private String username;
private String password;
public String getEndpoint() {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
然后将@EnableConfigurationProperties(WebserviceCredentials.class)
添加到Configuration
或主应用程序类。
答案 1 :(得分:0)
@ConfigurationProperties(prefix =" webservice")应驻留在代表您的属性文件的POJO上。要在WebserviceCredentials类中使用它,可以使用
public class WebserviceCredentials {
@Autowire
private ConfigurationProperties configurationProperties;
//the rest of your code
}
您可以参考我刚刚发布的这个链接