我想知道什么时候静态变量初始化为默认值.是否正确加载类时,会创建(分配)静态变量,然后执行声明中的静态初始化和初始化?在什么时候给出默认值?这导致了前向参考的问题.
另外请您解释一下这个问题,为什么静态字段没有及时初始化?特别是Kevin Brock在同一网站上给出的答案.我无法理解第三点.
我正在尝试获取财产的价值
hello.world=Hello World
Run Code Online (Sandbox Code Playgroud)
在 MainApp 类中
@SpringBootApplication
public class MainApp {
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
Run Code Online (Sandbox Code Playgroud)
这不是它的主要方法。
@Value("${hello.world}")
public static String helloWorld;
Run Code Online (Sandbox Code Playgroud)
也许它可以加载
Properties prop = new Properties();
// load a properties file
prop.load(new FileInputStream(filePath));
Run Code Online (Sandbox Code Playgroud)
在 SpringApplication.run 之前的 SpringBoot 的 main 方法中,有没有其他更好的方法来使用 Spring 获取属性