Spring Boot项目开发部署过程中,通常会有多套环境(开发dev、测试test、预生产rc,生产pro),每套环境的配置是不同的。将所有环境共同的参数配置在同一个文件中;再将每套环境不同的参数配置在各自文件中,可以减少部署错误的概率,同时项目可读性好,也便于维护。
application.yml 配置共同的参数,并确定当前运行环境
debug: false mybatis: mapper-locations: classpath:mapping/*.xml type-aliases-package: com.fhbean.springboot.mybatisdemo.model #pagehelper分页插件 pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql spring: profiles: active: dev
这里的spring.profiles.active=dev即表示,当前是dev环境;application-{profile}.yml就对应application-dev.yml,{profile}的取值可以是dev, test, rc, pro
application-dev.yml
server: port: 8080 spring: profiles: dev datasource: name: test url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true username: root password: root # 使用druid数据源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20