SpringBoot怎么读取配置文件中的数据到map和list

发布时间:2022-02-23 09:07:36 作者:iii
来源:亿速云 阅读:723

SpringBoot怎么读取配置文件中的数据到map和list

引言

在Spring Boot应用程序中,配置文件是一个非常重要的部分。它允许我们将应用程序的配置信息从代码中分离出来,使得应用程序更加灵活和易于维护。Spring Boot支持多种格式的配置文件,如application.propertiesapplication.yml。在这些配置文件中,我们可以定义各种属性,如数据库连接信息、服务器端口、日志级别等。

在某些情况下,我们可能需要将配置文件中的数据读取到Java的MapList中,以便在应用程序中使用。本文将详细介绍如何在Spring Boot中实现这一功能。

1. Spring Boot配置文件概述

1.1 配置文件格式

Spring Boot支持两种主要的配置文件格式:

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

1.2 配置文件的加载顺序

Spring Boot会按照以下顺序加载配置文件:

  1. 当前目录下的/config子目录
  2. 当前目录
  3. 类路径下的/config
  4. 类路径根目录

如果在多个位置存在相同的配置文件,Spring Boot会按照上述顺序进行覆盖,后面的配置会覆盖前面的配置。

1.3 配置文件的优先级

Spring Boot还支持通过命令行参数、环境变量等方式来覆盖配置文件中的属性。例如,可以通过命令行参数--server.port=8081来覆盖application.properties中的server.port属性。

2. 读取配置文件中的数据到Map

2.1 使用@ConfigurationProperties注解

Spring Boot提供了@ConfigurationProperties注解,可以将配置文件中的数据绑定到Java对象中。我们可以使用这个注解将配置文件中的数据读取到Map中。

2.1.1 创建配置类

首先,我们需要创建一个配置类,并使用@ConfigurationProperties注解来指定配置前缀。

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

import java.util.Map;

@Component
@ConfigurationProperties(prefix = "myapp")
public class MyAppProperties {

    private Map<String, String> settings;

    public Map<String, String> getSettings() {
        return settings;
    }

    public void setSettings(Map<String, String> settings) {
        this.settings = settings;
    }
}

在这个例子中,我们创建了一个MyAppProperties类,并使用@ConfigurationProperties(prefix = "myapp")注解来指定配置前缀为myappsettings字段是一个Map<String, String>类型的属性,用于存储配置文件中的数据。

2.1.2 配置文件

接下来,我们需要在配置文件中定义myapp.settings属性。

myapp:
  settings:
    key1: value1
    key2: value2
    key3: value3

在这个例子中,我们定义了一个myapp.settings属性,并将其值设置为一个包含三个键值对的Map

2.1.3 使用配置类

最后,我们可以在应用程序中使用MyAppProperties类来访问配置文件中的数据。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @Autowired
    private MyAppProperties myAppProperties;

    @GetMapping("/settings")
    public Map<String, String> getSettings() {
        return myAppProperties.getSettings();
    }
}

在这个例子中,我们创建了一个MyController类,并通过@Autowired注解将MyAppProperties注入到控制器中。然后,我们在/settings端点中返回settings属性的值。

2.2 使用@Value注解

除了使用@ConfigurationProperties注解,我们还可以使用@Value注解来将配置文件中的数据读取到Map中。

2.2.1 创建配置类

首先,我们需要创建一个配置类,并使用@Value注解来注入配置文件中的数据。

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

import java.util.Map;

@Component
public class MyAppProperties {

    @Value("#{${myapp.settings}}")
    private Map<String, String> settings;

    public Map<String, String> getSettings() {
        return settings;
    }

    public void setSettings(Map<String, String> settings) {
        this.settings = settings;
    }
}

在这个例子中,我们使用@Value("#{${myapp.settings}}")注解来将myapp.settings属性的值注入到settings字段中。

2.2.2 配置文件

接下来,我们需要在配置文件中定义myapp.settings属性。

myapp:
  settings:
    key1: value1
    key2: value2
    key3: value3

2.2.3 使用配置类

最后,我们可以在应用程序中使用MyAppProperties类来访问配置文件中的数据。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @Autowired
    private MyAppProperties myAppProperties;

    @GetMapping("/settings")
    public Map<String, String> getSettings() {
        return myAppProperties.getSettings();
    }
}

2.3 使用Environment对象

除了使用@ConfigurationProperties@Value注解,我们还可以使用Environment对象来读取配置文件中的数据。

2.3.1 创建配置类

首先,我们需要创建一个配置类,并使用Environment对象来读取配置文件中的数据。

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

import java.util.HashMap;
import java.util.Map;

@Component
public class MyAppProperties {

