[英]How to read properties with special characters from application.yml in springboot
[英]Not able to access application.yml properties in springboot component
我有一個application.yml
文件,其中包含如下屬性
NAME:
CLASS:
ID: ABC123456
這是我的 Spring Boot 組件類
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Component
@Slf4j
public class ProcessMe {
@Value("${NAME.CLASS.ID}")
String StuId;
public boolean IsRightOrWrong(){
System.out.println(StuId);
}
}
在上面的組件System.out.println(StuId);
總是為空。 當我想使用 Junit 測試類調用這個函數時。 上面的代碼有什么問題?
我想這個解決方案在構造函數中定義
@Component
@Slf4j
public class ProcessMe {
String StuId;
@Autowired
ProcessMe(@Value("${NAME.CLASS.ID}") String StuId) {
this.StuId = StuId;
}
public boolean IsRightOrWrong(){
System.out.println(this.StuId);
}
}
希望有用
如果您可以啟動應用程序,那么我相信您的配置是正確的。 我的意思是StuId
有一些價值。
問題是你如何測試When i am tying to call this function using Junit test class. What's wrong in the above code?
When i am tying to call this function using Junit test class. What's wrong in the above code?
看來您是從單元測試中調用的。 然后你需要克隆 application.yaml 來測試配置文件,然后你的測試容器可以讀取數據,否則你必須模擬你的配置。
順便說一句:Java 對象屬性應該使用駝峰式大小寫 lol
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.