2 回答

TA貢獻1775條經驗 獲得超8個贊
MailTrainAPI
應該是一個 Spring bean,又名組件,由于 自動掃描@Component
,然后注入@Value("${mailtrain.url}")
.
但是,當您調用 時,您自己創建了一個單獨的類實例new MailTrainAPI()
。不要那樣做。
您使用該對象的代碼必須通過注入字段來接收它,例如
@Autowired private MailTrainAPI mt;

TA貢獻1776條經驗 獲得超12個贊
我能夠修復它
public void sendMail(MultiValueMap<String, String> map) {
try {
setAPI();
} catch (IOException e) {
Logger.error(this.getClass(), "sendMail()", "Cannot send mail. Cannot load mailtrain.url property: " + e.getMessage());
return;
}
if (API == null) {
Logger.error(this.getClass(), "sendMail()", "Cannot send mail. Cannot load mailtrain.url property.");
return;
}
System.out.println("API = "+API);
...
private void setAPI() throws IOException {
InputStream is = this.getClass().getResourceAsStream("/application.properties");
Properties p = new Properties();
p.load(is);
API = p.getProperty("mailtrain.url");
}
但我認為會有一種更簡單、更好的方法,而且絨毛更少。
添加回答
舉報