Spring Boot 配置教程

时间:2018-10-10
本文章向大家介绍Spring Boot 配置教程,需要的朋友可以参考一下

Profile 配置

Profile 是 Spring 用来针对不同的环境对不同的配置提供支持的,全局的 Profile 配置使用 application-{profile}.properties (如 application-prod.properties

通过在 application.properties 中设置 spring.profiles.active=prod 来指定活动的 Profile.

服务器常用配置

server.address # 服务器 ip 绑定地址,如果你的主机上有多个网卡,可以绑定一个 ip 地址
server.session.timeout #会话过期时间,以秒为单位
server.error.path # 服务器出错后的处理路径 /error
server.servlet.contextpath # springb boot 应用的上下文
server.port # spring boot 应用监听端口

Tomcat 相关配置

server.tomcat.accesslog.enabled=false # 打开tomcat访问日志
server.tomcat.accesslog.directory=logs # 访问日志所在的目录
server.tomcat.accept-count= # 允许http请求缓存到请求队列的最大个数,默认不限制
server.tomcat.max-connections= # 最大连接数,默认不限制,如果一旦连接数到达,剩下的连接将会保存到请求缓存队列里
server.tomcat.max-thread= # 最大工作线程数
server.tomcat.max-http-post-size= # http post 内容最大长度,默认不限制

日志配置

默认情况下,不需要对日志做任何配置就可以使用,Spring Boot 使用 LogBack 作为日志的实现:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory
...

public class HelloWorldController {
    private static final Logger log = LoggerFactory.getLogger(HelloWorldController.class);

    ....
}

日志级别有:ERROR、WARN、INFO、DEBUG和TRACE;

默认情况下,INFO级别以上的信息才会打印到控制台,可以自己设定日志输出级别

logging.level.root=info
# org 包下的日志级别
logging.level.org=warn
logging.level.com.yourcorp=debug

# Spring Boot 默认并未输出日志到文件,可以设置
logging.file=my.log
# 日志输出到my.log 中,位于Spring Boot 应用运行的当前目录,也可以指定日志存放的路径
logging.path=e:/temp/log

无论使用哪种方式记录日志文件,当日之达到10MB的时候会自动重新生成一个新日志文件。

配置浏览器显示 ico

Spring Boot 的 webapp 启动后,通过浏览器访问,浏览器会显示一个绿色的树叶图标。如果需要换成自己的图标,在项目 resources 目录下新建一个 static 目录,在 static 目录下创建 images 目录,然后项目的 favicon.ico 放在 images 目录下,每个页面添加以下样式即可

<link rel="shortcut icon" href="/images/apple.ico">

配置数据源

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Mybatis 配置

#mybatis
mybatis:
  config-locations: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
  type-aliases-package: net.dowhile.demo.entity

更多请参考Spring Boot Mybatis

读取应用配置

可以在应用中读取 application.properties 文件,Spring Boot 提供了三种方式,通用的 Eeviroment 类,可以通过 key-value 方式获取到 application.properties 中的值,也可以通过 @Value 注解,自动注入属性值,还可以将一组属性自动注入到一个配置类中。

1、 Environment

@Configuration
public class EnvConfig {

    @Autowired private Environment env;
    
    public int getServerPort() {
        return env.getProperty("server.port", Integer.class);
    }
}

2、 @Value

直接通过 @Value 注解注入一个配置信息到 Spring 管理的 Bean 中

    @GetMapping("/value")
    public String value(@Value("${server.port:8080}") int port) {
        return "port:" + port;
    }

@Value 注解支持 SpEL 表达式,如果属性不存在,可以提供一个默认值

3、@ConfigurationProperties

通常情况下,将一组同样类型的配置属性映射为一个类更为方便。

server.port=9090
server.context-path=/config

以上两个配置属性都与 web 服务器配置相关,都有 server 前缀,因此可以使用注解 `` 来获取这一组实现。

@ConfigurationProperties("server")
@Configuration
class ServerConfig {
    private int port;
    private String contextPath;
    public int getPort() {
        return port;
    }
    public void setPort(int port) {
        this.port = port;
    }
    public String getContextPath() {
        return contextPath;
    }
    public void setContextPath(String contextPath) {
        this.contextPath = contextPath;
    }
}

可以使用 @Autowired 直接注入该配置类,还可以指定 properties 文件位置。

@Autowired
private ServerConfig serverConfig;

@ConfigurationProperties(prefix = "server", locations = {"classpath:config/author.properties"});