Springでは、静的変数に値を挿入することはできません。たとえば、次のようになります。

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class GlobalValue {

    @Value("${mongodb.db}")
    public static String DATABASE;


}


GlobalValue.DATABASE`を出力すると、

null`が表示されます。

    GlobalValue.DATABASE = null

解決策

それを修正するには、静的変数に注入された値を割り当てるための “none static setter”を作成します。例えば ​​:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class GlobalValue {

    public static String DATABASE;

    @Value("${mongodb.db}")
    public void setDatabase(String db) {
        DATABASE = db;
    }

}

出力

    GlobalValue.DATABASE = "mongodb database name"