spring @value 注入static

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

@Component
public class GlobalValue {

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


}

DATABASE的值是nullspring

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;
    }

}

DATABASE能夠獲取到值mongodb

相關文章
相關標籤/搜索