首先,在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("$")
private String B;
@Value("$")
private String C;
@Value("$")
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

Spring boot获取yml字段内容为null的各种情况的更多相关文章

  1. Spring Boot获取前端页面参数的几种方式总结

    Spring Boot的一个好处就是通过注解可以轻松获取前端页面的参数,之后可以将参数经过一系列处理传送到后台数据库. 获得的方式有很多种,这里稍微总结一下,大致分为以下几种: 1.指定前端url请求 ...

  2. spring boot(10) 基础学习内容

    A Spring boot(10) 基础学习内容 B SpringBoot(16) 基础学习内容

  3. Spring Boot 获取 java resources 下文件

    Spring Boot 获取 java resources 下文件 Spring Boot 获取 resources 目录下的目录(例:获取 resources 目录下的 template 目录): ...

  4. spring boot 接口返回值去掉为null的字段

    现在项目都是前后端分离的,返回的数据都是使用json,但有些接口的返回值存在 null或者"",这种字段不仅影响理解,还浪费带宽,需要统一做一下处理,不返回空字段,或者把NULL转 ...

  5. Spring Boot 获取ApplicationContext

    package com.demo; import org.springframework.beans.BeansException; import org.springframework.contex ...

  6. Spring Boot 获取Bean对象实体

    一.实现 ApplicationContextAware 接口 package com.zxguan; import org.springframework.beans.BeansException; ...

  7. 【spring Boot】spring boot获取资源文件的三种方式【两种情况下】

    首先声明一点,springboot获取资源文件,需要看是 1>从spring boot默认的application.properties资源文件中获取 2>还是从自定义的资源文件中获取 带 ...

  8. Spring Boot 获取yaml配置文件信息

    Spring boot 项目启动过程中: org.springframework.boot.SpringApplication#prepareEnvironment 当程序步入listeners.en ...

  9. spring boot的 yml和properties的对比

    Spring Boot 虽然做了大量的工作来简化配置,但其配置依然是相当的复杂!支持的外部配置方式就有很多种,笔者没有去统计,也许是为了灵活使用吧.   application.yml 和 appli ...

随机推荐

  1. MySQL 10章_视图、事务

    一. 视图: 视图(view)是保存了查询语句的一种数据库对象,其数据来源是查询语句对应的数据表,他的结果与数据表查询的结果一样也是一张虚拟的数据表 . 为什么需要视图: ) 不同的用户关心的数据可能 ...

  2. 笔记:Python防止SQL注入

    非安全的方式,使用动态拼接SQL 输入' or 1 = 1 or '1 sql ="""SELECT * FROM goods WHERE name = '%s';&qu ...

  3. JS对象 字符串分割 split() 方法将字符串分割为字符串数组,并返回此数组。 语法: stringObject.split(separator,limit)

    字符串分割split() 知识讲解: split() 方法将字符串分割为字符串数组,并返回此数组. 语法: stringObject.split(separator,limit) 参数说明: 注意:如 ...

  4. VGDB提示

    由于之前地址的版本在未安装.Net 4.0的电脑上安装会出现插件使用装载失败的问题,已更新,新地址为:http://pan.baidu.com/s/1xdnuD,此版本需要.Net 2.0.

  5. code+第四次网络赛div2

    T1 组合数问题: 用k个不完全相同的组合数表示一个数n. 用k-1个1和一个n-k+1表示即可. #include<cstdio> using namespace std; int x, ...

  6. yum -y install python-devel

    yum -y install python-devel的时候报错如图: Could not retrieve mirrorlist http://mirrorlist.centos.org/?rele ...

  7. 校园商铺-2Logback配置与使用-3验证配置

    1. 验证logback配置 1.1. 启动tomcat,得到CATALINA_BASE地址: 1.2 访问接口,查看日志 浏览器打开http://localhost:18080/o2o/supera ...

  8. 同步锁与GIL的关系

    #_author:来童星#date:2019/12/2# Python的线程在GIL的控制之下,线程之间,对整个python解释器,对python提供的CAPI的访问都是互斥的,# 这可以看作是Pyt ...

  9. Windows del

    删除一个或数个文件. DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] namesERASE [/P] [/F] [/S] [/Q] [/A[[:]attribu ...

  10. R语言 循环

    R语言循环 可能有一种情况,当你需要执行一段代码几次. 通常,顺序执行语句. 首先执行函数中的第一个语句,然后执行第二个语句,依此类推. 编程语言提供允许更复杂的执行路径的各种控制结构. 循环语句允许 ...