application.properties配置文件

  一、常用配置

修改访问的端口号和上下文路径

server:
  port: 9090
  servlet:
    context-path: /TEST
    encoding:
      force: true
      charset: UTF-8
      enabled: true
  tomcat:
    uri-encoding: UTF-8

springboot配置mvc视图解析器(默认是resources/templates/*.html),一般都不需要配置

spring:
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .html
      cache: false # 关闭缓存
    static-path-pattern: /**
  resources:
    static-locations: classpath:/templates/,classpath:/static/ # 开放成公开资源目录

其他一些spring常用配置

spring:
  datasource:
    druid:
      username: root
      password: 密码
      url: jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false
      driver-class-name: com.mysql.cj.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
  thymeleaf:
    cache: false
    mode: HTML
  mvc:
    favicon:
      enable: false
  servlet:
    multipart:
      max-file-size: 6GB  # 最大支持文件大小
      max-request-size: 6GB  # 最大支持请求大小
  #      enabled: true  #默认支持文件上传.
  #      file-size-threshold: #支持文件写入磁盘.
  #      location: #上传文件的临时目录

mybatis-plus配置

mybatis-plus:
  configuration:
    #    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    # 关闭驼峰到下划线的映射
    map-underscore-to-camel-case: false

二、application.yml多配置切换

核心配置文件:application.properties

开发环境用的配置文件:application-dev.yml

生产环境用的配置文件:application-pro.yml

这样就可以通过application.properties里的

spring.profiles.active=pro 
# 或者 spring.profiles.active=dev

灵活地来切换使用哪个环境,主要针对生产环境和开发环境的部署端口不一致等问题。

 多模块spring项目也可以用该功能。在启动模块配置关联其他子模块的配置文件,将其他子模块配置文件命名使用application-common.yml这种类型命名,然后在application.properties里配置

spring.profiles.active=common,dao,service

三、读取自定义application.yml配置

 首先,得保证你读取配置的类是spring组件,不行你加个 @Component 注解将其变成组件

yml里的配置

test:
  key1: fileName
  key2: C://test

读取类

@Value("${test.key1:fileName}")
public static String key1;

@Value("${test.key2:C://test}")
public static String key2;

其中,@Value("${test.key1:fileName}")代表默认值为fileName,有个兜底值。

如果是静态变量,那么就麻烦些,使用set方法赋值

    public static String key1;
    public static String key2;   
 
    @Value("${test.key1:fileName}")
    public void setKey1(String key1){
        this.key1= key1;
    }
    @Value("${test.key2:C://test}")
    public void setKey2(String key2){
        this.key2= key2;
    }

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值