首先,在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. [转]从客户端中检测到有潜在危险的 Request.Form 值。

    参考资料: ASP.NET 4.0中使用FreeTextBox和FCKeditor遇到安全问题警告的解决办法关于问题出现的原因说的很清楚 引言 本人在.NET 4.0+VS2010环境下调试一个ASP ...

  2. 2019-8-31-C#-通过编程的方法在桌面创建回收站快捷方式

    title author date CreateTime categories C# 通过编程的方法在桌面创建回收站快捷方式 lindexi 2019-08-31 16:55:58 +0800 201 ...

  3. InsightFace源码以及pre-train模型以及使用

    一下摘自:https://blog.csdn.net/Fire_Light_/article/details/79602705 论文链接:ArcFace: Additive Angular Margi ...

  4. Scala 方法与函数简单记录

    /** * Scala 方法与函数 * Scala 有方法与函数,二者在语义上的区别很小.Scala 方法是类的一部分,而函数是一个对象可以赋值给一个变量.换句话来说在类中定义的函数即是方法 */ o ...

  5. 微服务配置中心实战:Spring + MyBatis + Druid + Nacos

    在结合场景谈服务发现和配置中我们讲述了 Nacos 配置中心的三个典型的应用场景,包括如何在 Spring Boot 中使用 Nacos 配置中心将数据库连接信息管控起来,而在“原生”的 Spring ...

  6. net.sf.jsqlparser.statement.select.PlainSelect.getGroupBy()Lnet/sf/jsqlparse

    添加pom依赖 <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>js ...

  7. System.Web.Mvc.HttpPutAttribute.cs

    ylbtech-System.Web.Mvc.HttpPutAttribute.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, P ...

  8. 【Vue】组件的基础与组件间通信

    转载:https://segmentfault.com/a/1190000016409329 Vue.js 最核心的功能就是组件(Component),从组件的构建.注册到组件间通信,Vue .x 提 ...

  9. Vue入坑——vue-cli(脚手架)目录结构认识

    转载:https://my.oschina.net/u/3802541/blog/1809182 一.目录结构 |-- build                            // 项目构建 ...

  10. 9.1 mongo_python.py

    # 安装 pymongo pip install pymongo import pymongo try: # 1.链接mongod的服务 mongo_py = pymongo.MongoClient( ...