java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot 参数配置

SpringBoot 的参数配置示例全解析

作者:程序员小假

Spring Boot的参数配置系统通过application.properties和application.yml文件实现,支持多种外部配置方式,本文介绍SpringBoot 的参数配置示例全解析,感兴趣的朋友跟随小编一起看看吧

当然了解,Spring Boot 的参数配置是其核心特性之一,也是它实现“约定大于配置”理念的关键。它极大地简化了传统 Spring 应用中繁琐的 XML 配置。

一、核心概念:application.properties与application.yml

Spring Boot 默认使用这两种文件进行配置(二者选其一即可,.yml 更常用)。

application.properties (传统键值对格式)

server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secret
logging.level.com.example.demo=debug

application.yml (YAML 格式,层次感更强,推荐使用)

server:
  port: 8081
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: secret
logging:
  level:
    com.example.demo: debug

YAML 注意事项:缩进必须使用空格,不能使用 Tab 键,冒号后面必须有一个空格。

二、配置的加载位置与优先级

Spring Boot 会从以下位置按从高到低的优先级加载 application 配置文件(高优先级的配置会覆盖低优先级的配置):

  1. 当前项目根目录下的 /config 子目录
  2. 当前项目根目录
  3. classpath 下的 /config 包 (即 src/main/resources/config)
  4. classpath 根路径 (即 src/main/resources)

最佳实践:在开发时,将通用配置放在 src/main/resources/application.yml 中。在打包部署时,可以在 JAR 包所在目录创建一个 config 文件夹,里面放一个 application.yml 来覆盖开发环境的配置(如数据库连接),这样就实现了配置与代码分离

三、外部化配置(非常强大)

除了配置文件,Spring Boot 还支持多种外部配置方式,优先级高于 application.yml。这在容器化部署(如 Docker)时尤其有用。

  1. 命令行参数
java -jar yourapp.jar --server.port=8888 --spring.datasource.url=jdbc:mysql://prod-server:3306/proddb

application.yml

spring:
  profiles:
    active: dev # 默认激活开发环境

激活方式

四、如何在代码中获取配置值?

@Value 注解 (适用于单个属性)

@Component
public class MyComponent {
    @Value("${server.port}")
    private int serverPort;
    @Value("${app.message: Hello Default}") // 使用冒号指定默认值
    private String message;
    // ... 
}

@ConfigurationProperties 注解 (推荐,用于绑定一组配置)

这是更类型安全、更面向对象的方式。

步骤 1:在 application.yml 中定义配置

app:
  user:
    name: "Alice"
    age: 30
    email: "alice@example.com"
    hobbies:
      - reading
      - hiking

步骤 2:创建一个配置类来绑定这些属性

@Component
@ConfigurationProperties(prefix = "app.user") // 前缀是 app.user
@Data // Lombok 注解,自动生成 getter/setter
// 或者也可以手动写 getter 和 setter
public class UserProperties {
    private String name;
    private Integer age;
    private String email;
    private List<String> hobbies;
}

步骤 3:在需要的地方注入并使用

@Service
public class MyService {
    @Autowired
    private UserProperties userProperties;
    public void doSomething() {
        System.out.println("User name: " + userProperties.getName());
        System.out.println("User hobbies: " + userProperties.getHobbies());
    }
}

别忘了在启动类上添加 @EnableConfigurationProperties 注解(但如果你像上面一样在配置类上使用了 @Component,则不需要)。

五、常用配置示例

# 服务器配置
server:
  port: 8080
  servlet:
    context-path: /api # 应用上下文路径
# 数据源配置
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
  # JPA 配置
  jpa:
    hibernate:
      ddl-auto: update # 生产环境不要用 create-drop 或 update
    show-sql: true
# 日志配置
logging:
  level:
    root: info
    org.springframework.web: debug
    com.example: trace
  file:
    name: logs/myapp.log # 输出到文件
# 自定义配置
myapp:
  feature:
    enabled: true
    api-url: https://api.example.com

总结

Spring Boot 的参数配置系统非常灵活和强大,其核心思想是:

到此这篇关于SpringBoot 的参数配置示例全解析的文章就介绍到这了,更多相关SpringBoot 参数配置内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文