I am trying to mimic the @Value
annotation programmatically by retrieving a property from the properties file using Environment
and then evaluate the expression using SpelExpressionParser
.
Here is a code snippet:
@Service
@Slf4j
public class Test {
private String dynamicSPELStr = "#{${test.spel.map}.default}";
@Autowired
private Environment environment;
void testSPEL() {
ExpressionParser parser = new SpelExpressionParser();
log.info("[PARSER] {}", parser
.parseExpression(environment.resolvePlaceholders(dynamicSPELStr))
.getValue());
}
}
and the property is: test.spel.map={default: '5', key1: '10', key2: '20', key3: '40'}
However I am receiving the following error:
Expression [#{{default: '5', key1: '10', key2: '20', key3: '40'}.default}] @1: EL1043E: Unexpected token. Expected 'identifier' but was 'lcurly({)'
Running the same expression using @Value
works just fine.