spring如何配置application.properties
时间: 2023-09-23 22:13:30 浏览: 50
1. 在Spring Boot项目中,在src/main/resources目录下创建一个名为application.properties的文件。
2. 在application.properties文件中添加配置项,例如:
```properties
# 数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 日志配置
logging.level.root=info
logging.file=myapp.log
# 端口配置
server.port=8080
```
3. 在Spring Boot应用程序中,可以通过@Value注解或@ConfigurationProperties注解来访问这些配置项,例如:
```java
@Value("${spring.datasource.url}")
private String dbUrl;
@ConfigurationProperties(prefix="logging")
public class LoggingProperties {
private String level;
private String file;
// getter and setter methods
}
```