SoFunction
Updated on 2025-04-07

Practice of springboot importing multiple configuration files

1. Introduction

Spring Boot has supported importing files to load configuration parameters since version 2.. Unlike -location, it does not need to be set in advance, and the file types that support import are relatively richer.

We only need to configure the list of files that need to be imported through attributes in the /config file.

The following is a brief introduction to several configuration files.

2. Import method

classpath

useclasspath:Prefix to specify the configuration file located in the classpath. This is usually used to reference resources inside a project, such assrc/main/resourcesFiles in the directory.

spring:
  config:
    import: classpath:

file

usefile:Prefix to specify the configuration file in the file system. This allows you to reference configuration files located outside of the project.

spring:
  config:
    import: file:/path/to/

optional

useoptional:The prefix can specify a configuration file. If the file does not exist, no error will be thrown and the application will continue to start. This is very useful when referencing optional configurations.

spring:
  config:
    import: optional:classpath:

nacos

If you use Nacos as the configuration center, you can usenacos:Prefix to import configuration files in Nacos.

spring:
  cloud:
    nacos:
      config:
        import: nacos:data-id:group-id

Wildcard import

You can use wildcard characters (e.g.*) to import all configuration files in a directory. This is useful when multiple configuration files need to be loaded together.

spring:
  config:
    import: classpath:config-templates/*.yml

No extension file

If you need to import files without extensions, you can add square brackets after the file name, for exampleclasspath:additional-application[.yml]

spring:
  config:
    import: classpath:additional-application[.yml]

Directory import

You can import the configuration files in the entire directory, and Spring Boot will automatically load all the configuration files in the directory.propertiesand.ymldocument.

spring:
  config:
    import: classpath:config-import-dir/

Environment-specific configuration files

Although notDirect options, but you can create environment-specific configuration files (e.g.) and load them by activating the corresponding profile.

java -jar  --=dev

Command line parameters

Although notorSet it in, but you can import the configuration file through command line parameters when starting the application.

java -jar  --=classpath:

These options provide a flexible way to organize and manage your profile, allowing you to adjust the configuration of your application based on different environments and needs.

3. Sample code

server:
  port: 8080
spring:
  application:
    name: spring-config-multiple-demo
  config:
    import:
      - classpath:
      - classpath:
      - classpath:

learn:
  cond:
    enabled: false

test:
  app:
    id: 123
    name: "Zhang San"

com:
  user:
    user-list:
      - name: "Zhang San"
        id: 1
      - name: "Li Si"
        id: 2
    person-list:
      - name: "Wang Wu"
        id: 3
      - name: "Zhao Liu"
        id: 4

This is the article about the practice of importing multiple configuration files in springboot. For more related contents of importing multiple configuration files in springboot, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!