admin管理员组

文章数量:1124668

I am trying to load the following YAML configuration into a Spring Boot application using @ConfigurationProperties:

project:
  component:
    users:
      default:
        username: "JohnDoe"
      anotherUser:
        username: "AliceSmith"

AppProperties class:

@Getter
@Setter
@Accessors(fluent = false)
@ConfigurationProperties(prefix = "projectponent")
@Component
public class AppProperties {
    private UsersConfiguration users = new UsersConfiguration();

    @Bean
    UsersConfiguration users() {
        return users;
    }
}

UsersConfiguration class:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class UsersConfiguration {
    private Map<String, User> users = new HashMap<>();
}

User record:

public record User(String username) {}

The Problem:

The users bean is exposed via the users() method in AppProperties. However, when debugging, the users field in AppProperties is empty (size = 0), even though the YAML configuration contains valid data.

Questions:

  1. Why is the users map in AppProperties empty, even though the YAML file seems correct?
  2. Are there additional steps or configurations required to bind YAML into a Map<String, User> in Spring Boot?

Any help would be greatly appreciated! Thank you

Verified the YAML structure matches the expected Map<String, User> format.

本文标签: javaConfigurationProperties map is empty when binding YAML configuration in Spring BootStack Overflow