今天剛好遇到了一個@Value 註解取不到值的問題

前言:

今天剛好碰到了一個@Value 註解取不到值的問題 ,我們一起看一下,先看一下這個問題是怎麼樣的。

我寫了一個工具類 ParameterParentUtil

@Value()註解一般用來取配置文件中的值,剛好我需要動態獲取配置文件中的值。

 

package com.test.util;

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

/**
 * @Author tanghh
 * @Date 2020/6/29 16:47
 */
public class ParameterParentUtil {
    private static String code;
    @Value("${spring.profiles.active}")
    private static String environment;

   public ParameterParentUtil(){
       System.out.println("構造方法執行--------");
   }

    static{
        System.out.println("static方法執行--------");

        if(environment.equals("test")){
            code = "100001";
        }else{
            code = "100002";
        }
    }

    public static void getCode(){
        System.out.println("code的值----"+code);
    }


}

我們來執行一下上述代碼。 

 

package com.test.test;

import com.test.util.ParameterParentUtil;

/**
 * @Author tanghh
 * @Date 2020/6/29 16:54
 */
public class parameterTest {
    public static void main(String[]args){
        //1.獲取code的參數值
        ParameterParentUtil.getCode();

    }
}

運行程序發現 報錯了,是我的 environment 這個變量獲取不到值。

 

報錯原因:

 1.@Value 需要通過依賴注入的方式進來,也就是說ParameterParentUtil 這個類需要加上@Service @Component 註解 就可以。

2.@Value 不能作用於靜態變量(用static 修飾),不能作用於常量(final 修飾)

3.如果是在配置工具類獲取值的話,可以通過另外一種方式也可以將值獲取到。

接下來我來講一下第三點的實現方式:

 public static Object getParamentFromProperties(String key) {
        InputStream is = ParameterParentUtil.class.getClassLoader().getResourceAsStream("application.properties");
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        Properties props = new Properties();
        try {
            props.load(br);
            return props.get(key);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

修改之後 ParameterParentUtil中的值:

package com.test.util;

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * @Author tanghh
 * @Date 2020/6/29 16:47
 */
public class ParameterParentUtil {
    private static String code;
//    @Value("${spring.profiles.active}")
//    private static String environment;

   public ParameterParentUtil(){
       System.out.println("構造方法執行--------");
   }

    static{
        System.out.println("static方法執行--------");
        String environment = (String) getParamentFromProperties("spring.profiles.active");
        System.out.println(environment);
        if(environment.equals("test")){
            code = "100001";
        }else{
            code = "100002";
        }
    }

    public static void getCode(){
        System.out.println("code的值----"+code);
    }

    public static Object getParamentFromProperties(String key) {
        InputStream is = ParameterParentUtil.class.getClassLoader().getResourceAsStream("application.properties");
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        Properties props = new Properties();
        try {
            props.load(br);
            return props.get(key);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

接下來我們運行一下程序 再測試一下這個功能,可以看到現在程序不報錯了,問題已經解決。

 


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