接口 Dict


public interface Dict
Represents a dictionary item used for internationalized value descriptions.

Implementations of this interface define a structured way to associate a code (typically stored in the database) with a dictionary namespace (called dictName) to facilitate localized text retrieval.

Implementation Types

This interface can be implemented in two ways:
  • Enum-based: Recommended for static and predefined dictionaries (e.g. order status, gender). These are type-safe, support scanning, configuration export, and web API introspection.
  • JavaBean-based: Suitable for dynamic dictionaries where items are not known at compile time. These classes must have a no-arg constructor and can return a dummy value (e.g. null or "*") from code(). Their primary purpose is to define the dictName(), which acts as a configuration anchor.

Tools such as the dict-i18n-generator-maven-plugin will include both Enum and JavaBean-based implementations when generating configuration files (e.g. YML, SQL), ensuring users can conveniently manage all dictionary sources.

Note: Runtime features that enumerate all dictionary items (e.g. web API /dict/items) only support Enum-based dictionaries, as JavaBean implementations are designed to work with dynamic data sources (e.g. MySQL, Redis) where dictionary codes are determined externally at runtime.

Example (Enum):


 public enum OrderStatus implements Dict {
     PENDING("pending"),
     SHIPPED("shipped");

     private final String code;

     OrderStatus(String code) {
         this.code = code;
     }

     @Override
     public String dictName() {
         return "order_status";
     }

     @Override
     public String code() {
         return this.code;
     }
 }
 

This interface is commonly used in conjunction with @DictDesc to support response enhancement and configuration generation across various internationalization loaders.

  • 方法概要

    修饰符和类型
    方法
    说明
    Returns the internal code or identifier of a dictionary item.
    Returns the dictionary name (namespace) used for grouping related dictionary items.
  • 方法详细资料

    • dictName

      String dictName()
      Returns the dictionary name (namespace) used for grouping related dictionary items.

      Typically formatted like "order_status", this value is used as part of the lookup key when resolving localized text (e.g. order_status.PENDING).

      Important: If Dict is implemented by an enum, all enum constants in the same enum class must return the same dictName. Returning different values will result in undefined behavior in dictionary scanning, caching, or configuration generation.

      返回:
      the dictionary name (namespace)
    • code

      String code()
      Returns the internal code or identifier of a dictionary item.

      This value usually comes from the business object (e.g. database field), and is used to match against the localized dictionary value.

      返回:
      the code representing a dictionary item