SMALL
  • 스프링의 표현식으로 스프링의 모든 영역에서 사용 가능
  • @Value(”$(config.value)”) 처럼 사용
  • SpEL의 값 Evaluation
    • Spellparser는 “” 안에 있는 문자열을 Eval 해서 결과값을 만들어냄
    • String 객체를 new로 생성하여 사용할 수도 있음
    • ExpressionParser parser = new SpelExpressionParser();
      Expression exp = parser.parseExpression("'Hello World'");
      String message = (String) exp.getValue(); //"Hello World"
      
      Expression expWow = 
      	parser.parseExpression("'Hello world'.concat('!')");
      String messageWow = (String) expWow.getValue(); //"Hello World!"
      
      Expression expString =
      	parser.parseExpression("new String('hello world').toUpperCase()");
      String messageString = expString.getValue(String.class); //"HELLO WORLD"
      
  • Bean의 Property를 설정할 때 사용하는 방식
    • 보통 직전의 방식 보다는 아래와 같이 사용함
    • 기본적으로 #{<표현식>} 이렇게 사용
    • application.properties(또는 application.yml)의 값을 가져올 땐 ${<property 이름>} 방식으로 가져옴 ex) CPU의 개수 등
    • 테스트 환경과 운영 환경을 분리하고 싶을 때도 사용
    @Component
    public class SimpleComponent {
    	@Value("#{ 1+1 }")
    	int two; // 2
    
    	@Value("#{ 2 eq 2}")
    	boolean isTrue; // true
    
    	@Value("${ server.hostname }")
    	String hostName; // www.server.com
    
    	@Value("#{ ${ server.hostname } eq 'www.server.com'}")
    	boolean isHostSame; // true
    
    
LIST

+ Recent posts