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

I want to be able to do this:

@Controller @RequestMapping("/#{handlerMappingPaths.security}/*") public class SecurityController { etc //for instance, to resuse the value as a base for the folder resolution @Value("#{handlerMappingPaths.security}/") public String RESOURCE_FOLDER; @RequestMapping(value="/signin-again", method = RequestMethod.GET) public String signinAgainHandler() { return RESOURCE_FOLDER + "signin_again"; } } 

Now it doesn’t work, am I missing something?

+6
java spring
source share
2 answers

@Sean answered the question of whether spring supports, but I also wanted to answer the question of how to simply not duplicate the configuration when using annotations. It turns out that this is possible using static imports, as in:

 import static com.test.util.RequestMappingConstants.SECURITY_CONTROLLER_PATH @Controller @RequestMapping("/" + SECURITY_CONTROLLER_PATH + "/*") public class SecurityController { etc //for instance, to resuse the value as a base for the folder resolution public String RESOURCE_FOLDER = SECURITY_CONTROLLER_PATH + "/"; @RequestMapping(value="/signin-again", method = RequestMethod.GET) public String signinAgainHandler() { return RESOURCE_FOLDER + "signin_again"; } } 
0
source share

One way to find out how it looks is to see for yourself. This is an example for eclipse, but it should work similarly for other IDEs:

First of all, make sure you have the spring library sources that you use. 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 models like me). Type / OK. Now right-click the class name in the source file and select References -> Project . All applications of this class or annotation appear in the search view.

One of them is DefaultAnnotationHandlerMapping.determineUrlsForHandlerMethods (class, logical) , where this piece of code will tell you that the language of the expression 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 that it is called Open Source. It makes no sense to use open source software if you are not trying to understand what you are using.

+2
source share

All Articles