sitelink1  
sitelink2  
sitelink3  
extra_vars6  

클래스의 full package 는 org.springframework.boot.context.properties.ConfigurationProperties 이다.

 

기본값인지는 모르지만 application.yml 에 다음과 같이 정의해두고 이를 로직에서 참조 할 수 있게 해준다.

 

# 사용자 정의 설정 (Custom Configuration)

jangbogo:

  config:

    localdb-name: jangbogo-dev

    localdb-path: ./db

    max-retry-count: 3

    timeout-seconds: 30

    debug-mode: true

    app-version: 1.0.0

 

그리고 다음과 같이 클래스를 정의해 둔다.

 

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

import org.springframework.stereotype.Component;

 

/**

 * 장보고 프로젝트 사용자 정의 설정

 * application.yml의 jangbogo.config 값을 바인딩

 */

@Component

@ConfigurationProperties(prefix = "jangbogo.config")

public class JangbogoConfig {

    

    private String localdbName;

    private String localdbPath;

    private int maxRetryCount;

    private int timeoutSeconds;

    private boolean debugMode;

    private String appVersion;

    

    // Getters and Setters

    

    public String getLocaldbName() {

        return localdbName;

    }

    

    public void setLocaldbName(String localdbName) {

        this.localdbName = localdbName;

    }

    

    public String getLocaldbPath() {

        return localdbPath;

    }

    

    public void setLocaldbPath(String localdbPath) {

        this.localdbPath = localdbPath;

    }

    

    public int getMaxRetryCount() {

        return maxRetryCount;

    }

    

    public void setMaxRetryCount(int maxRetryCount) {

        this.maxRetryCount = maxRetryCount;

    }

    

    public int getTimeoutSeconds() {

        return timeoutSeconds;

    }

    

    public void setTimeoutSeconds(int timeoutSeconds) {

        this.timeoutSeconds = timeoutSeconds;

    }

    

    public boolean isDebugMode() {

        return debugMode;

    }

    

    public void setDebugMode(boolean debugMode) {

        this.debugMode = debugMode;

    }

    

    public String getAppVersion() {

        return appVersion;

    }

    

    public void setAppVersion(String appVersion) {

        this.appVersion = appVersion;

    }

    

    /**

     * 특정 속성값을 key로 가져오는 메서드

     * PropertiesUtil.get("LOCALDB_NAME") 같은 방식 지원

     */

    public String get(String propertyName) {

        switch (propertyName.toUpperCase()) {

            case "LOCALDB_NAME":

            case "LOCALDB-NAME":

                return localdbName;

            case "LOCALDB_PATH":

            case "LOCALDB-PATH":

                return localdbPath;

            case "MAX_RETRY_COUNT":

            case "MAX-RETRY-COUNT":

                return String.valueOf(maxRetryCount);

            case "TIMEOUT_SECONDS":

            case "TIMEOUT-SECONDS":

                return String.valueOf(timeoutSeconds);

            case "DEBUG_MODE":

            case "DEBUG-MODE":

                return String.valueOf(debugMode);

            case "APP_VERSION":

            case "APP-VERSION":

                return appVersion;

            default:

                return null;

        }

    }

    

    @Override

    public String toString() {

        return "JangbogoConfig{" +

                "localdbName='" + localdbName + '\'' +

                ", localdbPath='" + localdbPath + '\'' +

                ", maxRetryCount=" + maxRetryCount +

                ", timeoutSeconds=" + timeoutSeconds +

                ", debugMode=" + debugMode +

                ", appVersion='" + appVersion + '\'' +

                '}';

    }

}

 

이제 사용하려는 클래스 상단에 전역 변수로 다음과 같이 변수를 선언해준다.

 

    @Autowired

    private JangbogoConfig jangbogoConfig;

 

다음과 같이 사용하면 된다.

 

        ObjectMapper objectMapper = new ObjectMapper();

        ObjectNode node = objectMapper.createObjectNode();

 

        // 사용자 정의 설정 값 가져오기 예제

        // 방법 1: 직접 접근

        node.put("localdbName", jangbogoConfig.getLocaldbName());

        node.put("localdbPath", jangbogoConfig.getLocaldbPath());

        

        // 방법 2: get() 메서드 사용 (PropertiesUtil.get("LOCALDB_NAME") 스타일)

        String dbName = jangbogoConfig.get("LOCALDB_NAME");

        String dbPath = jangbogoConfig.get("LOCALDB_PATH");

        String appVersion = jangbogoConfig.get("APP_VERSION");

        

        logger.info("설정 값 조회 - LOCALDB_NAME: {}, LOCALDB_PATH: {}, APP_VERSION: {}", 

                    dbName, dbPath, appVersion);

        

        // 설정 정보를 JSON에 추가

        ObjectNode configNode = objectMapper.createObjectNode();

        configNode.put("localdbName", dbName);

        configNode.put("localdbPath", dbPath);

        configNode.put("maxRetryCount", jangbogoConfig.getMaxRetryCount());

        configNode.put("timeoutSeconds", jangbogoConfig.getTimeoutSeconds());

        configNode.put("debugMode", jangbogoConfig.isDebugMode());

        configNode.put("appVersion", appVersion);

        node.set("config", configNode);

        

        // 중첩 객체 추가

        ObjectNode userData = objectMapper.createObjectNode();

        userData.put("name", "홍길동");

        userData.put("level", 5);

        node.set("user", userData);

        

        return node;