spring boot 配置文件

Posted zhenghuasheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot 配置文件相关的知识,希望对你有一定的参考价值。

Spring Boot的默认属性配置文件是在resources下的application.properties,也可以使用application.yaml来配置默认参数。如果是使用Spring Boot自带的配置的话,配置即使用,无需再单独去声明内容。

例如:

###spring datasource###
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/sms?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull
spring.datasource.username=zhenghuasheng
spring.datasource.password=zhs2111014
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-active=10
spring.datasource.max-wait=10000
spring.datasource.max-idle=5
spring.datasource.min-idle=0
###mybatis config###
mybatis.mapper-locations=classpath:/mybatis/*Mapper.xml
mybatis.type-aliases-package=com.sample.model

Spring Boot推荐使用类型安全的方式来进行自定义配置,例如:

@Component
@ConfigurationProperties( //1
        prefix = "usetest",
        locations = "classpath:config/app.properties"
)
public class TestInfoSettings 
    private String name;
    private String age;
    //省略get、set

说明:
1:@ConfigurationProperties加载properties文件内的配置,prefix属性指定配置的前缀,例如:usetest.*=string 。locations可以指定自定义的文件位置,不指定默认使用application.properties。

以上是关于spring boot 配置文件的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot学习——Spring Boot配置文件application

初识Spring Boot (Spring Boot配置文件)

Spring —— Spring Boot 配置文件

spring-boot学习五:Spring boot配置文件的加载位置

Spring Boot:Spring Boot项目配置详解

Spring Boot系列Spring Boot初级应用及配置文件