DEV Community

eidher
eidher

Posted on • Edited on

1

Spring Expression Language (SpEL)

You can use it as implicit references defining an attribute or an argument in a method:

@Value("#{ systemProperties['user.region'] }") String defaultLocale;
Enter fullscreen mode Exit fullscreen mode

SpEL can access Spring beans:

@Value("#{beanId.method}") BeanType param;
Enter fullscreen mode Exit fullscreen mode

SpEL can access properties as Strings (casting may be needed):

@Value("${property1}") int property1;
@Value("#{environment['property2']}") int property2;
@Value("#{new Integer(environment['property3'])*2}") int property3;
Enter fullscreen mode Exit fullscreen mode

If undefined use : or ?: for SpEL to assign a default value:

@Value("${property1 : 1}") int property1;
@Value("#{environment['property2'] ?: 1}") int property2;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay