Managing Spring Boot Configurations with Enums and @ConfigurationProperties

This guide explains how to improve Spring Boot configuration management by defining configuration values as Java enums, mapping them with @ConfigurationProperties, and displaying them via a Thymeleaf front‑end, including Maven dependencies, YAML settings, enum class, config class, controller, and HTML template examples.

Architect's Guide
Architect's Guide
Architect's Guide
Managing Spring Boot Configurations with Enums and @ConfigurationProperties

Why map enums with @ConfigurationProperties

Spring Boot stores configuration in application.yml or application.properties. Embedding raw strings or numbers in code reduces readability and introduces magic‑value bugs. Defining a Java enum for fixed values (e.g., user roles) and binding those enum constants to external configuration via @ConfigurationProperties yields a strongly‑typed, maintainable configuration model.

Project dependencies (pom.xml)

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

Configuration file (application.yml)

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/your_database
    username: your_username
    password: your_password
server:
  port: 8080

app:
  user-type:
    admin: ADMIN
    user: USER
    guest: GUEST
    vip: VIP
    moderator: MODERATOR

Enum definition (UserTypeEnum.java)

public enum UserTypeEnum {
    ADMIN("管理员"),
    USER("普通用户"),
    GUEST("游客"),
    VIP("VIP用户"),
    MODERATOR("版主");

    private final String description;

    UserTypeEnum(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }
}

Configuration properties class (AppConfig.java)

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import lombok.Data;
import com.icoderoad.enumconfig.enums.UserTypeEnum;

@Data
@Component
@ConfigurationProperties(prefix = "app")
public class AppConfig {
    private UserType userType;
    private Database database;
    private Server server;

    @Data
    public static class UserType {
        private UserTypeEnum admin;
        private UserTypeEnum user;
        private UserTypeEnum guest;
        private UserTypeEnum vip;
        private UserTypeEnum moderator;
    }

    @Data
    public static class Database {
        private String url;
        private String username;
        private String password;
    }

    @Data
    public static class Server {
        private int port;
    }
}

Controller that exposes the enum values (UserController.java)

import com.example.demo.config.AppConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class UserController {

    @Autowired
    private AppConfig appConfig;

    @GetMapping("/user-types")
    public String getUserTypes(Model model) {
        model.addAttribute("adminType", appConfig.getUserType().getAdmin().getDescription());
        model.addAttribute("userType", appConfig.getUserType().getUser().getDescription());
        model.addAttribute("guestType", appConfig.getUserType().getGuest().getDescription());
        model.addAttribute("vipType", appConfig.getUserType().getVip().getDescription());
        model.addAttribute("moderatorType", appConfig.getUserType().getModerator().getDescription());
        return "index";
    }
}

Thymeleaf view (index.html)

<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户类型</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <h1 class="mt-5">用户类型</h1>
    <table class="table table-bordered mt-3">
        <thead>
            <tr><th>角色</th><th>描述</th></tr>
        </thead>
        <tbody>
            <tr><td>ADMIN</td><td th:text="${adminType}"></td></tr>
            <tr><td>USER</td><td th:text="${userType}"></td></tr>
            <tr><td>GUEST</td><td th:text="${guestType}"></td></tr>
            <tr><td>VIP</td><td th:text="${vipType}"></td></tr>
            <tr><td>MODERATOR</td><td th:text="${moderatorType}"></td></tr>
        </tbody>
    </table>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Result

When the application starts, Spring Boot binds the app.user-type section of application.yml to AppConfig.UserType. Each property is an instance of UserTypeEnum, so the controller can retrieve the human‑readable description via getDescription() and render it in the Thymeleaf page. This approach eliminates magic strings, improves type safety, and centralises configuration management.

Diagram
Diagram
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentEnumSpring BootConfigurationPropertiesThymeleaf
Architect's Guide
Written by

Architect's Guide

Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.