    @Autowired
    private Environment env;

    public Map<String, String> getSettings() {
        Map<String, String> settings = new HashMap<>();
        settings.put("key1", env.getProperty("myapp.settings.key1"));
        settings.put("key2", env.getProperty("myapp.settings.key2"));
        settings.put("key3", env.getProperty("myapp.settings.key3"));
        return settings;
    }
}

在这个例子中,我们使用Environment对象来读取myapp.settings属性中的值,并将其存储在一个Map中。

2.3.2 配置文件

接下来,我们需要在配置文件中定义myapp.settings属性。

myapp:
  settings:
    key1: value1
    key2: value2
    key3: value3

2.3.3 使用配置类

最后,我们可以在应用程序中使用MyAppProperties类来访问配置文件中的数据。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @Autowired
    private MyAppProperties myAppProperties;

    @GetMapping("/settings")
    public Map<String, String> getSettings() {
        return myAppProperties.getSettings();
    }
}

3. 读取配置文件中的数据到List

3.1 使用@ConfigurationProperties注解

与读取Map类似,我们也可以使用@ConfigurationProperties注解将配置文件中的数据读取到List中。

3.1.1 创建配置类

首先,我们需要创建一个配置类,并使用@ConfigurationProperties注解来指定配置前缀。

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

import java.util.List;

@Component
@ConfigurationProperties(prefix = "myapp")
public class MyAppProperties {

    private List<String> items;

    public List<String> getItems() {
        return items;
    }

    public void setItems(List<String> items) {
        this.items = items;
    }
}

在这个例子中,我们创建了一个MyAppProperties类,并使用@ConfigurationProperties(prefix = "myapp")注解来指定配置前缀为myappitems字段是一个List<String>类型的属性,用于存储配置文件中的数据。

3.1.2 配置文件

接下来,我们需要在配置文件中定义myapp.items属性。

myapp:
  items:
    - item1
    - item2
    - item3

在这个例子中,我们定义了一个myapp.items属性,并将其值设置为一个包含三个元素的List

3.1.3 使用配置类

最后,我们可以在应用程序中使用MyAppProperties类来访问配置文件中的数据。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @Autowired
    private MyAppProperties myAppProperties;

    @GetMapping("/items")
    public List<String> getItems() {
        return myAppProperties.getItems();
    }
}

3.2 使用@Value注解

除了使用@ConfigurationProperties注解,我们还可以使用@Value注解来将配置文件中的数据读取到List中。

3.2.1 创建配置类

首先,我们需要创建一个配置类,并使用@Value注解来注入配置文件中的数据。

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

import java.util.List;

@Component
public class MyAppProperties {

    @Value("${myapp.items}")
    private List<String> items;

    public List<String> getItems() {
        return items;
    }

    public void setItems(List<String> items) {
        this.items = items;
    }
}

在这个例子中,我们使用@Value("${myapp.items}")注解来将myapp.items属性的值注入到items字段中。

3.2.2 配置文件

接下来,我们需要在配置文件中定义myapp.items属性。

myapp:
  items:
    - item1
    - item2
    - item3

3.2.3 使用配置类

最后,我们可以在应用程序中使用MyAppProperties类来访问配置文件中的数据。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @Autowired
    private MyAppProperties myAppProperties;

    @GetMapping("/items")
    public List<String> getItems() {
        return myAppProperties.getItems();
    }
}

3.3 使用Environment对象

除了使用@ConfigurationProperties@Value注解,我们还可以使用Environment对象来读取配置文件中的数据。

3.3.1 创建配置类

首先,我们需要创建一个配置类,并使用Environment对象来读取配置文件中的数据。

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

import java.util.ArrayList;
import java.util.List;

@Component
public class MyAppProperties {

    @Autowired
    private Environment env;

    public List<String> getItems() {
        List<String> items = new ArrayList<>();
        items.add(env.getProperty("myapp.items[0]"));
        items.add(env.getProperty("myapp.items[1]"));
        items.add(env.getProperty("myapp.items[2]"));
        return items;
    }
}

在这个例子中,我们使用Environment对象来读取myapp.items属性中的值,并将其存储在一个List中。

3.3.2 配置文件

接下来,我们需要在配置文件中定义myapp.items属性。

myapp:
  items:
    - item1
    - item2
    - item3

3.3.3 使用配置类

最后,我们可以在应用程序中使用MyAppProperties类来访问配置文件中的数据。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @Autowired
    private MyAppProperties myAppProperties;

    @GetMapping("/items")
    public List<String> getItems() {
        return myAppProperties.getItems();
    }
}

4. 读取嵌套的Map和List

