Home > Java > javaTutorial > Summary of configuration methods of related files in Spring Boot

Summary of configuration methods of related files in Spring Boot

不言
Release: 2018-10-10 11:35:03
forward
2930 people have browsed it

This article brings you a summary of the configuration methods of related files in Spring Boot. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Profile configuration

Profile is used by Spring to provide support for different configurations for different environments. The global Profile configuration uses application-{profile}.properties (such as application- prod.properties)

Specify the active Profile by setting spring.profiles.active=prod in application.properties.

Common server configuration

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

Tomcat related configuration

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 内容最大长度,默认不限制
Copy after login

Log configuration

By default, the log can be used without any configuration. Spring Boot uses LogBack as a log implementation:

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

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

    ....
}
Copy after login

The log levels include: ERROR, WARN, INFO, DEBUG and TRACE;

By default, only information above the INFO level will be printed to the console. You can Set the log output level yourself

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
Copy after login

No matter which method is used to record the log file, a new log file will be automatically regenerated when it reaches 10MB that day.

Configure the browser to display ico

After the Spring Boot webapp is started and accessed through the browser, the browser will display a green leaf icon. If you need to change to your own icon, create a new static directory in the project resources directory, create the images directory under the static directory, and then create the project's favicon.ico Place it in the images directory and add the following styles to each page

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

Configuration data source

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
Copy after login

Mybatis configuration

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

For more information, please refer to Spring Boot Mybatis

Read application configuration

Can be read in the applicationapplication.properties file, Spring Boot provides three methods. The general Eeviroment class can obtain the value in application.properties through the key-value method. You can also automatically inject attribute values ​​through the @Value annotation, and you can also automatically inject a set of attributes into a configuration class.

1, Environment

@Configuration
public class EnvConfig {

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

2, @Value

Directly inject a configuration information into the Bean managed by Spring through the @Value annotation

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

@Value annotation supports SpEL expressions. If the property does not exist, a default value can be provided

3, @ConfigurationProperties

Normally, a group of configuration properties of the same type It is more convenient to map to a class.

server.port=9090
server.context-path=/config
Copy after login

The above two configuration properties are related to the web server configuration and both have the server prefix, so you can use the annotation `` to obtain this set of implementations.

@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;
    }
}
Copy after login

You can use @Autowired to directly inject the configuration class, and you can also specify the properties file location.

@Autowired
private ServerConfig serverConfig;

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

The above is the detailed content of Summary of configuration methods of related files in Spring Boot. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template