目录
配置文件
SpringBoot
在 resources
目录下,有一个默认的全局配置文件,名称为 application.properties
或者 application.yml
。
SpringBoot
支持两种格式配置文件,properties
及yml
。
配置文件优先级
SpringBoot
自动从多个目录进行配置文件加载,加载优先级从高到底:
file:./config/
file:./
classpath:/config/
classpath:/
SpringBoot
启动时会自动扫描以下目录的 application.properties
及 application.yml
文件,如果存在多个配置文件则遵循以下规则:
- 多个配置文件使用并集
- 配置存在冲突时,高优先级覆盖低优先级
常用配置
设置端口
配置项:server.port
,默认值:8080
application.properties
server.port = 8080
application.yml
server:
port: 8080
设置 context-path
配置项:server.servlet.context-path
,默认值:``
application.properties
server.servlet.context-path = /example
application.yml
server:
servlet:
context-path: /example
读取配置
编写配置
example.username = mm
example.age = 20
通过 @Value
注解读取单个配置
在 Spring 的 Bean 中使用 @Value
注解进行注入
@Value("${example.username}")
private String username;
如果使用 @Value
注入的配置不存在,则程序在启动时会报错
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'example.username' in value "${example.username}"
可以通过添加默认值,规避这个错误
@Value("${example.username:hello}")
private String username;
通过 @ConfigurationProperties
注解批量读取配置
1、创建一个实体类,在类上使用 @ConfigurationProperties
注解,prefix
表示需要读取配置的前缀,会把以 prefix
前缀的所有配置,与类中的属性名进行匹配加载。
@Data
@ConfigurationProperties(prefix = "example")
public class UserProperties {
private String username;
private Integer age;
}
2、启动加载配置
有两种方法可以在启动时进行配置加载。
- 在启动类上添加
@EnableConfigurationProperties(UserProperties.class)
注解,这样启动时,会自动对UserProperties
类进行配置加载。 - 在启动类上添加
@ConfigurationPropertiesScan
注解,这样启动时,会自动扫描当前包及子包下的所有@ConfigurationProperties
标签,进行配置加载。
3、依赖注入
@ConfigurationPropertiesScan
@RestController
@SpringBootApplication
public class Application {
@Autowired
private UserProperties properties;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@GetMapping("/showUser")
public UserProperties showUser() {
return properties;
}
}
通过 @ConfigurationProperties
注入的配置如果不存在,启动不会报错,而是通过类加载机制自动赋了一个默认值。
实际开发中建议使用
@ConfigurationProperties
来读取自定义属性。
配置自动提示
通过 @ConfigurationProperties
在应用中添加一个自定义配置类,在集成开发环境中对 application.properties
编辑时,不会自动提示这个自定义配置,但是 SpringBoot
自带的属性则会自动提示。
如何在编辑 application.properties
时也会自动提示自定义配置?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
自定义配置文件
除了在默认配置文件 application.properties
中进行配置外,SpringBoot
也可以自定义配置文件
加载自定义 XML 文件
1、编写配置文件 mybatis-config.xml
2、在启动类上添加 @ImportResource("classpath:mybatis-config.xml")
注解
@SpringBootApplication
@ImportResource("classpath:app-config.xml")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
加载自定义 Properties 文件
1、编写配置文件 jdbc.properties
2、在启动类上添加 @PropertySource("classpath:jdbc.properties")
注解
@SpringBootApplication
@PropertySource("classpath:jdbc.properties")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
命令行参数
1、使用 --server.port=8080
java -jar --server.servlet.context-path=/example --server.port=8888 my-app.jar
2、使用 -Dserver.port=8080
java -jar -Dserver.servlet.context-path=/example -Dserver.port=8888 my-app.jar