본문 바로가기
▶ Back-End/Java

@Value 사용법

by 오늘도 코딩 2023. 9. 21.
728x90
반응형

application.properties에 설정한 값을 가져와 사용하는 @Value

*자세한 설명 생략

 

 

▷ application.properties

*Test를 위한 값 설정

 

# value
test.val = HELLO

# map
test.mapInt = {W:0, O:1, R:2, L:3, D:4}
test.mapStr = {W:'w', O:'o', R:'r', L:'l', D:'d'}

# list
test.listInt = 0, 1, 2, 3, 4
test.listStr = H, E, L, L, O

 

 

▷ Application.java

*간단한 Test를 위해 Application.java에서 진행

*Bean이 생성된 후 사용해야 하기 때문에 CommandLineRunner 사용

*CommandLineRunner : Server가 실행된 후 Override 한 run Method를 실행

*CmmUtile.getObjStr : 아래 관련 글 참고

 

@Slf4j
@SpringBootApplication
public class Application implements CommandLineRunner {

    @Value("${test.val}")
    private String val;

    @Value("#{${test.mapInt}}")
    private Map<String, Integer> mapInt;

    @Value("#{${test.mapStr}}")
    private Map<String, String> mapStr;

    @Value("${test.listInt}")
    private List<Integer> listInt;

    @Value("${test.listStr}")
    private List<String> listStr;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        log.error("val : " + val);
        log.error("mapInt : " + CmmUtile.getObjStr(mapInt));
        log.error("mapStr : " + CmmUtile.getObjStr(mapStr));
        log.error("listInt : " + CmmUtile.getObjStr(listInt));
        log.error("listStr : " + CmmUtile.getObjStr(listStr));
    }
}

 

 

▷ 결과

*Server 구동

 

 

 

▷ 관련 글

 

Objcet to String

Object를 String으로 변환하는 간단한 방법 /** * * Object to String * * @param Object * @return String */ public static String getObjStr(Object obj) { String str = ""; try { str = new ObjectMapper().writeValueAsString(obj); } catch (Exception e)

coding-today.tistory.com

 

 

728x90
728x90

'▶ Back-End > Java' 카테고리의 다른 글

SpringBoot Security Login 기능 추가  (4) 2023.09.26
Objcet to String  (0) 2023.09.21
LocalTime AVG  (0) 2023.09.21
String to LocalTime  (0) 2023.09.21
String to LocalDate  (0) 2023.09.21

댓글