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";
}
}
this doesn't appear to work now, am I missing something?
SpEL supports a wide range of features, such as calling methods, accessing properties, and calling constructors. As an example of method invocation, we call the 'concat' method on the string literal. ExpressionParser parser = new SpelExpressionParser(); Expression exp = parser. parseExpression("'Hello World'.
public class StandardEvaluationContext extends Object implements EvaluationContext. A powerful and highly configurable EvaluationContext implementation. This context uses standard implementations of all applicable strategies, based on reflection to resolve properties, methods and fields.
Spring Boot basic annotations tutorial shows how to use basic Spring Boot annotations including @Bean, @Service, @Configuration, @Controller, @RequestMapping, @Repository, @Autowired, and @SpringBootApplication. Spring is a popular Java application framework for creating enterprise applications.
Why do we need Conditional Beans? A Spring application context contains an object graph that makes up all the beans that our application needs at runtime. Spring's @Conditional annotation allows us to define conditions under which a certain bean is included into that object graph.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With