Results 1 to 3 of 3

Thread: Getting a resource URL in ApplicationContext?

  1. #1
    Join Date
    Sep 2004
    Location
    Bonn/Germany
    Posts
    10

    Default Getting a resource URL in ApplicationContext?

    I have a bean, MyDigester, that internally uses a Digester to create objects from an XML file. The digester rules are not defined programmatically, but rather in a file /WEB-INF/mydigester.xml.

    MyDigester's ctor takes as a single argument a URL to the digester rules. Thus, in the bean definition, I'd like to write something like this

    Code:
      <bean
       id="mydigester"
       class="...MyDigester"
       singleton="false">
       <constructor-arg><value>/WEB-INF/mydigester.xml</value></constructor-arg>
      </bean>
    Presumably the value string is automatically converted to a URL, but unfortunately, java.net.URL seems to accept only absolute URLs.

    Programmatically I can get at the URL with
    appctx.getResource("/WEB-INF/mydigester.xml").getURL(), but I don't see a way to do it declaratively, which I'd prefer obviously.

    Michael

  2. #2
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

    You'd have to register a customereditorconfigurer along with a PropertyEditor that is capable of resolving Strings to URLs. The difficulty here is that you need the servletcontext or the application context.

    This is resolved by extending implementing ApplicationContextAware and casting it to a WebApplicationContext (so this only works in WebApplicationContext):

    Code:
    public void UrlResourceEditor 
    extends PropertyEditorSupport 
    implements ApplicationContextAware &#123;
    
      ApplicationContext ctx;
      public void setApplicationContext&#40;&#41; &#123; .. &#125;
    
      public void setAsText&#40;String text&#41; &#123;
        WebApplicationContext webAppCtx = &#40;WebApplication&#41;ctx;
        Resource res = webAppCtx.getResource&#40;text&#41;;
        setValue&#40;res.getUrl&#40;&#41;&#41;;
      &#125;
    &#125;
    Register the custom editor using the CustomEditorConfigurer (see sect. 3.13 of the reference manual).

  3. #3
    Join Date
    Sep 2004
    Location
    Bonn/Germany
    Posts
    10

    Default

    There already is org.springframework.beans.propertyeditors.URLEdito r which currently just passes the string to the ctor of java.net.URL. If the URL string is relative, the latter will throw a MalformedURLException. In this case, wouldn't it be better, if the URLEditor would resolve relative URLs? I'm an utter newbie regarding Spring, thus I'm not sure whether a PropertyEditor can usefully be ApplicationContextAware.

    Michael

Similar Threads

  1. Context initialization failed
    By kanonmicke in forum Container
    Replies: 7
    Last Post: Sep 29th, 2005, 12:35 AM
  2. Odd behaviour when injecting TransactionTemplate
    By damon311 in forum Container
    Replies: 3
    Last Post: Jul 23rd, 2005, 11:21 AM
  3. Replies: 2
    Last Post: Jul 21st, 2005, 04:07 AM
  4. Resource: Add valid path not found
    By moacsjr in forum Data
    Replies: 3
    Last Post: May 24th, 2005, 05:53 PM
  5. Quartz problem
    By khem in forum Web
    Replies: 4
    Last Post: Aug 17th, 2004, 02:34 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
  •