can we use spring expressions (spel) in other annotations?

One way you can find out things like this is to have a look yourself. This is an example for eclipse, but it should work similarly for other IDEs:

First of all, make sure you have the sources of the spring libraries you are using. This is easiest if you use maven, using the maven-eclipse-plugin or using m2eclipse.

Then, in Eclipse select Navigate -> Open Type.... Enter the type you are looking for (something like RequestMa* should do for lazy typers like myself). Enter / OK. Now right-click the class name in the source file and select References -> Project. In the search view, all uses of this class or annotation will appear.

One of them is DefaultAnnotationHandlerMapping.determineUrlsForHandlerMethods(Class, boolean), where this code snippet will tell you that expression language is not evaluated:

ReflectionUtils.doWithMethods(currentHandlerType, new ReflectionUtils.MethodCallback() {
    public void doWith(Method method) {
        RequestMapping mapping = AnnotationUtils.findAnnotation(
                                     method, RequestMapping.class);
        if (mapping != null) {
            String[] mappedPatterns = mapping.value();
            if (mappedPatterns.length > 0) {
                for (String mappedPattern : mappedPatterns) {
                    // this is where Expression Language would be parsed
                    // but it isn't, as you can see
                    if (!hasTypeLevelMapping && !mappedPattern.startsWith("/")) {
                        mappedPattern = "/" + mappedPattern;
                    }
                    addUrlsForPath(urls, mappedPattern);
                }
            }
            else if (hasTypeLevelMapping) {
                urls.add(null);
            }
        }
    }
}, ReflectionUtils.USER_DECLARED_METHODS);

Remember, it's called Open Source. There's no point in using Open Source Software if you don't try to understand what you are using.


Answering in 2020: with current Spring versions, SpEL expressions can be used in @RquestMappning annotations. They are correctly parsed.

Inner details:
Spring's RequestMappingHandlerMapping calls embeddedValueResolver#resolveStringValue.
JavaDoc of EmbeddedValueResolver states the following:

StringValueResolver adapter for resolving placeholders and expressions against a ConfigurableBeanFactory. Note that this adapter resolves expressions as well, in contrast to the ConfigurableBeanFactory.resolveEmbeddedValue method. The BeanExpressionContext used is for the plain bean factory, with no scope specified for any contextual objects to access.

Since: 4.3

This means both regular placeholders (e.g. ${my.property}) and SpEL expressions will be parsed.

Note that because regular placeholders are parsed first and SpEL expressions are parsed later, it's even possible to set the value of a property to a SpEL expression. Spring will first replace the placeholder with the property value (SpEL expression) and then parse the SpEL expression.

Tags:

Java

Spring