返回 导航

SpringBoot / Cloud

hangge.com

SpringBoot - 配置文件application.properties使用详解(附:Profile多环境配置)

作者:hangge | 2019-08-29 08:10

1,开启自动转码功能

application.properties 提供了自定义属性的支持,如果数据有中文的话需要进行转码,否则可能会出现乱码问题。
如果我们使用的是 IntelliJ IDEA,那么直接在 setting 配置中进行如下设置,这样编辑器就会自动对中文内容进行转码。

2,配置属性的定义

(1)我们可以在 application.properties 中添加类似如下这样简单的常量配置:
my.name=航歌
my.age=100

(2)配置属性之间也可以相互引用使用: 
my.name=航歌
my.age=100
my.info=name:${my.name} age:${my.age}

(3)配置文件中可以使用 ${random} 来生成各种不同类型的随机值:
my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}

3,将数据注入到属性上

(1)在需要的地方我们使用 @Value 注解就可以将数据注入到属性上:
package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;

@RestController
public class HelloController {
    @Value("${my.name}")
    String name;

    @GetMapping("/hello")
    public String hello() {
        return "welcome to " + name;
    }
}

(2)运行结果如下:

4,将数据注入到 Bean 中

有时候属性太多了,一个个绑定到属性字段上太麻烦,官方提倡绑定一个对象的 bean

(1)首先我们创建一个名为 MyBean,并将前面的配置数据注入到这个 Bean 中。
(1)@ConfigurationProperties 中的 prefix 属性描述了要加载的配置文件的前缀。
(2)Spring Boot 采用了一种宽松的规则来进行属性绑定:
  • 假设 Bean 中的属性名为 authorName,那么配置文件中的属性可以是 my.author_namemy.author-namemy.authorName 或者 my.AUTHORNAME
package com.example.demo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "my")
public class My {
    private String name;
    private String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

(2)然后我们在 Controller 中引入这个 Bean 使用即可:
package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;

@RestController
public class HelloController {
    @Autowired
    My my;

    @GetMapping("/hello")
    public String hello() {
        return my.getName() + " : " + my.getAge();
    }
}
(3)运行结果如下:

5,使用自定义的配置文件

(1)有时候我们不希望把所有配置都放在 application.properties 里面,这时候我们可以另外定义一个。假设我们自定义的配置文件是 test.properties,放在 src/main/resources 下面。
(2)新建一个 bean 类后,通过如下方式将这个自定义的配置文件数据注入进来:
package com.example.demo;
 
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
@Component
@ConfigurationProperties(prefix = "my")
@PropertySource("classpath:test.properties")
public class My {
    private String name;
    private String age;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getAge() {
        return age;
    }
 
    public void setAge(String age) {
        this.age = age;
    }
}

6,使用命令行参数进行配置

(1)在命令行中通过 java -jar 命令启动项目时,可以使用连续的两个减号 --application.properties 中的属性值进行赋值。
(2)比如下面命令修改 tomcat 端口号为 8081。其等价于在 application.properties 中添加属性 server.port=8081
注意:如果 application.properties 中已经有同名属性,那么命令行属性会覆盖 application.properties 的属性。
java -jar xx.jar --server.port=8081

7,配置文件的优先级

(1)Spring Boot 项目中的 application.properties 配置文件一共可以出现在如下 4 个位置(优先级逐渐降低):
  • 项目根目录下的 config 文件夹
  • 项目根目录下
  • classpath 下的 config 文件夹
  • classpath

(2)如果这 4 个地方都有 application.properties 文件,加载的优先级就是从 1 4 依次降低,Spring Boot 将按照这个优先级查找配置信息。

8,加载外部的配置文件

(1)项目打包好以后,我们可以使用命令行参数的形式,启动项目的时候来指定外部配置文件的位置。
java -jar xxx.jar --spring.config.location=/Volumes/BOOTCAMP/application.properties

(2)当然我们也可以指定外部配置所在的文件夹,启动时会搜索并使用该文件夹下的配置文件:
java -jar xxx.jar --spring.config.location=/Volumes/BOOTCAMP/

(3)我们还可以同时配置多个路径,比如下面样例先加载外部配置文件,如果不存在外部配置文件的话则使用包内默认的配置文件:
java -jar xxx.jar --spring.config.location=/Volumes/BOOTCAMP/application.properties,classpath:/,classpath:/config/

附:使用 Profile 实现多环境配置

    我们在项目发布之前,一般需要频繁地在开发环境、测试环境以及生产环境之间进行切换,这个时候大量的配置需要频繁更改(比如数据库配置、redis 配置、mongodb 配置等等)。
    Spring Boot Profile 就给我们提供了解决方案,它约定不同环境下的配置文件名称规则为:
  • application-{profile}.properties,其中 {profile} 表示当前环境的名称。

1,创建配置文件 

(1)首先在 resources 目录下创建两个配置文件:application-dev.properties application-prod.properties,分别表示开发环境中的配置和生产环境中的配置。
(2)它们两个分别设置不同的端口号。
#application-dev.properties
server.port=8080

#application-prod.properties
server.port=80

2,在 application.properties 中配置环境

(1)假设我们在 application.properties 中进行如下配置,则表示使用 application-dev.properties 配置文件启动项目。
spring.profiles.active=dev

(2)如果将 dev 改为 prod,则表示使用 application-prod.properties 启动项目。
spring.profiles.active=prod

(3)项目启动成功后,就可以通过相应的端口进行访问了。

3,在代码中配置环境

(1)除了像前面那样在 application.properties 中添加配置,我们也可以在代码中添加配置来完成。
(2)比如我们在启动类的 main 方法上添加如下代码,表示使用 application-dev.properties 配置文件启动项目。
package com.example.demo;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplicationBuilder builder = new
                SpringApplicationBuilder(DemoApplication.class);
        builder.application().setAdditionalProfiles("dev");
        builder.run(args);
    }
}

4,在项目启动时配置环境

我们也可以在项目打包成 jar 包后启动时,在命令行中动态指定当前环境:
java -jar xxx.jar --spring.profiles.active=dev
评论

全部评论(0)

回到顶部