我正在尝试使用@ConfigurationProperties 获取变量的空值从 application.properties 文件中读取属性,尝试使用 @Value 也有同样的问题。有没有其他方法可以将属性加载到应用程序中
@Component
@ConfigurationProperties(prefix = "email")
public class EmailProperties {enter code here
private String host;
private String port;
private String username ;
private String password;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
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;
}
@Override
public String toString() {
return "EmailProperties [host=" + host + ", port=" + port + ", username=" + username + ", password=" + password
+ "]";
}
属性文件
email.host=test
email.port=25
email.username=Test
email.password=Test
使用这些属性的方法
@Autowired
private EmailProperties emailProperties;
@SuppressWarnings("unused")
public void sendMail( String toEmail, String messageSubject, String messageBody) {
System.out.println("host");
Properties prop = new Properties();
prop.put("mail.smtp.auth", false);
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.host", emailProperties.getHost());
prop.put("mail.smtp.port", emailProperties.getPort());
prop.put("mail.smtp.ssl.trust", emailProperties.getHost());