Spring Boot-11

. **`application.properties` 或 `application.yml` 文件**

Spring Boot 支持使用 `application.properties` 或 `application.yml` 文件来定义和读取应用配置。这是最常见的配置方式。

**示例:`application.properties`**


server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secret

**示例:`application.yml`**


server:
  port: 8080

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: secret

@Value` 注解

`@Value` 注解用于注入 `application.properties` 或 `application.yml` 中定义的配置值。

**示例:**


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {

    @Value("${server.port}")
    private int serverPort;

    @Value("${spring.datasource.url}")
    private String datasourceUrl;

    // Getter and Setter
}

@ConfigurationProperties` 注解

`@ConfigurationProperties` 注解用于将配置文件中的属性映射到 Java Bean。它比 `@Value` 更适合处理复杂的配置结构。

**示例:**


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties {

    private String url;
    private String username;
    private String password;

    // Getters and Setters
}

**配置文件:**


spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: secret

`Environment` 接口

`Environment` 接口允许你在应用运行时程序化地访问配置属性。

**示例:**


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class MyService {

    @Autowired
    private Environment env;

    public void printProperties() {
        String datasourceUrl = env.getProperty("spring.datasource.url");
        System.out.println("Datasource URL: " + datasourceUrl);
    }
}

@PropertySource

`@PropertySource` 注解用于从外部配置文件中加载属性。可以与 `@Configuration` 类结合使用。

**示例:**


import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:custom.properties")
public class AppConfig {
    // Configuration code
}

**配置文件:`custom.properties`**


custom.property=value

Spring Cloud Config

Spring Cloud Config 是一个集中式配置管理服务,用于管理分布式系统中的配置。它支持将配置存储在 Git、SVN 或文件系统中,并且可以在运行时动态更新配置。

**示例:**

1. 在 `bootstrap.yml` 或 `bootstrap.properties` 中配置 Spring Cloud Config 客户端:


   spring:
     cloud:
       config:
         uri: http://localhost:8888

2. 创建 `@ConfigurationProperties` 类来绑定配置:


   import org.springframework.boot.context.properties.ConfigurationProperties;
   import org.springframework.stereotype.Component;

   @Component
   @ConfigurationProperties(prefix = "custom")
   public class CustomProperties {

       private String property;

       // Getters and Setters
   }

系统环境变量

系统环境变量也可以用于配置 Spring Boot 应用。这些变量可以在 `application.properties` 或 `application.yml` 文件中通过 `ENV_VAR_NAME` 形式访问。

**示例:**


server.port=${PORT:8080}

如果环境变量 `PORT` 被设置,它将覆盖默认的 `8080`。

命令行参数

在启动应用时,你可以通过命令行参数覆盖配置文件中的属性。

**示例:**


java -jar your-app.jar --server.port=9090

在应用中,你可以通过 `@Value` 或 `Environment` 接口来访问这些参数。

总结

  1. application.properties/application.yml:在Spring Boot项目中,可以在application.properties或application.yml文件中定义配置属性。Spring Boot会自动读取这些文件,并将配置属性加载到应用程序的上下文中。可以使用@Value注解或@ConfigurationProperties注解来注入配置属性。

  2. 注解@ConfigurationProperties:可以使用@ConfigurationProperties注解将配置属性绑定到Java Bean上。可以通过将prefix属性设置为配置文件中的前缀来限定绑定的属性。

  3. @Value注解:可以使用@Value注解将配置属性直接注入到Java Bean中。可以使用SpEL表达式来动态设置注入的值。

  4. Environment接口:可以通过Environment接口来读取配置属性。可以使用getProperty方法来获取配置属性的值。

  5. @PropertySource注解:可以使用@PropertySource注解来指定要加载的配置文件。可以使用@Value注解或Environment接口来读取配置属性。

  6. 外部化配置:Spring Boot支持将配置属性外部化,可以将配置文件放置在特定的位置,如操作系统的环境变量、系统属性、命令行参数或JNDI等。

  7. 配置中心:Spring Boot支持将配置属性集中管理,可以使用配置中心的服务来集中管理和分发配置属性。常见的配置中心有Spring Cloud Config、Apache ZooKeeper、Consul等。

  • 26
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值