zoukankan      html  css  js  c++  java
  • 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);
        }
    }
    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;

    </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;
    }
    

    }

    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;

    </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;
    }
    

    }

    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

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

    原文地址:https://www.cnblogs.com/biyuting/p/11184254.html

  • 相关阅读:
    曲线与直线相切
    两函数切线
    导数+放缩
    选取拟合函数
    二分法求零点次数
    奇函数+方程的根与零点
    对数函数绝对值交点问题
    判断函数相等+判断反函数+判断周期函数
    已知分段函数零点个数求范围
    2020-05-03 助教一周小结(第十二周)
  • 原文地址:https://www.cnblogs.com/jpfss/p/11357495.html
Copyright © 2011-2022 走看看