Spring boot獲取yml字段內容為null的各種情況


首先,在resource目錄下配置test.yml文件

A:
  B: http://123.com?
  C: username="lili"&password="123456"
  D: username="lisa"&password="123456"

1.為了調用方便,將參數全部設置為static,結果可想而知,獲取不到,只能是null

package com.example.demo.constants;

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

@Component
public class TestYml {
    public static String B;
    public static String C;
    public static String D;

    public static String getB() {
        return B;
    }
    @Value("${A.B}")
    public static void setB(String b) {
        B = b;
    }

    public static String getC() {
        return C;
    }
    @Value("${A.C}")
    public static void setC(String c) {
        C = c;
    }

    public static String getD() {
        return D;
    }
    @Value("${A.D}")
    public static void setD(String d) {
        D = d;
    }
}
View Code

執行測試代碼

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTests {
    @Test
    public void test(){
        String b = TestYml.B;
        System.out.println(b);
    }
}
View Code

得到結果如下:

2.然后去掉set方法中的static,執行上一步的測試代碼可以正常獲取

 

3.如果需要將B分別和C,D進行拼接呢,將代碼修改如下:

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

@Component
public class TestYml {
    public static String B;
    public static String C;
    public static String D;

    public static String getB() {
        return B;
    }
    @Value("${A.B}")
    public void setB(String b) {
        B = b;
    }

    public static String getC() {
        return C;
    }
    @Value("${A.C}")
    public void setC(String c) {
        C = getB() + c;
    }

    public static String getD() {
        return D;
    }
    @Value("${A.D}")
    public void setD(String d) {
        D = getB() +  d;
    }
}
View Code

執行代碼如下:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTests {
    @Test
    public void test(){
        String b = TestYml.B;
        String c = TestYml.C;
        String d = TestYml.D;
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }
}

拼接的結果時而正常,時而為null,如下:

4.然后將get方法的static也去掉,結果同樣也是不穩定

測試代碼如下:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTests {
    @Test
    public void test(){
        int i = 10;
        for (int i1 = 0; i1 < i; i1++) {
            String b = TestYml.B;
            String c = TestYml.C;
            String d = TestYml.D;
            System.out.println(b);
            System.out.println(c);
            System.out.println(d);
        }
    }
}

結果如下:

5.將@Value至於參數處,且將參數的static也去掉,並且將測試代碼改為注入的方式,結果則是拼接的null都不見了

 

6.然后修改get方法,將拼接的值get作為該參數的返回,調用方式直接使用注入和get方法,獲取值才正常

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

@Component
public class TestYml {
    @Value("${A.B}")
    private String B;
    @Value("${A.C}")
    private String C;
    @Value("${A.D}")
    private String D;

    public String getB() {
        return B;
    }

    public void setB(String b) {
        B = b;
    }

    public String getC() {
        return getB() + C;
    }

    public void setC(String c) {
        C = c;
    }

    public String getD() {
        return getB() + D;
    }

    public void setD(String d) {
        D = d;
    }
}
View Code

測試代碼

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTests {
    @Autowired
    TestYml testYml;
    @Test
    public void test(){
        int i = 10;
        for (int i1 = 0; i1 < i; i1++) {
            String b = testYml.getB();
            String c = testYml.getC();
            String d = testYml.getD();
            System.out.println(b);
            System.out.println(c);
            System.out.println(d);
        }
    }
}
View Code

執行結果可以正常獲取到值

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM