{"id":17121416,"url":"https://github.com/hellojukay/springboot-configs-guide","last_synced_at":"2026-01-05T14:17:21.991Z","repository":{"id":140615776,"uuid":"366917710","full_name":"hellojukay/springboot-configs-guide","owner":"hellojukay","description":"一篇文章说清楚 springboot 的配置文件是咋回事","archived":false,"fork":false,"pushed_at":"2021-05-17T08:35:58.000Z","size":11,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-29T08:33:49.681Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hellojukay.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-13T02:59:43.000Z","updated_at":"2021-05-17T08:36:00.000Z","dependencies_parsed_at":null,"dependency_job_id":"0614df83-97e2-4033-b02b-29a676039ad7","html_url":"https://github.com/hellojukay/springboot-configs-guide","commit_stats":{"total_commits":11,"total_committers":2,"mean_commits":5.5,"dds":0.2727272727272727,"last_synced_commit":"d791add1225d1e803fa7cd2041960f53dab29d0e"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellojukay%2Fspringboot-configs-guide","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellojukay%2Fspringboot-configs-guide/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellojukay%2Fspringboot-configs-guide/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellojukay%2Fspringboot-configs-guide/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hellojukay","download_url":"https://codeload.github.com/hellojukay/springboot-configs-guide/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245197789,"owners_count":20576262,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-14T18:02:15.782Z","updated_at":"2026-01-05T14:17:21.946Z","avatar_url":"https://github.com/hellojukay.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# springboot-configs-guide\n总结一下 SpringBoot 配置文件的用法\n\n## 创建项目\n创建一个标准的 SpringBoot 项目，可以从这个页面生成模板项目\n```\nhttps://start.spring.io/\n```\n## 基本配置文件\n创建项目以后默认会在 `resources` 目录生成 `application.properties` 文件，如果这个文件不存在也没有关系，可以自己生成这个文件.文件名字取别的不行\n\n```\n└── src\n    ├── main\n    │   ├── java\n    │   │   └── com\n    │   │       └── example\n    │   │           └── springboot\n    │   │               └── Application.java\n    │   └── resources\n    │       ├── application.properties\n    │       ├── static\n    │       └── templates\n```\nresouces 目录下的 application.properties ，还有一些其他的配置也会自动读取(优先级从高到低)\n\n1. resources 目录下的 bootstrap.yml(我们一般用这个配置来覆盖 spring 的一些默认行为)\n2. 通过命令行指定 --spring.config.additional-location,\n3. 通过命令行指定的 --spring.config.location=xxx.yml\n3. ${PWD}/config/application.yml 也会自动读取\n5. classpath 下面的 config/application.yml\n6. ${PWD}} 下面的 application.yml \n\n```\n备注： 这里的 PWD 是程序的工作空间\n```\n需要接介绍的是，我们可以同时指定多个文件当成配置文件，如果有冲突，后面的配置文件会覆盖前面的配置，例如：\n```\njava -jar aap.jar --spring.config.location=classpath:application.yml,file:app.yml\n```\n这里  `app.yml` 中的配置在冲突情况下会覆盖 `classhpath:application.yml` 中的配置.\n### 文件格式\n目前 springboot 支持2种格式的配置文件\n* yml\n* properties\n\n如果 `properties` 格式文件不喜欢，没有关系，可以只将这个文件转化成 `yml` 格式的配置文件。\n### profiles\n推荐的做法是，在 application.yml 中写入默认配置，通过其他的配置文件覆盖里面的配置，关于环境有关的信息，我们写在其他的文件中，比如数据库的帐号密码，其他服务的配置信息。这里我们就需要用到多个配置文件组合来配置了\n\n```\n└── src\n    ├── main\n    │   ├── java\n    │   │   └── com\n    │   │       └── example\n    │   │           └── springboot\n    │   │               ├── Application.java\n    │   │               └── DemoController.java\n    │   └── resources\n    │       ├── application-local.yml\n    │       ├── application.yml\n```\n类似的 `application-{env}.yml` 的形式，\n这里的 {env} 就是环境，\n\n因为 `yaml` 文件的特性，我们也可以将多个 `profile`写在一个 yml 文件中，只需要用 `---` 分开即可，比如\n```\nspring:\n  profile: local\ndemo:\n  mame: aaa\n\n---\n\nspring:\n  profile: prod\ndemo:\n  name: bbb\n```\n\n如果我们想要激活 如果我们想要激活某一个 `profile` 的配置，我们有三种方式,优先级从高到低\n\n方法1: \n\n通过命令行参数指定,在 spring 的启动参数上加上\n```\njava -jar --spring.profiles.active=local\n```\n\n方法2：\n\n在 application.yml 中配置 spring.profiles.active\n```\nspring:\n    profiles:\n        active: local\n```\n\n方法3:\n\n通过`SPRING_PROFILES_ACTIVE` 环境变量来控制，如下\n```\nexport SPRING_PROFILES_ACTIVE=local\njava -jar app.jar\n```\n\n如果 `application-{env}.yml` 被激活，那么`application-{env}.yml` 中的配置将覆盖掉 `application.yml` 中的配置\n\n## 配置引用\n在定义一个公共配置以后，在其他的配置项中，还可以引用这个配置\n```\ndemo:\n  msg: hello ${name}\nname: Bob\n```\n\n## 取值方式\nspringboot 有三种取值方式，优先级从高到低\n\n1. 命令行参数\n\n在 SpringBoot 项目及中用到的配置，我们都可以通过命令行的方式指定，不管是 SpringBoot 的一些内置配置项目，还是我们自己定义的配置，比如：\n```yaml\ndemo:\n    name: Bod\n```\n我们定义了 `demo.name`,并且在代理里面引用了这个配置\n```\n  @Value(\"${demo.name}\")\n```\n我们也可以通过命令行设置 `demo.name` 的值，它将覆盖掉 `application.yml`的配置\n```\njava -jar app.jar --demo.name=Nick\n```\n\n2. 配置文件\n\n写在 yml 或者 properties 文件中的值都能被读取\n\n3. 环境变量\n\nxxx.yyy.zzz 这样的配置，我们可以通过设置环境变量 XXX_YYY_ZZZ 这样的方式来注入值\n\n\n## 默认值\n我们在通过  Value 注解配置一些默认，如下：\n```\n@Value(\"${demo.name：Bob}\")\n```\n在没有配置`demo.name` 或者环境变量，以及命令行参数的时候，就会使用默认值 `Bob`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhellojukay%2Fspringboot-configs-guide","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhellojukay%2Fspringboot-configs-guide","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhellojukay%2Fspringboot-configs-guide/lists"}