SpringBoot--加载配置文件

本文介绍SpringBoot中配置文件的位置及加载方式,包括默认位置、自定义位置、使用@Component和@Value注解读取属性、使用@ConfigurationProperties简化配置,以及如何根据不同环境加载不同配置。

配置文件的位置

1.默认位置

SpringBoot的默认配置文件名称为application.properties,SpringBoot将从以下列表加载application.properties文件,并将它们添加到Spring Environment中:

  1. 部署时,jar包所在目录的/config子目录
  2. jar包所在目录
  3. classpath目录下的/config子目录
  4. classpath目录

这个列表是按照优先级排序的,如果存在多个重名的application.properties的话,列表中位置高的将覆盖位置低的。

2.自定义位置

如果不喜欢application.properties这个文件名或者需要自定义配置文件的位置,可以在启动SpringBoot应用的时候,传入一个spring.config.location参数,指定配置文件的位置和名称。例:
java -jar xxxx.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
上述例子加载了两个配置文件,分别是位于classpath目录下的default.properties和override.properties文件。

加载配置文件的属性

在配置文件application.properties中配置属性:
test.name = wahaha
test.age = 27
test.tel = 18800118888

1.@Component和@Value("${“xxx”}")

在类中获取配置文件的属性:

@Component
public class Configurations {
    
    @Value("${test.name}")
    private String name;

    @Value("${test.age}")
    private String age;

    @Value("${test.tel}")
    private Long tel;

    // getter and setter
}

在类的域属性上通过@Value("${xxxx}")指定关联属性,SpringApplication会自动加载。

2.@ConfigurationsProperties(prefix=“test”)

在上述方法中,手动书写@Value还是比较繁重的工作,好在SpringBoot提供了更加简洁的方式–@ConfigurationsProperties(prefix=“test”)。
prefix指定了配置文件中属性的前缀是test,并按照属性名进行自动匹配。例如:

@Component
@ConfigurationProperties(prefix = "test")
public class Configuration {

    private String name;

    private String age;

    private Long tel;

    // setter getter
}

另外,@ConfigurationsProperties(prefix=“test”,locations=“xxx.properties”)可以指定配置文件的位置和名称

根据运行环境加载不同的配置

1.Profiles

Profiles提供了一种隔离应用程序配置的方式,并让这些配置只能在特定的环境下生效。在配置文件中,用spring.profiles.active属性来指定特定环境的配置文件。在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:

  • application-dev.properties:开发环境
  • application-test.properties:测试环境
  • application-prod.properties:生产环境
    至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。例如:
    application.properties配置:spring.profiles.active = dev即指定application-dev.properties会被加载。

2.通过启动参数指定运行环境

通过命令启动:

java -jar xxxxx.jar --spring.profiles.active=prod

3.加载多个配置文件

例如:

spring.profiles.active = dev,database

等于告诉Spring Boot加载application-dev.properties和application-database.properties文件,从而实现多个配置文件的加载。

<think>我们有一个Deployment和一个Service的YAML配置。用户要求: 1. 为image: springboot-app添加tag(例如,假设我们使用latest标签,但用户没有指定,我们可以用latest) 2. 添加一个imagePullPolicy: Never 注意:在Kubernetes中,如果使用本地构建的镜像(比如在Minikube环境中),通常需要设置imagePullPolicy: Never,以避免Kubernetes尝试从远程仓库拉取镜像。 修改点: 在Deployment的容器部分: - 将`image: springboot-app` 改为 `image: springboot-app:latest`(或其他具体的tag,如果没有指定,我们可以用latest) - 添加一行`imagePullPolicy: Never` 修改后的Deployment部分如下: ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: springboot-app-deployment spec: replicas: 1 selector: matchLabels: app: springboot-app template: metadata: labels: app: springboot-app spec: containers: - name: springboot-app image: springboot-app:latest # 这里添加了tag imagePullPolicy: Never # 添加这一行 ports: - containerPort: 8080 ``` Service部分不需要修改,因为Service不涉及镜像拉取。 因此,完整的修改后的YAML文件为: ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: springboot-app-deployment spec: replicas: 1 selector: matchLabels: app: springboot-app template: metadata: labels: app: springboot-app spec: containers: - name: springboot-app image: springboot-app:latest imagePullPolicy: Never ports: - containerPort: 8080 --- apiVersion: v1 kind: Service metadata: name: springboot-app-service spec: selector: app: springboot-app ports: - protocol: TCP port: 80 targetPort: 8080 type: LoadBalancer ``` 注意:如果用户实际使用的tag不是latest,请替换为实际的tag。这里我们按照要求添加了tag(使用latest)和imagePullPolicy: Never。 现在,根据要求,在回答后提出3-5个相关问题。</think>### 修改后的 YAML 配置 ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: springboot-app-deployment spec: replicas: 1 selector: matchLabels: app: springboot-app template: metadata: labels: app: springboot-app spec: containers: - name: springboot-app image: springboot-app:latest # 添加了:latest标签 imagePullPolicy: Never # 添加了镜像拉取策略 ports: - containerPort: 8080 --- apiVersion: v1 kind: Service metadata: name: springboot-app-service spec: selector: app: springboot-app ports: - protocol: TCP port: 80 targetPort: 8080 type: LoadBalancer ``` ### 关键修改说明: 1. **添加镜像标签**: - 将 `image: springboot-app` 改为 `image: springboot-app:latest` - 最佳实践:应使用具体版本标签而非 `latest`(如 `v1.2.3`) 2. **添加拉取策略**: - 新增 `imagePullPolicy: Never` 确保使用本地镜像 - 此策略阻止 Kubernetes 尝试从远程仓库拉取镜像 ### 部署验证步骤: ```bash # 应用修改后的配置 kubectl apply -f deployment.yaml # 检查Pod状态(应显示Running) kubectl get pods -l app=springboot-app # 查看详细事件(确认无拉取错误) kubectl describe pod <pod-name> | grep -A 10 Events # 测试服务访问 minikube service springboot-app-service --url ``` ### 重要注意事项: 1. **本地镜像准备**: ```bash # 确保镜像已加载到Minikube minikube image load springboot-app:latest # 验证镜像存在 minikube ssh docker images | grep springboot-app ``` 2. **标签管理建议**: - 生产环境应避免使用 `latest` 标签 - 使用语义化版本控制:`springboot-app:1.5.2` - 每次更新镜像时递增标签版本 3. **替代策略**: ```yaml imagePullPolicy: IfNotPresent # 仅当本地不存在时拉取 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值