springboot加载application.yml文件null
- 话不多说,直接上代码
- 本人项目为maven项目
- 以下是项目结构

- pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1..RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>testSpringBootApplication</groupId>
<artifactId>testSpringBootApplication</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>- 配置文件:application.yml
batch:
buffer:
size:
max:
sleep:
excute:
pool: - 写一个对应次配置文件的类
@Component
public class BatchConfig {
@Value("${batch.buffer.size}")
public String buffer_size;
@Value("${batch.excute.pool}")
public String excute_pool;
@Value("${batch.buffer.max}")
public String buffer_max;
@Value("${batch.buffer.sleep}")
public String buffer_sleep;
}- 因为使用注解autowired一直创建对象失败,所以只能写一个静态类来自动加载BatchConfig
@Component
public class StaticConfig {
public static BatchConfig batchConfigstatic;
@Autowired
private BatchConfig batchConfig;
@PostConstruct
public void initStaticDao() {
batchConfigstatic = this.batchConfig;
}
}service层调用配置文件
@Service
public class TestService {
private String buffermax;
public TestService() {
buffermax = StaticConfig.batchConfigstatic.buffer_max;
System.out.println("size="+buffermax);
}
}启动类
@SpringBootApplication
@EnableConfigurationProperties
@EnableAutoConfiguration
@ComponentScan("com.hywc")
public class Application {
public static void main(String[] args) {
// 启动springboot
//ApplicationContext applicationContext =
SpringApplication.run(Application.class, args);
}
}- 运行结果

- 因之前@Value以及@ConfigurationProperties形式均为null,所以中间加了一层,亲测可用,哈哈!!!
springboot加载application.yml文件null的更多相关文章
- SpringBoot加载自定义yml文件
自定义配置文件(跟SpringBoot的application.yml同一目录下): nlu-parse-rule: title: "NLU响应结果解析规则" desc: &quo ...
- Springboot默认加载application.yml原理以及扩展
Springboot默认加载application.yml原理以及扩展 SpringApplication.run(...)默认会加载classpath下的application.yml或applic ...
- Eclipse下SpringBoot没有自动加载application.properties文件
Eclipse内创建SpringBoot项目,在java/main/resources文件夹下面创建application.properties配置文件,SpringApplication.run后发 ...
- SpringBoot启动如何加载application.yml配置文件
一.前言 在spring时代配置文件的加载都是通过web.xml配置加载的(Servlet3.0之前),可能配置方式有所不同,但是大多数都是通过指定路径的文件名的形式去告诉spring该加载哪个文件: ...
- springboot:读取application.yml文件
现在开发主要使用微服务框架springboot,在springboot中经常遇到读取application.yml文件的情形. 一.概述 开发过程中经常遇到要读取application.yml文件中的 ...
- 使用IDEA开发SpringBoot不加载application.yml配置文件的解决方案
1.如果启动项目不加载application.yml配置文件,那么请确认下是否应用了Resources为项目资源文件夹 2.如果项目起初是可以正常使用的,突然不知道改了什么,然后进行启动项目的时候不加 ...
- (转)spring boot实战(第六篇)加载application资源文件源码分析
原文:http://blog.csdn.net/liaokailin/article/details/48878447
- Spring Boot加载application.properties配置文件顺序规则
SpringApplication会从以下路径加载所有的application.properties文件: 1.file:./config/(当前目录下的config文件夹) 2.file:./(当前 ...
- Springboot 加载配置文件源码分析
Springboot 加载配置文件源码分析 本文的分析是基于springboot 2.2.0.RELEASE. 本篇文章的相关源码位置:https://github.com/wbo112/blogde ...
随机推荐
- 2019巅峰极客CTF-web1(LOL英雄联盟)
今晚有空 以后随缘写博客了 好好沉淀 web1当天做出的队伍很少 其实不难 折腾到最后就差一步 可惜 0x01 读取文件 截图没留了 只留了代码部分. 有个页面 有上传和下载功能 起初 ...
- Linux:sed
[参考文章]:shell中sed命令的用法 [参考文章]:SED 简明教程 1. 简述 sed是一种流编辑器,它是文本处理中非常重要的工具,能够完美的配合正则表达式使用. 执行命令时,一次处理一行内容 ...
- Spring Security整合JWT,实现单点登录,So Easy~!
前面整理过一篇 SpringBoot Security前后端分离,登录退出等返回json数据,也就是用Spring Security,基于SpringBoot2.1.4 RELEASE前后端分离的情况 ...
- Appium Desktop 元素定位和脚本录制功能
Appium Desktop除了可以做Server之外还可以进行元素定位和脚本录制功能,点击放大镜按钮,进入页面设置.开始配置Desired Capabilities. 配置Desired Capab ...
- Connection
作用: * 获取执行sql语句对象 ** createStatement(): 获取Statement对象 ** prepareStatement(String sql): 获取预处理对象 ** pr ...
- Markdown 图片的简单处理
0. 前言 最近写 md 文章的时候发现,在 markdown 里插入一些很长的图片的时候,会显得很不好看,于是去查了一下如何实现 markdown 里图片的并排显示,参考了各个博客内的内容和 mar ...
- 阶段5 3.微服务项目【学成在线】_day05 消息中间件RabbitMQ_4.RabbitMQ研究-安装RabbitMQ
RabbitMQ由Erlang语言开发,Erlang语言用于并发及分布式系统的开发,在电信领域应用广泛,OTP(Open Telecom Platform)作为Erlang语言的一部分,包含了很多基于 ...
- 生动详细解释javascript的冒泡和捕获
原文:Event order 翻译:hh54188的博客 前言:虽然精通jquery,但对它的原型javascript却不是很了解,最近在学习javascript中遇到了一些困难,比如冒泡和捕获,很多 ...
- utgard OPC 主要功能简介
度娘还行,尽管不好用,但所有的开发人员不懈努力地写博客,能得到很多东西! 这里向所有未谋面的博主们致敬! 搜了一堆OPC资料,在这里整理一下,用一个封装类来说明utgard的主要接口.使用了java自 ...
- 通过bat批处理程序如何实现在多个txt文件后面加上相同的一行文字
通过bat批处理程序如何实现在多个txt文件后面加上相同的一行文字 set/p a=输入要增加的文字 for /f "delims=" %%i in ('dir /b *.txt' ...