您的位置:首页 > 游戏 > 游戏 > Spring Boot-11

Spring Boot-11

2024/10/6 16:25:48 来源:https://blog.csdn.net/Flying_Fish_roe/article/details/140892002  浏览:    关键词: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: 8080spring:datasource:url: jdbc:mysql://localhost:3306/mydbusername: rootpassword: 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/mydbusername: rootpassword: 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 {@Autowiredprivate 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等。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com