• 话不多说,直接上代码
  • 本人项目为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的更多相关文章

  1. SpringBoot加载自定义yml文件

    自定义配置文件(跟SpringBoot的application.yml同一目录下): nlu-parse-rule: title: "NLU响应结果解析规则" desc: &quo ...

  2. Springboot默认加载application.yml原理以及扩展

    Springboot默认加载application.yml原理以及扩展 SpringApplication.run(...)默认会加载classpath下的application.yml或applic ...

  3. Eclipse下SpringBoot没有自动加载application.properties文件

    Eclipse内创建SpringBoot项目,在java/main/resources文件夹下面创建application.properties配置文件,SpringApplication.run后发 ...

  4. SpringBoot启动如何加载application.yml配置文件

    一.前言 在spring时代配置文件的加载都是通过web.xml配置加载的(Servlet3.0之前),可能配置方式有所不同,但是大多数都是通过指定路径的文件名的形式去告诉spring该加载哪个文件: ...

  5. springboot:读取application.yml文件

    现在开发主要使用微服务框架springboot,在springboot中经常遇到读取application.yml文件的情形. 一.概述 开发过程中经常遇到要读取application.yml文件中的 ...

  6. 使用IDEA开发SpringBoot不加载application.yml配置文件的解决方案

    1.如果启动项目不加载application.yml配置文件,那么请确认下是否应用了Resources为项目资源文件夹 2.如果项目起初是可以正常使用的,突然不知道改了什么,然后进行启动项目的时候不加 ...

  7. (转)spring boot实战(第六篇)加载application资源文件源码分析

    原文:http://blog.csdn.net/liaokailin/article/details/48878447

  8. Spring Boot加载application.properties配置文件顺序规则

    SpringApplication会从以下路径加载所有的application.properties文件: 1.file:./config/(当前目录下的config文件夹) 2.file:./(当前 ...

  9. Springboot 加载配置文件源码分析

    Springboot 加载配置文件源码分析 本文的分析是基于springboot 2.2.0.RELEASE. 本篇文章的相关源码位置:https://github.com/wbo112/blogde ...

随机推荐

  1. OpenJudge1.5.6:整数序列的元素最大跨度值

    描述 给定一个长度为n的非负整数序列,请计算序列的最大跨度值(最大跨度值 = 最大值减去最小值). 输入一共2行,第一行为序列的个数n(1 <= n <= 1000),第二行为序列的n个不 ...

  2. kubernetes部署nginx/tomcat

    kubernetes集群已经部署好了,需要的话可以参考之前的文章https://www.cnblogs.com/winter1519/p/10015420.html [root@master tomc ...

  3. JAVA基础知识|小知识点

    1.强烈建议,不使用char类型 那么,到底为什么java里不推荐使用char类型呢?其实,1个java的char字符并不完全等于一个unicode的字符.char采用的UCS-2编码,是一种淘汰的U ...

  4. varnish web cache服务

    varnish介绍 缓存开源解决方案: - varnish - 充分利用epoll机制(能显著提高程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率),并发量大,单连接资源较轻 - squid ...

  5. tp5 回滚事务记录,其中一条语句报错,全部回滚

    #################################### 测试事务 // 启动事务 Db::startTrans(); try { //插入行为表 $data = [ 'userId' ...

  6. go -- application/x-www-form-urlencoded发送post数据

  7. PHP之Memcache和Memcached

    本篇文章向大家介绍的是PHP中的Memcache和Memcached ,有兴趣的朋友可以看一下 **Memcache介绍:**Memcache是一套分布式缓存系统,分布式就是说可以在多台服务器上同时安 ...

  8. fcgi vs. gunicorn vs. uWSGI

    fcgi vs. gunicorn vs. uWSGI - Peterbe.comhttps://www.peterbe.com/plog/fcgi-vs-gunicorn-vs-uwsgi uWSG ...

  9. Google Python Style Guide

    https://google.github.io/styleguide/pyguide.html

  10. vue的路由认识

    this.$router.options.routes //获得整个路由路径对象 this.$route.matched //获得当前路由的路径对象