@Component
public class Sample {
private int date = 200;
public int getDate() {
return date;
}
public void setDate(int date) {
this.date = date;
}
}
@Component
public class AppRunner implements ApplicationRunner {
@Value("#{1 + 1}")
int value;
@Value("#{'Hello ' + 'world'}")
String greeting;
@Value("#{1 eq 1}")
boolean trueOfFalse;
@Value("hello")
String hello;
@Value("${my.value}")
int myValue;
@Value("#{${my.value} eq 100}")
boolean isMyValue100;
@Value("#{'spring'}")
String spring;
@Value("#{sample.date}")
int sampleDate;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("=============================");
System.out.println(value);
System.out.println(greeting);
System.out.println(trueOfFalse);
System.out.println(hello);
System.out.println(myValue);
System.out.println(isMyValue100);
System.out.println(spring);
System.out.println(sampleDate);
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("2 + 100");
Integer value = expression.getValue(Integer.class);
System.out.println(value);
}
}
여기서도 역시 ConversionService를 사용해 변환하게 된다.
참고