Spring Boot2 series of tutorials (e) | yaml Detailed profiles

yaml

Custom attributes load

First constructed SpringBoot project, will not see this old article using the IDEA project to build Spring Boot .

First, add the following custom properties in the project root directory src >> resource >> application.properties file:

# 防止读取乱码
spring.http.encoding.charset=UTF-8
# 项目启动端口
server.port=9999
# 自定义配置
com.nasus.author.name=一个优秀的废人
com.nasus.article.title=SpringBoot配置文件详解

Use @value notes read configuration file attributes:

package com.nasus.bean;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Project Name:springboot_properties_demo <br/>
 * Package Name:com.nasus.properties <br/>
 * Date:2019/1/28 20:59 <br/>
 * <b>Description:</b> TODO: 描述该类的作用 <br/>
 * @author <a href="[email protected]">nasus</a><br/>
 */

@Data
@Component
public class PropertiesBean {

    @Value("${com.nasus.author.name}")
    private String name;

    @Value("${com.nasus.article.title}")
    private String title;

    @Value("${com.nasus.doing}")
    private String desc;

}

After the new custom property loading test controller, as follows:

package com.nasus.controller;

import com.nasus.bean.PropertiesBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Project Name:springboot_properties_demo <br/>
 * Package Name:com.nasus.controller <br/>
 * Date:2019/1/28 21:41 <br/>
 * <b>Description:</b> TODO:  测试自定义属性加载<br/>
 *
 * @author <a href="[email protected]">nasus</a><br/>
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private PropertiesBean propertiesBean;

    @GetMapping("/getInfo")
    public PropertiesBean getInfo(){
        return propertiesBean;
    }
}

Visit http: // localhost: 8080 / test / getInfo view loading results:

Test Results

Can be seen, after adding @value annotation, attribute profiles are read out. Previously, perhaps we also need specialized tools to write a class to read the profile of the attributes read out, and now with Spring, we can directly use @value will be able to read, simply can not be too easy. In the present embodiment this source: GitHub Address

References between parameters

Profile code is as follows:

# 防止读取乱码
spring.http.encoding.charset=UTF-8
# 项目启动端口
server.port=9999
# 自定义配置
com.nasus.author.name=一个优秀的废人
com.nasus.article.title=SpringBoot配置文件详解

com.nasus.doing=${com.nasus.author.name}写文章《${com.nasus.article.title}》

The last parameter configuration can be seen that the use of the first two configuration parameters, test results are as follows:
Test Results

Using a random number

Sometimes project requirements, we may need to configure some random numbers for, say, safety and random configuration server port, and login keys. Then we can use random attributes SpringBoot to configure random number, for example:

# 随机字符串
com.nasus.article.value=${random.value}
# 随机int
com.nasus.article.number=${random.int}
# 随机long
com.nasus.article.bignumber=${random.long}
# 10以内的随机数
com.nasus.article.test1=${random.int(10)}
# 10-20的随机数
com.nasus.article.test2=${random.int[10,20]}

Use multiple configuration files

Many times we all need a lot of development projects set the environment, such as a test environment, development environment and production environment. Different environments need to use a different configuration file, for which we are following three new configuration files based on these three environments.

application-dev.properties: development environment
application-test.properties: Test environment
application-prod.properties: the production environment

项目中默认的配置文件是 application.properties 。这时我们可以根据自己的环境去使用相应的配置文件,比如说,项目各个环境的端口必须不一样。那我们可以这样配置:
application-dev.properties:开发环境

server.port=6666

application-test.properties:测试环境

server.port=7777

application-prod.properties:生产环境

server.port=8888

假如,现在我打包上线,那就必须用生产环境的配置文件了,这时我们可以在 默认的配置文件 application.properties 中加入以下配置即可

spring.profiles.active=prod

配置数据库

SpringBoot 的配置文件有两种格式,一种是 .properties 格式(以上栗子都是用的这种)还有一种用的是 .yaml 格式。以下是用 yaml 方式配置。这两种格式并无好坏之分,纯看个人使用习惯。我就比较喜欢 yaml 格式,因为看起来比较简洁。

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=true
    username: 你的数据库名称
    password: 你的数据库密码

  jpa:
    hibernate:
      ddl-auto: update   #ddl-auto:设为update 表示每次都重新建表
    show-sql: true

注意事项

  1. 使用 yaml 格式需要注意一点就是 键值对冒号后面,必须空一格
  2. application.properties 配置中文值的时候,读取出来的属性值会出现乱码问题。但是 application.yml 不会出现乱码问题。原因是,Spring Boot 是以 iso-8859 的编码方式读取 application.properties 配置文件。
    解决第二点,只需加入 spring.http.encoding.charset=UTF-8 配置即可。

后语

That is all I SpringBoot configuration file to understand and use, of course, a bit more than just introduce a few SpringBoot usage profile, there are a lot of its use, want or need you a lot of depth using depth practice. Finally, Python, Java are interested please press two-dimensional code wave of attention, I will try to bring you value, even if you think this paper has a little bit of help, please help spot good-looking, so that more people know.

If you see here, that you liked this article, please forward, thumbs up. Micro-channel search " a good basket case ", after concerns reply " 1024 " will give you a complete set of java tutorial.

Tutorial excerpt

Guess you like

Origin www.cnblogs.com/nasus/p/12149653.html