springBoot配置文件(二)application常见配置

一、命令行参数:

1、server.address=xxx.xxx.xx.xxx  

    服务器绑定ip地址,多网卡时可以指定

2、server.port=xxx 

 可以指定springboot内嵌容器启动的端口,默认使用tomcat容器时在8080端口,右键run- java application/springboot..,可以支持不同的容器,在引入不同的依赖时。当server.port=0时,表示自动扫面获取一个可用的端口。

3、ssl的安全访问配置:

server.port=8443
#ssl的安全访问配置
server.ssl.key-store=classpath:keystore.jks
server.ssl.key-store-password=secret
server.ssl.key-password=another-secret

注意: 目前spring-boot不支持http和https同时启用的情况,只支持使用其中一个,如果需要同时使用,可以使用其他形式的实现方式。

该部分对应org.springframework.boot.autoconfigure.webServerProperties类。

此外还有一些不是很常用的如:server.http2.enable=true/false//该属性可以支持http2的协议类型,目前只支持tomcat和undertow的容器并且需要JDK1.8+,官文上对于内嵌tomcat的配置参数也有很多。

二、开发/测试/生产环境配置:

1、语法:

spring.profiles.active=xxxx

  //该系统变量可以指明要使用的配置文件,一般应用于多环境配置分离,如生产环境(production),开发环境(development),测试环境(test)等,可以自定义,如开发环境配置文件为application-dev.properties,则spring.profiles.active=dev,在启动时会加载application-dev.properties配置文件。

2、使用方法:

(1)手动指定:这种方法切换环境需要修改配置文件,不够方便

spring.profiles.active = {profile}
#如spring.profiles.active = prod​

 (2)打包自动指定。

spring.profiles.active=@spring.profiles.active@

3、demo:

启动类:

package com;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class ProfilesApplication {
 
    public static void main(String args[]){
        SpringApplication.run(ProfilesApplication.class,args);
    }
}

多环境配置文件:

开发环境application-dev.properties:

zt.profiles = this is dev

 测试环境application-test.properties

zt.profiles = this is test

生产环境application-prod.properties 

zt.profiles = this is prod

接口:

package com.controller;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class TestController {
 
    @Value("${zt.common}")
    private String common;
 
    @Value("${zt.profiles}")
    private String profilesStr;
 
    @RequestMapping("/test")
    public String getStr(){
        return "公共:"+common+";环境:"+profilesStr;
    }
}

下面分别列下手动、自动方法的区别写法:

(1)手动方法:

 pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.profiles</groupId>
    <artifactId>profiles-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

 默认配置文件:

server.port= 8888
server.context-path=/profiles
spring.profiles.active=prod
 
zt.common = conmmon

mvn clean package打包测试一下: 

   

这时候我们打包带参数,如mvn clean package -Ptest、mvn clean package -Pdev、mvn clean package -Pprod,打包后的仍然是prod,就是说必须手动修改application.properties中的环境。

(2)自动方法:

在pom中添加以下配置:

<profiles>
        <!-- 测试环境 -->
        <profile>
            <id>test</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>test</spring.profiles.active>
            </properties>
        </profile>
        <!-- 开发环境 -->
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
        </profile>
        <!-- 生产环境 -->
        <profile>
            <id>prod</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>prod</spring.profiles.active>
            </properties>
        </profile>
    </profiles>

 并把application.properties中的profiles指定改为

spring.profiles.active=@spring.profiles.active@

这时候打包使用mvn clean package -Ptest、mvn clean package -Pdev、mvn clean package -Pprod,打出来的就是指定环境的包了。pom文件中test一项默认是true,所以如果不指定环境直接使用mvn clean package打包就是test环境。
idea启动则可以点击右侧的maven,选择环境勾上,再启动服务即可:

启动服务访问的为prod环境:

再去掉prod,勾上test:

重启服务访问的为test:

三、关于Banner

项目启动时会打印一大堆符号组成的东西,看了好久感觉好像是打印的Spring这几个字母。

SpringBoot里叫banner配置。

banner.location=xxx.txt //可以自定义输出信息的位置

banner.charset=utf-8 //指定编码格式

spring.main.banner-mode=console/off    //banner图开启或者打印模式

四、cache配置(引入spring-boot-starter-cache自动集成)

对于缓存自动集成,spring-boot也提供了很多方案,这里说一下默认的spring-cache、ehcache、redis-cache的相关属性配置

spring.cache.type=cache/ehcache/redis....(参照CacheType类) //用于指明要使用的缓存类型

ehcache专用:

spring.cache.ehcache.config=classpath:cache/ehcache.xml //用于配置使用ehcache时配置文件所在位置

在使用默认的cache时(默认cache是采用concurrentMap实现的缓存,前提是没有引入spring-boot-starter-data-redis等其他缓存依赖)或者使用redis时,可以使用下面参数配置

spring.cache.cache-names=xxx,xxx,xxx   //配置缓存名称

redis专用:

spring.cache.redis.time-to-live=600000    //缓存有效时间,单位毫秒

spring.cache.*对应类为:org.springframework.boot.autoconfigure.cache.cacheProperties.java

完成上面的设置就可以在项目中使用spring注解,但是要使用@EnableCaching启用注解,具体cache使用详见下面的章节。

五、文件上传配置

#默认true

#spring.http.mutipart.enabled=true

#上传中转文件位置,
#spring.http.multipart.location=
#最大上传文件大小,默认1MB
spring.http.multipart.max-file-size=5MB
#最大请求大小,默认10MB
spring.http.multipart.max-request-size=20MB

对应类:org.springframework.boot.autoconfigure.web.MultipartProperties
 

(注意:在实际类中出现驼峰表示的变量,配置属性单词间用-分割)

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值