spring boot 环境设置

spring boot 项目环境有很多, 不可能每次切换环境都改代码, 如下是springboot配置环境的配置

一个总的配置文件 application.yml. 三个分环境配置文件。

总的配置文件配置不变的信息

server:
  port: 8088
  servlet:
    context-path: /

database: mysql

spring:
  profiles:
    active: prod
  freemarker:
    allow-request-override: false
    cache: false
    check-template-location: false
    charset: UTF-8
    content-type: text/html; charset=utf-8
    expose-request-attributes: false
    expose-session-attributes: false
    expose-spring-macro-helpers: false
    suffix: .html
    template-loader-path: classpath:/templates
    request-context-attribute: request

mybatis:
    configuration:
        mapUnderscoreToCamelCase: true
        logImpl: org.apache.ibatis.logging.stdout.StdOutImpl
        typeAliasesPackage: com.sqc.mock.tfci.mapper,;com.sqc.fuzzy.mapper
    mapperLocations: classpath:mapper/**/*Mapper.xml
    typeAliasesPackage: com.sqc.mock.tfci.entity,;com.sqc.fuzzy.entity

spring.devtools.restart.enabled: true
    
main:
    allow-bean-definition-overriding: true

切换环境需要修改profiles

spring boot 环境设置_第1张图片

不同环境的配置文件如下

如application_dev.xml

Spring:
  datasource:
    username: ***********
    password: **********
    url: jdbc:mysql://*******************?characterEncoding=utf-8&useTimezone=true&serverTimezone=GMT%2B8
    type: com.alibaba.druid.pool.DruidDataSource
    initialSize: 5  # 启动时初始化连接数
    minIdle: 5      # 最小空闲连接数
    maxActive: 20   # 最大连接数
    maxWait: 60000  # 获取连接等待时间60s 超出报错
    timeBetweenEvictionRunsMillis: 60000    # 每60s执行一次连接回收器
    minEvictableIdleTimeMillis: 300000      # 5分钟内没有任何操作的空闲连接会被回收
    validationQuery: SELECT 1 FROM DUAL     # 验证连接有效性的sql
    testWhileIdle: true   # 空闲时校验,建议开启
    testOnBorrow: false   # 使用中是否校验有效性,建议关闭
    testOnReturn: false   # 归还连接时校验有效性,建议关闭
    poolPreparedStatements: false   # 是否缓存 PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。
    filters: stat,wall,logback      # 设置过滤器 stat用于接收状态,wall用于防止生sql注入,使用logback输出日志
    useGlobalDataSourceStat: true   # 统计所有数据源状态
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500   # sql合并统计,与设置慢sql时间为500ms

maven打包时也需要注意。 添加profiles


		
		
			dev
			
				dev
			
			
				true
			
		
		
		
			test
			
				test
			
		
		
		
			prod
			
				prod
			
		
	

默认是开发环境 ,比如mvn打包上线需要制定profile  prod . 测试环境使用test

mvn clean package -P prod

 

你可能感兴趣的:(Spring)