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)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

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

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay