스프링 EL?
간단한 예제)
package me.jinmin;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class AppRunner implements ApplicationRunner {
@Value("#{1 + 1}")
int value;
@Value("#{'hello ' + 'world'}")
String greeting;
@Value("#{1 eq 1}")
boolean trueOrFalse;
@Value("hello")
String hello;
@Value("${my.value}")
int myValue;
@Value("#{${my.value} + 1}")
int myValuePlus;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("===============");
System.out.println(value);
System.out.println(greeting);
System.out.println(trueOrFalse);
System.out.println(hello);
System.out.println(myValue);
System.out.println(myValuePlus);
}
}
결과
print:
===============
2
hello world
true
hello
100
101
표현식 : #{} // 프로퍼티 : ${}
표현식(#{})을 실행하고 결과값을 프로퍼티(int value;, String greeting;에 넣는다.
표현식은 프로퍼티를 가질 수 있지만, 반대는 안 된다.
#{${my.value} + 1} 가능.빈을 참고하는 것도 가능하다.
\main\java\me\jinmin\Sample
package me.jinmin;
import org.springframework.stereotype.Component;
@Component
public class Sample {
private int value = 200;
}
\main\java\me\jinmin\AppRunner
package me.jinmin;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class AppRunner implements ApplicationRunner {
@Value("#{sample.value}")
int sampleValue;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("===============");
System.out.println(sampleValue);
}
}
print:
===============
200
실제 사용은 어디서 할까?
@Value(위의 예제들)
@ConditionalOnExpression

메소드 시큐리티, @PreAuthorize, @PostAuthorize, @PreFilter, @PostFilter
간단 예)
@PreAuthorize("hasRole('ROLE_USER')")
public void create(Contact contact);
@PreAuthorize("hasPermission(#contact, 'admin')")
public void deletePermission(Contact contact, Sid recipient, Permission permission);
@PreAuthorize("#contact.name == authentication.name")
public void doSomething(Contact contact);
@Query
간단 예)
@Query("select u from User u where u.age = ?#{[0]}")
List<User> findUsersByAge(int age);
@Query("select u from User u where u.firstname = :#{#customer.firstname}")
List<User> findUsersByCustomersFirstname(@Param("customer") Customer customer);
ExpressionParser의 예) (⇒ ConversionService를 사용)
package me.jinmin;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.stereotype.Component;
@Component
public class AppRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("2 + 100");
int value = expression.getValue(Integer.class);
System.out.println(value);
}
}
print:
102