Results 1 to 6 of 6

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

Hybrid View

  1. #1
    Join Date
    May 2005
    Posts
    394

    Default 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. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    May 2005
    Posts
    394

    Default

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

  4. #4
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  5. #5
    Join Date
    May 2005
    Posts
    394

    Default

    It's possible, look at ServletContextPropertyPlaceholderConfigurer

  6. #6
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Right! I had no idea there is a property place holder for web application also .
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

Similar Threads

  1. Replies: 4
    Last Post: May 31st, 2011, 07:53 PM
  2. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  3. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM
  4. Replies: 6
    Last Post: May 8th, 2005, 11:09 AM
  5. Questioning the core component
    By Martin Kersten in forum Swing
    Replies: 6
    Last Post: Feb 21st, 2005, 03:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •