Spring Boot学习(三)——环境配置

          Spring Boot的基本配置文件为 application.properties。Spring Boot项目下的配置信息基本上都可以从这个配置文件上读取。以下分点说明可以配置的信息:

          1 服务器配置
          server.port = 8080;
         当然Spring Boot同时也支持命令启动端口 : java-jar test.jar --server.port=8080
          亦或者是 传入虚拟机系统属性:  java -Dserver.port=8080 -jar test.jar
          常用的服务器配置还有  
         
属性     描述
server.address 服务器IP绑定地址
server.session.timeout 回话过期时间,以秒为单位
server.error.path 服务器出错后的处理路径 /error
server.servlet.contextpath Spring Boot应用的上下文
server.port Spring Boot监听的端口


          2 配置web 服务器
          Spring Boot 内置了Tomcat, 同时也致辞Jetty、Undertow作为web服务器。使用这些应用服务器时,只需要映入应用的starter即可
          Undertow
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-undertow</artifactId>
          </dependency>

          Jetty
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-jetty</artifactId>
          </dependency>
          
          同时,还需要再Spring-boot-starter-web中取出Tomcat依赖:
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
              <exclusions>
                   <exclusion>     
                       <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-tomcat</artifactId>
                   </exclusion>
              </exclusions>
          </dependency>

          application.properties中 server.tomcat.*  包含了Tomcat的相关配置,如下
属性      描述
server.tomcat.accesslog.enable 打开Tomcat访问日志,默认false
server.tomcat.accesslog.directory 访问日志所在目录,默认logs
server.tomcat.accept-count 允许Http请求缓存到请求队列的最大个数,默认不限制
server.tomcat.max-connections 最大连接数、默认不限制,如果一旦达到连接数,剩下的连接将会保存到请求缓存队列中,也就是accept-count队列
server.tomcat.max-threads
最大工作线程数
server.tomcat.max-http-post-size HTTP POST 内容最大长度,默认不限制
          


          3 配置启动信息
          Sping Boot在启动时会在控制台中默认打印 “Spring”
          可以在 classpath中增加banner.txt显示自己的输出信息,在Spring Boot项目的resources目录中创建一个banner.txt文件
          在application.properties中可以配置输出内容的属性:
        
banner.charset 字符编码 例:UTF-8
banner.location 路址 例:classpath:banner.txt
banner.image.location 可以使用图片 例:classpath:banner.gif
banner.image.width 图片宽度
banner.image.heigth 图片长度
banner.image.margin 图片左边边距
        


          4 日志配置
          默认情况下,不需要对日志做任何配置也可以使用, Sping Boot使用的是LogBack作为日志的实现,使用apache Commons Logging作为日志接口,因此代码中通常是这样的:
          public class Test {
              private Log log = LogFactory.getLog(Test.class);
              ...
          }
         
          application.properties配置文件中常用配置如下:
          logging.level.root=info
          #指定包下的日志级别
          logging.level.org=warn
          logging.level.com.xhwl=debug

          logging.file = my.log #输出日志文件,默认路径在Spring Boot当前目录
          logging.path=e:/temp/log  #日志路径,会在e盘的 /temp/log文件夹下生产spring.log日志文件

          logging.pattern.console=%level %date{HH:mm:ss} %logger{20}.%M %L :%m%n
          logging.pattern.file= %level %date{ISO8601} [%thread] %logger{20}.%M %L: %m%n
          #其中   %level为日志级别  %date 标识日志发生时间格式  %logger用于输出Logger的名字{*}限制长度  %thread表示当前线程名 %M日志发生时的方法名字   %L 错误代码行 %m日志消息  %n日志换行

          我们可以在启动时 java -jar test.jar -debug调整日志级别















猜你喜欢

转载自blog.csdn.net/q410654146/article/details/80674657