Using @Value annotation with static final variable in Spring Framework

23,917

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)

Share:
23,917
Japan Trivedi
Author by

Japan Trivedi

I am a JAVA guy. Loves to work with Spring Framework and GWT. Also like to explore new technologies. I am very good at R&D and problem solving. I have developed a Spring Application without creating any damn XML file in the application(not even web.xml) :) profile for Japan Trivedi on Stack Exchange, a network of free, community-driven Q&A sites http://stackexchange.com/users/flair/910060.png

Updated on April 25, 2020

Comments

  • Japan Trivedi
    Japan Trivedi about 4 years

    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.

  • Japan Trivedi
    Japan Trivedi over 11 years
    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. :)
  • Japan Trivedi
    Japan Trivedi over 11 years
    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. :)
  • Adam Gent
    Adam Gent over 11 years
    @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...