Tip : ApplicationRunner를 상속받게 되면
Springboot 어플리케이션이 실행된 다음에 아래의 run 코드를 실행 하게 됨
@Component
public class AppRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
}
}
@Component
public class AppRunner implements ApplicationRunner {
@Value("#{1 + 1}")
int value;
@Value("#{'hello ' + 'world'}")
String greeting;
@Value("#{1 eq 1}")
boolean trueOrflase;
@Value("hello")
String hello;
@Value("${my.value}")
int myValue;
//표현식 안에 property를 넣음
@Value("#{${my.value} eq 100}")
boolean ismyValue100;
@Value("#{'spring'}")
String spring;
//spEL은 빈을 주입받아 사용 가능함.
@Value("#{sample.data}")
int sampleData;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("=================");
System.out.println(value);
System.out.println(greeting);
System.out.println(trueOrflase);
System.out.println(hello);
System.out.println(myValue);
System.out.println(ismyValue100);
System.out.println(spring);
System.out.println(sampleData);
//Expression parser를 이용하여 spEL을 사용
//String > Integer 변환시에 저번에 배웠던 ConversionService를 사용한다.
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("2 + 100");
Integer value = expression.getValue(Integer.class);
System.out.println(value);
}
}
my.value = 100
@Component
public class Sample {
private int data = 200;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
실행결과
스프링 EL 이란?
SpEL 구성
문법
실제로 어디서 쓰나?