在实际应用中,配置文件中的数据可能会更加复杂,包含嵌套的MapList。Spring Boot同样支持读取这些复杂的数据结构。

4.1 读取嵌套的Map

4.1.1 创建配置类

首先,我们需要创建一个配置类,并使用@ConfigurationProperties注解来指定配置前缀。

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

import java.util.Map;

@Component
@ConfigurationProperties(prefix = "myapp")
public class MyAppProperties {

    private Map<String, Map<String, String>> nestedSettings;

    public Map<String, Map<String, String>> getNestedSettings() {
        return nestedSettings;
    }

    public void setNestedSettings(Map<String, Map<String, String>> nestedSettings) {
        this.nestedSettings = nestedSettings;
    }
}

在这个例子中,我们创建了一个MyAppProperties类,并使用@ConfigurationProperties(prefix = "myapp")注解来指定配置前缀为myappnestedSettings字段是一个Map<String, Map<String, String>>类型的属性,用于存储嵌套的Map

4.1.2 配置文件

接下来,我们需要在配置文件中定义myapp.nestedSettings属性。

myapp:
  nestedSettings:
    group1:
      key1: value1
      key2: value2
    group2:
      key1: value1
      key2: value2

在这个例子中,我们定义了一个myapp.nestedSettings属性,并将其值设置为一个包含两个嵌套MapMap

4.1.3 使用配置类

最后,我们可以在应用程序中使用MyAppProperties类来访问配置文件中的数据。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @Autowired
    private MyAppProperties myAppProperties;

    @GetMapping("/nested-settings")
    public Map<String, Map<String, String>> getNestedSettings() {
        return myAppProperties.getNestedSettings();
    }
}

4.2 读取嵌套的List

4.2.1 创建配置类

首先,我们需要创建一个配置类,并使用@ConfigurationProperties注解来指定配置前缀。

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

import java.util.List;

@Component
@ConfigurationProperties(prefix = "myapp")
public class MyAppProperties {

    private List<List<String>> nestedItems;

    public List<List<String>> getNestedItems() {
        return nestedItems;
    }

    public void setNestedItems(List<List<String>> nestedItems) {
        this.nestedItems = nestedItems;
    }
}

在这个例子中,我们创建了一个MyAppProperties类,并使用@ConfigurationProperties(prefix = "myapp")注解来指定配置前缀为myappnestedItems字段是一个List<List<String>>类型的属性,用于存储嵌套的List

4.2.2 配置文件

接下来,我们需要在配置文件中定义myapp.nestedItems属性。

myapp:
  nestedItems:
    - 
      - item1
      - item2
    - 
      - item3
      - item4

在这个例子中,我们定义了一个myapp.nestedItems属性,并将其值设置为一个包含两个嵌套ListList

4.2.3 使用配置类

最后,我们可以在应用程序中使用MyAppProperties类来访问配置文件中的数据。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @Autowired
    private MyAppProperties myAppProperties;

    @GetMapping("/nested-items")
    public List<List<String>> getNestedItems() {
        return myAppProperties.getNestedItems();
    }
}

5. 使用@PropertySource注解加载自定义配置文件

在某些情况下,我们可能需要加载自定义的配置文件,而不是使用默认的application.propertiesapplication.yml文件。Spring Boot提供了@PropertySource注解来实现这一功能。

5.1 创建配置类

首先,我们需要创建一个配置类,并使用@PropertySource注解来指定自定义配置文件的路径。

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.List;

@Configuration
@PropertySource("classpath:custom.properties")
public class CustomProperties {

    @Value("${custom.items}")
    private List<String> items;

    public List<String> getItems() {
        return items;
    }

    public void setItems(List<String> items) {
        this.items = items;
    }
}

在这个例子中,我们创建了一个CustomProperties类,并使用@PropertySource("classpath:custom.properties")注解来指定自定义配置文件的路径为classpath:custom.propertiesitems字段是一个List<String>类型的属性,用于存储配置文件中的数据。

5.2 自定义配置文件

接下来,我们需要在src/main/resources目录下创建一个名为custom.properties的文件,并定义custom.items属性。

custom.items=item1,item2,item3

在这个例子中,我们定义了一个custom.items属性,并将其值设置为一个包含三个元素的List

5.3 使用配置类

最后,我们可以在应用程序中使用CustomProperties类来访问自定义配置文件中的数据。

”`java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;

@RestController public class MyController {

@Autowired
private CustomProperties customProperties;

@GetMapping
推荐阅读:
  1. java中map和list的区别是什么
  2. 如何判断List和Map是否相等并合并List中相同的Map

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot list map

上一篇:javascript中如何用Switch语句

下一篇:laravel中软删除的实例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》