SpringEL

after spring 3.0 - spring core

core doc 4

Spring Data Spring Security Thymeleaf등 에서 사용

AppRunner.java
package com.harm.firstvscodespringboot;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
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.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;

@Component
public class AppRunner implements ApplicationRunner {
    private Logger logger = LoggerFactory.getLogger(AppRunner.class);
    
    @Value("spring")
    private String vauleSpring;
    @Value("#{'hello' + 'world'}")
    private String valueStrConcat;
    @Value("#{1 + 1}")
    private int valueIntEval;
    @Value("${my.value1}")
    private String valueProperty1;
    @Value("#{${my.value1} eq 101}")
    private String valueProperty2Eval;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        String logText = "Hello my first Spring Boot in VSCode";
        logger.debug(logText);
        logger.debug("vauleSpring : {}", this.vauleSpring);
        logger.debug("valueStrConcat : {}", this.valueStrConcat);
        logger.debug("valueIntEval : {}", this.valueIntEval);
        logger.debug("valueProperty1 : {}", this.valueProperty1);
        logger.debug("valueProperty2Eval : {}", this.valueProperty2Eval);

        ExpressionParser parser = new SpelExpressionParser();
        StandardEvaluationContext context = new StandardEvaluationContext();
        Expression expression = parser.parseExpression("1 + 100");
        Integer valueInt = (Integer) expression.getValue();
        String valueStr = (String) expression.getValue(String.class);
        logger.debug("valueInt from parser {}", valueInt);
        logger.debug("valueStr from parser {}", valueStr);
    }
}

그 결과

Last updated

Was this helpful?