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.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.4.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: 100
         max: 60
         sleep: 10
      excute:
          pool: 50
  • 写一个对应次配置文件的类
  • @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,所以中间加了一层,亲测可用,哈哈!!!

原文地址:https://www.cnblogs.com/lxyuuuuu/p/10949960.html

时间: 2024-08-30 16:11:33

springboot加载application.yml文件null的相关文章

application.properties和application.yml文件的区别

关于 一般上来说,当我们创建一个SpringBoot项目时,IDE会默认帮我们创建一个application.properties配置文件.有些朋友习惯把.properties文件改成.yml文件.那么这两种文件类型有什么区别呢? 区别 1.内容格式比较: .properties文件,通过.来连接,通过=来赋值,结构上,没有分层的感觉,但比较直接. .yml文件,通过:来分层,结构上,有比较明显的层次感,最后key赋值的:后需要留一个空格 2.执行顺序 如果工程中同时存在application.

ThinkPHP 3.2.3 自动加载公共函数文件的方法

方法一.加载默认的公共函数文件 在 ThinkPHP 3.2.3 中,默认的公共函数文件位于公共模块 ./Application/Common 下,访问所有的模块之前都会首先加载公共模块下面的配置文件(Conf/config.php)和公共函数文件(Common/function.php),即默认的公共函数文件为 ./Application/Common/Common/function.php. 例如,在 ./Application/Common/Common 下新建 function.php,

SpringBoot使用@Value从yml文件取值为空--注入静态变量

SpringBoot使用@Value从yml文件取值为空--注入静态变量 1.application.yml中配置内容如下: pcacmgr:   publicCertFilePath: E:\\pcacmgr\\CerFiles\\xh_public.cer   encPublicCertFilePath: E:\\pcacmgr\\CerFiles\\hjzf_encPublic.cer   encPfxFilePath: E:\\pcacmgr\\CerFiles\\hjzf_encPfx

xBIM 实战01 在浏览器中加载IFC模型文件

一.创建Web项目 打开VS,新建Web项目,选择 .NET Framework 4.5 选择一个空的项目 新建完成后,项目结构如下: 二.添加webServer访问文件类型 由于WexXplorer 加载的是 .wexBIM格式的文件或者文件流,所以需要在Web.config文件中添加如下配置 <system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLeng

SpringBoot加载配置文件(@[email&#160;protected]@Value)

情景描述 最近新搭建了一个项目,从Spring迁到了Springboot,为了兼容Spring加载配置文件的风格,所以还想把PropertyPlaceholderConfigurer放在.xml文件里面,然后通过@importSource来加载.xml文件将配置加载到spring环境中,通过@value或者PropertyUtil来引入对应配置的值.于是发现了以下问题,并根据这些问题进行了问题拓展. 问题描述 1.在.xml引入PropertyPlaceholderConfigurer时,若项目

ios UIWebView 加载网页、文件、 html

UIWebView  是用来加载加载网页数据的一个框.UIWebView可以用来加载pdf word doc 等等文件 生成webview 有两种方法,1.通过storyboard 拖拽 2.通过alloc init 来初始化 创建webview,下列文本中 _webView.dataDetectorTypes = UIDataDetectorTypeAll; 是识别webview中的类型,例如 当webview中有电话号码,点击号码就能直接打电话 - (UIWebView *)webView

使用iFrame动态加载Application Cache

为了避免缓存主页面,一般使用动态iFrame的方式来加载Application Cache,方法如下: 1 updateImageCache: function () { 2 3 if (null == $("iframe")) { 4 5 } else { 6 $("iframe").remove(); 7 } 8 9 var iframe = document.createElement('iframe'); 10 iframe.setAttribute('wi

springboot不加载bootstrap.properties文件

1.首先说一下官网对bootstrap和application两种配置文件的区别: Spring Cloud 构建于 Spring Boot 之上,在 Spring Boot 中有两种上下文,一种是 bootstrap, 另外一种是 application, bootstrap 是应用程序的父上下文,也就是说 bootstrap 加载优先于 applicaton.bootstrap 主要用于从额外的资源来加载配置信息,还可以在本地外部配置文件中解密属性.这两个上下文共用一个环境,它是任何Spri

springboot配置多个yml文件

springboot配置多个yml文件 参考 springboot配置多个yml文件 maven(三)最详细的profile的使用 实战 为生产和开发分别配置一个profile.每个都有多个文件. profile 每个profile都应该有唯一的id, 可以同时激活多个profile,每个profile提供一些配置信息. <project> <profiles> <!-- profile的id应该唯一,但是当重复时,使用的是文件后面的配置.--> <profile