Announcement Announcement Module
Collapse
No announcement yet.
Set a ${property} at runtime in the application context Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Set a ${property} at runtime in the application context

    In my application context I want to be able to do
    <property name="serviceUrl" value="${webappContextUrl}/myservice.service">

    But ${webappContextUrl} is determined at runtime initialization and it's not in a *.properties file.
    Instead a bean called JnlpWebappContextUrlLocator determines it.
    How do I make my JnlpWebappContextUrlLocator.getWebAppContextUrl() bind to ${webappContextUrl} in the application context?

    Code:
    /**
     * @author Geoffrey De Smet
     */
    public class JnlpWebappContextUrlLocator &#123;
    
        private String fallBackWebAppContextUrl;
    
        public String getFallBackWebAppContextUrl&#40;&#41; &#123;
            return fallBackWebAppContextUrl;
        &#125;
    
        public void setFallBackWebAppContextUrl&#40;String fallBackWebAppContextUrl&#41; &#123;
            this.fallBackWebAppContextUrl = fallBackWebAppContextUrl;
        &#125;
    
        /**
         * Uses the webstart API to determine the webapp context.
         * If used outside of webstart, <code>fallBackWebAppContextUrl</code> is returned.
         * @return the url to the webapp ending with a slash
         */
        public String getWebAppContextUrl&#40;&#41; &#123;
            String webAppContextUrl;
            try &#123;
                BasicService basicService = &#40;BasicService&#41; ServiceManager.lookup&#40;"javax.jnlp.BasicService"&#41;;
                String codeBase = basicService.getCodeBase&#40;&#41;.toExternalForm&#40;&#41;;
                // Remove "jnlp/" or "jnlp"
                int webAppContextUrlLength = codeBase.lastIndexOf&#40;"jnlp"&#41;; // TODO better solution required
                webAppContextUrl = codeBase.substring&#40;0, webAppContextUrlLength&#41;;
            &#125; catch &#40;UnavailableServiceException e&#41; &#123;
                // TODO logging
                webAppContextUrl = fallBackWebAppContextUrl;
            &#125;
            return webAppContextUrl;
        &#125;
    &#125;
    PS: I 'll submit it as a patch to spring rich

  • #2
    I don't think there is an easy way - the post configurator for properties is active at configuration time while your property is evaluated at runtime (after the context has been evaluated and instantiated). I think you should use a different mechanism.

    Comment


    • #3
      How is the lifecycle need of my class different from that of org.springframework.beans.factory.config.PropertyP laceholderConfigurer?

      Comment


      • #4
        You need to call a mehtod on a bean if I understood correctly. This means that your bean has to be already intialized. With property files you have the info available at runtime but before the context is parsed.

        Comment


        • #5
          It's possible, look at ServletContextPropertyPlaceholderConfigurer

          Comment


          • #6
            Right! I had no idea there is a property place holder for web application also .

            Comment

            Working...
            X