Sunday, October 22, 2017

Cloud config and SpEL in @Value

Leave a Comment

I met a issue,I try to listen to EnvironmentChangeEvent and re-init some configuration.

While i found that @Value annotation with SpEL is not working, but plain @Value annotation is OK:

@Component public class ConsumeService {      @Autowired     ConsumeConfig consumeConfig;   @EventListener(EnvironmentChangeEvent.class)     void onEnvChange() {      log.debug("{}",consumeConfig);  //when i print here, i get only consumeDesc, but cardList is an empty List.     } }    @Configuration @RefreshScope public class ConsumeConfig {      @Value("${consume.desc}")     private String consumeDesc;      @Value("#{'${api.server.round.card}'.split(',')}")     private List<String> cardList; } 

Question:

Is this issue related with lifecycle of SpEL? It seems that, the value of SpEL is not yet parsed.

1 Answers

Answers 1

The split should return an array of String, not a list. Try this:

@Value("#{'${api.server.round.card}'.split(',')}") private String[] cardList; 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment