ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [스프링] SpEL - Spring Expression Language
    스프링프레임워크/기타 2021. 7. 16. 18:14

    SpEL (Spring Expression Language)

    런타임시에 객체 그래프를 조회하고 조작하는 강력한 표현 언어

     

    SpEL 표기법

    #{SpEL 표현식}

     

    SpEL 지원 기능

    • 리터럴 표현식
    • Boolean과 관계연산자
    • 정규 표현식
    • 클래스 표현식
    • 프로퍼티, 배열, 리스트, 맴에 대한 접근 지원
    • 메서드 호출
    • 관계 연산자
    • 할당
    • 생성자 호출
    • Bean 참조
    • 배열 생성
    • 인라인 리스트/맵
    • 삼항 연산자
    • 변수
    • 사용자 정의 함수
    • Collections Projection
    • Collections Selection
    • Templated expression

    @Value annotation에서 SpEL 사용

    @Value("#{1+1}")
    int value;
     
    @Value("#{'hello ' + 'world'}")
    String greeting;
     
    @Value("#{1 eq 5}")
    boolean trueOrFalse;
     
    @Value("Literal String")
    String literalString;
     
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(value);          // 2
        System.out.println(greeting);       // hello world
        System.out.println(trueOrFalse);    // false
        System.out.println(literalString);  // Literal String
    }

     

    프로퍼티 접근 지원

    application.properties

    my.value=100
    @Value("#{'${my.value}' eq '100'}")
    boolean isEqual;
     
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(isEqual);   // true
    }

     

    Bean 참조

    @Component
    public class Sample {
        
        private int value = 123;
     
        public int getValue() {
            return value;
        }
    }
    @Value("#{sample.Value}")
    int sampleValue;
     
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(sampleValue);	// 123
    }

    '스프링프레임워크 > 기타' 카테고리의 다른 글

    [Batch] 스프링 배치 테이블 초기화 방법  (0) 2021.07.29
    [RestDoc] asciidoc 문법  (0) 2021.06.03

    댓글

Designed by Tistory.