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

执行结果可以正常获取到值

猜你喜欢

转载自www.cnblogs.com/biyuting/p/11184254.html