springboot .yml配置所有内容总结

(1)端口服务配置

server:
  port: 8080   #端口号
  servlet:
    context-path: /main    #项目访问路径

(2)数据库配置

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: root
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  redis:
    database: 0
    host: localhost
    port: 6379
    password:
    jedis:
      pool:
        max-active: 8
        max-wait: -1
        max-idle: 8
        min-idle: 0
    timeout: 0

(3)配置多个不同的profile,实现在不同的环境(比如开发、测试和生产环境)使用不同的配置变量。

默认的profile为dev,其他环境通过指定启动参数使用不同的profile,比如:
测试环境:java -jar my-spring-boot.jar --spring.profiles.active=test
生产环境:java -jar my-spring-boot.jar --spring.profiles.active=prod


spring:
  profiles:
    active: dev


---
开发环境配置
spring:
  profiles: dev
mysql:
  ipPort: localhost:3306

---
测试环境配置
spring:
  profiles: test
mysql:
  ipPort: ip:port

---
生产环境配置
spring:
  profiles: prod
mysql:
  ipPort: ip:port
使用方法:
通过指定启动参数使用不同的profile
测试环境: java -jar my-spring-boot.jar --spring.profiles.active=test
生产环境: java -jar my-spring-boot.jar --spring.profiles.active=prod
在配置文件中指定 spring.profiles.active=dev
虚拟机参数
​-Dspring.profiles.active=dev

springboot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件

–file:./config/

–file:./

–classpath:/config/

–classpath:/
优先级由高到底,高优先级的配置会覆盖低优先级的配置

SpringBoot会从这四个位置全部加载主配置文件,互补配置;

项目打包好以后,我们可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置;指定配置文件和默认加载的这些配置文件共同起作用形成互补配置;

java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --spring.config.location=G:/application.properties

(4)指定静态资源路径

spring:
  resources:
    #指定静态资源路径,默认为classpath:[/META-INF/resources/,/resources/, /static/, /public/]以及context:/
    static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/}

(5)热部署

在Springboot+Thymeleaf的开发过程中,默认情况下修改到任何代码都需要重新启动项目才能生效。使用spring.thymeleaf.cache和devtools来解决html热启动的问题
首先在pom.xml中配置

<!--增加maven的devtools依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
注意:true只有设置为true时才会热启动,即当修改了html、css、js等这些静态资源后不用重启项目直接刷新即可。
然后修改application.yml

#热部署--静态资源立即生效
spring:
  #热部署--静态资源立即生效
  thymeleaf:
    cache: false                    #是否开启模板缓存,默认true
    encoding: UTF-8                 #指定模板的编码,默认为: UTF-8
    mode: HTML                      #指定模板的模式,具体查看StandardTemplateModeHandlers,默认为: HTML5
    prefix: classpath:/templates/   #前缀
    suffix: .html                   #后缀
    check-template-location: true  #是否检查模板路径是否存在,默认true
    servlet:
      content-type: text/html        #响应类型,指定Content-Type,默认为: text/html
  #热部署生效
  devtools:
    restart:
      enabled: true

(6)时间配置

spring:
    jackson:
        #指定日期格式,比如yyyy-MM-dd HH:mm:ss
        date-format: yyyy-MM-dd HH:mm:ss
        #指定日期格式化时区
        time-zone: GMT+8

(7)模板配置

springboot 中自带的页面渲染工具为thymeleaf 还有freemarker 这两种模板引擎 。

<!--freemarker-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
spring:
   freemarker:
    suffix: .html   #设定模板的后缀
    request-context-attribute: request  #request访问request
    content-type: text/html
    enabled: true
    cache: false   #缓存配置
    template-loader-path: classpath:/templates/ #模板加载路径 按需配置
    charset: UTF-8   #编码格式
    settings:
      number_format: '0.##'   #数字格式化,无小数点

(8)redis和shiro配置

<!--redis和shiro-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.3.2</version>
</dependency>
spring:
    redis:
        database: 0
        host: localhost
        port: 6379
        password:
        jedis:
          pool:
            max-active: 8
            max-wait: -1
            max-idle: 8
            min-idle: 0
        timeout: 0
    shiro:
        conf:
          domain:
          cookiePath: /
          successUrl: /index
          loginView: /login
          openToken: false
          sessionTimeout: 1800000
          algorithmName: md5
          hashIterations: 5
          #不拦截的路径
          sysanon:
            - /login
            - /regist
          #跨域配置
          allowedOrigins:
            - /

作者:花开半時偏妍
链接:https:https://www.jianshu.com/p/18c4e93b0220
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值