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 ...
随机推荐
- PHP+CI框架+Memcache集成
一.目录结构 二.具体代码 MemcacheCluster.php <?php /** * 一致性哈希memcache分布式,采用的是虚拟节点的方式解决分布均匀性问题,查找节点采用二分法快速查找 ...
- spring事务配置异常
spring事务配置不回滚spring事务管理配置,一般来说都是可以回滚的,最近在开发的过程中遇到了一个异常不回滚的问题,最终找到了原因,贴出来一下 1.首先这里定义一个接口 在接口中定义几个方法 2 ...
- tensorflow训练时用到的一些“工具”
1.graph和参数的store和restore 2.tensorboard查看 2.1tensorboard根据.meta文件查看图 2.2如何看图
- PostgreSQL判断一个表是否存在
postgresql判断一个表是否存在: 方法一: select count(*) from pg_class where relname = 'tablename'; 方法二: select cou ...
- linux系统安装Memcache
Linux系统安装memcached 首先要先安装libevent库. centos 下执行 yum install libevent libevent-devel 查看memcached 是否已经 ...
- JVM的线程
我们使用java命令来运行一个程序,那么就需要启动JVM , 而jvm的启动就相当于启动了一个进程 , 而这个进程在启动的时候会自动启动一个线程,由这个线程去调用main方法,而这个线程就是主线程 ; ...
- 安装mysql数据库及问题解决方法
1.mysql官网下载安装包,官网地址:www.mysql.com [root@seiang software]# ll total 580020 -rw-r--r--. 1 root root 59 ...
- IEnumerable和IQueryable口的区别
IQueryable: 动态表达式树拼接查询语句,把拼接后查询语句进行执行:Execute触发,延迟加载IEnumerable:对内存中的数据,动态拼接查询语句,进行查询:ToList触发,延迟加载: ...
- 使用 bash 脚本把 AWS EC2 数据备份到 S3
目录 一.IAM 秘钥授权方式(普通) 1.1.打开 IAM 1.2.添加用户 1.3.安装和配置 AWS CLI 1.4.配置授权 二.IAM 角色授权方式(安全) 2.1.创建一个 EC2 访问 ...
- nrpe command
1. nrpe 连接问题: 报错:/usr/local/nagios/libexec/check_nrpe -H destip ; CHECK_NRPE: Error - Could no ...