Maksym Rybalkin
Maksym Rybalkin

Reputation: 545

Is it possible to use static final variables for Spring Expression Language?

I'm using CaffeineCache in my SpringBoot application, and here is one of my annotations for a method:

@Cacheable(value = PROMOCODE_BY_CONFIG_BUNDLE_CODE, key = "{#configBundleCode, #siebelId ?: 'all'}")
    public Long countPromocodesByConfigBundleCodeAndSiebelId(String configBundleCode, String siebelId) {
        return preferentialMapper.countPromocodesByConfigBundleCodeAndSiebelId(configBundleCode, siebelId, NULL_SIEBEL_PROMOCODE_CACHE_KEY);
    }

I have this variable:

private static final String NULL_SIEBEL_PROMOCODE_CACHE_KEY = "all";

and I want to use it in my SpEL query instead of 'all'. I've tried to use it with # or $ symbols, but that doesn't work.

Is it possible to use the variable in the query, and how?

Upvotes: 1

Views: 447

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121552

That is called a type operator: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#expressions-types

So, you can do like this:

T(com.my.proj.ClassWithConstant).NULL_SIEBEL_PROMOCODE_CACHE_KEY 

But your NULL_SIEBEL_PROMOCODE_CACHE_KEY has to be public.

Upvotes: 3

Related Questions