Using @Value annotation with static final variable in Spring Framework
Asked Answered
M

3

9

I want to make the Request Mappings in my Spring application dynamic. So that my url can not be understandable. And I can show anything meaningless to the user and still mapping purpose will be resolved.

For that I am storing the dynamic part of the URL in properties file. And want to use that in the @RequestMapping annotation. And the same thing will be done on the client side in JSP. I will read the value from the property file and then create the href.

I am using @Value annotation to read the property file values.

There is one class that holds all such values in final static variables.

public class UrlMappingAbstraction {
    public static final @Value("#{urlAbstractionProperties['url.message']?:'message'}") String MESSAGE = "";
}

And I am extending this class in my controller and using the static final field in the @RequestMapping annotation like below.

@RequestMapping(value="/"+MESSAGE+"/{id}", method=RequestMethod.GET)

And in jsp also I am reading the value from property file using <spring:message/> and generating the url in href.

The problem is jsp able to create the correct url based on the property file value but in the @RequestMapping annotation my value is not getting replaced.

Can anybody tell me the exact problem? I know that we can not change the value of static final variable after its initialized. Then what's the use of @Value annotation.

If this can be done another way then you can also show me it.

Thanks in advance.

Meras answered 21/1, 2013 at 12:57 Comment(0)
A
6

Annotations are static by their nature, therefore you cannot do it this way.

@Value cannot be used on static fields, but it doesn't matter here - the real problem is that there is no way to use values other than compile time constants as attributes of annotations.

You can use one of the following alternatives:

  • Add a URL rewrite filter (such as this or this) and configure it to perform the necessary conversion.

    This approach looks more elegant due to clear separation of responsibilities - controllers are responsible for doing their jobs, rewrite filter is responsible for obfuscation of URLs.

  • Intercept creation of controller mappings by overriding RequestMappingHandlerMapping. getMappingForMethod() and change their URL patterns at this step (not tested)

Auberon answered 21/1, 2013 at 14:1 Comment(1)
The second option looks more promising to me as it will be more challenging. :-D I don't want to go with the first option because I've already figured it out before asking this question. And I found that it will need more configuration plus an overhead of extra dependency in my already overloaded project. Thanks for your help. Once I'll get back to my office I'll check this out and come back to you. :)Meras
M
1

You can follow this approach

@Value("${name}")
private String name;

private static String NAME_STATIC;

@Value("${name}")
public void setNameStatic(String name){
    PropertyController.NAME_STATIC = name;
}

src - https://www.baeldung.com/spring-inject-static-field

Mildew answered 20/9, 2022 at 4:20 Comment(0)
F
0

I will augment @axtavt's suggestions by saying you should just do it in reverse. Do you really need to make the message URL runtime configurable?

If you don't than just make a static variable just like you have it but with out the @Value:

public final class UrlMapping {
    public static final String MESSAGE = "message";
}

Then in your JSP refer to UrlMapping.MESSAGE instead of the properties file.

Although its not as flexible its far simpler and IMHO its a bad idea to make endpoint URLs too configurable because inevitably you will hardcode something either in Javascript or in a template. Also changing URLs are bad for SEO.

Freud answered 21/1, 2013 at 14:15 Comment(2)
My web application purpose is not for SEO. Its first priority is security. That's why I want to go with something like this. I've already figured out your option but as per that option I need to write 'UrlMapping.MESSAGE' part in JSP using scriptlet. And that is very bad thing to write scriplets in jsp. IE will slap you sometimes if it found that. :D But thanks for your time and help. :)Meras
@Japs of course this is "my opinion" but JSPs and Internet Explorer are both almost equally evil :) . I prefer Handlebars.java, Freemarker, or Scalate. But if JSPs float your boat...Freud

© 2022 - 2024 — McMap. All rights reserved.