Search code examples
spring-bootyamlspring-el

Use SpringBoot SpEL in YAML


Is it possible in spring boot yaml configuration to use SpEL to call static method functions? I'm wanting to find an available port between a port range and set the server port. I know this can be done programmatically but am curious if this can be done just in YAML with SpEL. I've tried the following which does not work as it sees the SpEL as a YAML comment

server:
    port: #{T(org.springframework.util.SocketUtils).findAvailableTcpPort(8000,9000)}

This one fails as it tries to set the SpEL as a string to the port

server:
    port: "#{T(org.springframework.util.SocketUtils).findAvailableTcpPort(8000,9000)}"

Solution

  • The docs say that

    Finally, while you can write a SpEL expression in @Value, such expressions are not processed from Application property files.

    So you will need to move the SpEL to a @Value annotation, but can supply values for it via YAML.