springboot中static靜態工具方法獲取配置文件屬性值

 工具類:

@Component
public class RedisUtils implements InitializingBean{
    // Redis服務器IP
    public static String IP;
    // Redis的端口號
    public static int PORT;
    // 訪問密碼
    public static String AUTH;
    // Redis的節點
    public static String INDEX;
    
    @Value("${host}")
    private String ip;
    @Value("${port}")
    private int port;
    @Value("${password}")
    private String auth;
    @Value("${index}")
    private String index;

    @Override
	public void afterPropertiesSet() throws Exception {
	   try {
			IP = ip;
			PORT = port;
			AUTH = auth;
			INDEX = index;
            //......
	   } catch (Exception e) {
	       e.printStackTrace();
	   }
	}

}

application.properties

host=127.0.0.1
port=21822
password=123456
index=0

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章