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;

 

 

 

번호 제목 글쓴이 날짜 조회 수
공지 [작성중/인프런] 스프링부트 시큐리티 & JWT 강의 황제낙엽 2023.12.20 4436
» @ConfigurationProperties 사용하기 황제낙엽 2025.10.23 1465
83 Spring Boot 에서 서버 로직의 역할별 애노테이션 구분 황제낙엽 2025.10.13 1275
82 (확인전) [2021.03.12] Eclipse에서 Spring Boot로 JSP사용하기(Gradle) file 황제낙엽 2023.12.23 2664
81 Spring Boot PetClinic Sample Application 황제낙엽 2023.12.21 2439
80 Eclipse, Spring Boot, Gradle, SVN 레거시(2019) 시스템 유지보수 환경 구축 file 황제낙엽 2023.11.14 1600
79 [시리즈 강좌] 스프링부트로 웹서비스 구축하기 황제낙엽 2023.07.13 1567
78 Spring Boot에서의 RESTful API 모듈 file 황제낙엽 2020.04.16 1772
77 [POST] Spring MVC 구조 분석 황제낙엽 2024.01.17 1943
76 Spring Framework 에서 사용하는 annotation 황제낙엽 2024.01.17 1988
75 Spring MVC configuration file 황제낙엽 2024.01.17 1938
74 [스프링 시큐리티 OAuth2] 강의 자료와 학습용 소스 file 황제낙엽 2024.01.15 1488
73 OAuth 2.0 Resource Server - Spring Security OAuth2.0 황제낙엽 2023.12.27 1903
72 Spring, JSP, Gradle, Eclipse 환경 구축[2] - 샘플 프로젝트 file 황제낙엽 2023.12.24 1809
71 Spring, JSP, Gradle, Eclipse 환경 구축[1] - 레퍼런스 조사 황제낙엽 2023.12.23 1946
70 spring-security-samples 황제낙엽 2023.12.22 1692
69 [Bard] Spring 과 Spring Boot의 차이 file 황제낙엽 2023.12.21 1685
68 Spring 과 Spring Boot의 차이 file 황제낙엽 2020.05.26 2191
67 Spring Security OAuth2.0 파헤치기 황제낙엽 2019.09.05 1973
66 Spring Security OAuth2구현 file 황제낙엽 2019.09.05 2085