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;</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> String getB() { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> B; } @Value(</span>"${A.B}"<span style="color: #000000;">) </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setB(String b) { B </span>=<span style="color: #000000;"> b; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> String getC() { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> C; } @Value(</span>"${A.C}"<span style="color: #000000;">) </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setC(String c) { C </span>=<span style="color: #000000;"> c; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> String getD() { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> D; } @Value(</span>"${A.D}"<span style="color: #000000;">) </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setD(String d) { D </span>=<span style="color: #000000;"> d; }
}
执行测试代码

@RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) public class ApplicationTests { @Test public void test(){ String b = TestYml.B; System.out.println(b); } }
得到结果如下:
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;</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> String getB() { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> B; } @Value(</span>"${A.B}"<span style="color: #000000;">) </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setB(String b) { B </span>=<span style="color: #000000;"> b; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> String getC() { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> C; } @Value(</span>"${A.C}"<span style="color: #000000;">) </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setC(String c) { C </span>= getB() +<span style="color: #000000;"> c; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> String getD() { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> D; } @Value(</span>"${A.D}"<span style="color: #000000;">) </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setD(String d) { D </span>= getB() +<span style="color: #000000;"> d; }
}
执行代码如下:
@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;</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getB() { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> B; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setB(String b) { B </span>=<span style="color: #000000;"> b; } </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getC() { </span><span style="color: #0000ff;">return</span> getB() +<span style="color: #000000;"> C; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setC(String c) { C </span>=<span style="color: #000000;"> c; } </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getD() { </span><span style="color: #0000ff;">return</span> getB() +<span style="color: #000000;"> D; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setD(String d) { D </span>=<span style="color: #000000;"> d; }
}
测试代码

@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); } } }
执行结果可以正常获取到值
原文地址:https://www.cnblogs.com/biyuting/p/11184254.html