springboot学习笔记系列(四)-2018年8月30日 13:41:53

springboot学习笔记系列(四)-2018年8月30日 13:41:53

今天先整理一下配置文件的事吧,看着application.properties文件越来越杂,把各框架的配置信息分开,然后把自己配置的信息拿出来.(习惯使用application.properties,不喜欢使用application.yml),所以不整理yml文件了.

现在的application.properties文件配置如下:

##服务器一些基本配置
server.port=8080
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss


##freemarker的配置(暂时不用先关闭,只是添加上依赖)
spring.freemarker.enabled=false
spring.freemarker.cache=false
spring.freemarker.charset=utf-8
spring.freemarker.checkTemplateLocation=false

##thymeleaf的配置
spring.thymeleaf.enabled=true
spring.thymeleaf.cache=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.check-template=true
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.check-template-location=true

##数据源配置
spring.datasource.driver-class-name = com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url = jdbc:sqlserver://localhost:1433;databaseName=Sammery
spring.datasource.username=sa
spring.datasource.password=123456

##durid连接池的配置
spring.datasource.name = sqlserver
spring.datasource.type = com.alibaba.druid.pool.DruidDataSource

#连接池的扩展数据
spring.datasource.druid.db-type=sqlserver
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=60000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000

spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.validation-query=SELECT 1

spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
spring.datasource.druid.filters=stat,wall,slf4j
spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
spring.datasource.druid.use-global-data-source-stat=true

spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*
spring.datasource.druid.web-stat-filter.url-pattern=/druid/*
spring.datasource.druid.web-stat-filter.profile-enable=true
spring.datasource.druid.web-stat-filter.session-stat-enable=true
spring.datasource.druid.web-stat-filter.session-stat-max-count=10

spring.datasource.druid.filter.stat.db-type=sqlserver
spring.datasource.druid.filter.stat.merge-sql=true
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=1000
spring.datasource.druid.filter.wall.enabled=true
spring.datasource.druid.filter.wall.db-type=sqlserver

spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
spring.datasource.druid.stat-view-servlet.reset-enable=false


mybatis.check-config-location=true
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
mybatis.type-aliases-package=com.sammery.tang.domain
#配置项:开启下划线到驼峰的自动转换. 作用:将数据库字段根据驼峰规则自动注入到对象属性。
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.cache-enabled=true
mybatis.configuration.lazy-loading-enabled=true
#是否允许单一语句返回多结果集(需要兼容驱动)。
mybatis.configuration.multiple-result-sets-enabled=true

看到这里是不是感觉太多了?各种配置信息都在一个properties文件中,后期维护起来会很麻烦.

接下来就说一下如何分出来多个properties文件,需要某个模块就启用某个模块的配置文件,另外还可以配置多环境的配置文件,比如开发 生产和测试环境等.

我们首先把公共文件或者自己想直接配置在公共文件中的信息整理到application.properties中

application.properties

#要启用的配置文件
spring.profiles.active=servertest,freemarker,thymeleaf,datasource,druid,mybatis
com.sammery.tang.title=沙漠渔溏
com.sammery.tang.startTime=2018年8月30日 14:09:20
com.sammery.tang.description=学习
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

application-servertest.propertis

##服务器一些基本配置
server.port=8080

application-serverpro.propertis

##服务器一些基本配置
server.port=8000

application-freemarker.propertis

##freemarker的配置(暂时不用先关闭,只是添加上依赖)
spring.freemarker.enabled=false
spring.freemarker.cache=false
spring.freemarker.charset=utf-8
spring.freemarker.checkTemplateLocation=false

application-thymeleaf.propertis

##thymeleaf的配置
spring.thymeleaf.enabled=true
spring.thymeleaf.cache=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.check-template=true
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.check-template-location=true

application-datasource.propertis

##数据源配置
spring.datasource.driver-class-name = com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url = jdbc:sqlserver://localhost:1433;databaseName=Sammery
spring.datasource.username=sa
spring.datasource.password=123456
spring.datasource.name = sqlserver
spring.datasource.type = com.alibaba.druid.pool.DruidDataSource

application-druid.propertis

#druid连接池的扩展数据
spring.datasource.druid.db-type=sqlserver
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=60000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000

spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.validation-query=SELECT 1

spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
spring.datasource.druid.filters=stat,wall,slf4j
spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
spring.datasource.druid.use-global-data-source-stat=true

spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*
spring.datasource.druid.web-stat-filter.url-pattern=/druid/*
spring.datasource.druid.web-stat-filter.profile-enable=true
spring.datasource.druid.web-stat-filter.session-stat-enable=true
spring.datasource.druid.web-stat-filter.session-stat-max-count=10

spring.datasource.druid.filter.stat.db-type=sqlserver
spring.datasource.druid.filter.stat.merge-sql=true
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=1000
spring.datasource.druid.filter.wall.enabled=true
spring.datasource.druid.filter.wall.db-type=sqlserver

spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
spring.datasource.druid.stat-view-servlet.reset-enable=false

application-mybatis.propertis

mybatis.check-config-location=true
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
mybatis.type-aliases-package=com.sammery.tang.domain
#配置项:开启下划线到驼峰的自动转换. 作用:将数据库字段根据驼峰规则自动注入到对象属性。
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.cache-enabled=true
mybatis.configuration.lazy-loading-enabled=true
#是否允许单一语句返回多结果集(需要兼容驱动)。
mybatis.configuration.multiple-result-sets-enabled=true

现在都分开了,公共部分就放在application中,其他的配置信息放到相应的配置文件中就可以了,这样启用哪个只需要在公共部分启用或者关闭就可以了,然后maven设置好依赖信息就可以了,避免有些自动检测不过导致的程序无法运行.

ok,到这里就改造完成了,后期需要添加什么配置信息在相应的配置模块内添加即可,并且可以快速切换数据库等信息,是不是很方便了?

猜你喜欢

转载自blog.csdn.net/forose/article/details/82226482