设为首页 加入收藏

TOP

Spring Boot 2 读取配置文件(一)
2019-09-24 11:16:30 】 浏览:59
Tags:Spring Boot 读取 配置 文件

开发环境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8

新建一个名称为demo的Spring Boot项目。

一、默认配置文件

Spring Boot会读取名称application.properties(yml)的配置文件。
如果有多个同名文件,默认情况下,按照下面顺序读取:
(1)项目根目录的config目录
(2)项目根目录
(3)项目classpath下的config目录
(4)项目classpath根目录
如果同一个配置项出现在多份配置文件中,后面读取的值不会覆盖前面的。

测试:
在项目的4个位置各建立application.properties,内容如下:
(1)config/application.properties

test = config/application.properties
test1 = test1

(2)application.properties

test = application.properties
test2 = test2

(3)src/main/resources/config/application.properties

test = src/main/resources/config/application.properties
test3 = test3

(4)src/main/resources/application.properties

test = src/main/resources/application.properties
test4 = test4

 修改默认生成的启动类 DemoApplication.cs 代码:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Autowired
    private Environment env;

    @RequestMapping("/")
    public String getProp(){
        String test = env.getProperty("test");
        String test1 = env.getProperty("test1");
        String test2 = env.getProperty("test2");
        String test3 = env.getProperty("test3");
        String test4 = env.getProperty("test4");
        return test + "," + test1 + "," + test2 + "," + test3 + "," + test4;
    }
}

访问 http://localhost:8080/
输出:config/application.properties,test1,test2,test3,test4

二、指定配置文件

读取指定的配置文件,不使用默认的application.properties。

测试:
(1)src/main/resources/application.properties 内容:

test1 = application.properties

(2)在项目的src/main/resources新建目录config,新建配置文件myConfig.properties,内容:

test2= myConfig.properties

修改默认生成的启动类 DemoApplication.cs 代码:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

    public static void main(String[] args) {
        //SpringApplication.run(DemoApplication.class, args);
        new SpringApplicationBuilder(DemoApplication.class).properties(
                "spring.config.location=classpath:/config/myConfig.properties"
        ).run(args);
    }

    @Autowired
    private Environment env;

    @RequestMapping("/")
    public String getProp(){
        String test1 = env.getProperty("test1");
        String test2 = env.getProperty("test
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇高强度学习训练第九天总结:5道剑.. 下一篇SpringApplication到底run了什么..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目