Results 1 to 3 of 3

Thread: Set bean properties from a Properties bean, not assign the properties bean

  1. #1

    Post Set bean properties from a Properties bean, not assign the properties bean

    Is it possible to assign the properties of a bean, to properties contained within a Properties object, loaded using util:properties ?

    Atm, I have a property holder class, that get's assigned the properties object, and lazely get's the properties out e.g:

    public String getSearchURL(){
    return props.getProperty("search");
    }

    I know I can do it using PropertyPlaceholderConfigurer, but I can't in my case, because I already used that to selectively load a properties file.

    What I would like to do:
    <bean id="propertyConfigurer"
    class="PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:environment.properties"/>
    </bean>

    <util:properties id="testURLs" value="classpath:test-urls.properties"/>
    <util:properties id="devURLs" value="classpath:dev-urls.properties"/>

    <bean id="urlsHolder" class="my.package.URLHolder">
    <property name="searchWebServiceURL" value="${env}URLs.search"/>
    <property name="addWebServiceURL" value="${env}URLs.add"/>
    <property name="deleteWebServiceURL" value="${env}URLs.delete"/>
    </bean>

    environment.properties:
    env=test

    test-urls.properties:
    search=http://test/testerTeam/search
    add=http://test/testerTeam/add
    delete=http://test/testerTeam/delete

    dev-urls.properties:
    search=http://dev/developmentWS/search
    add=http://dev/developmentWS/add
    delete=http://dev/developmentWS/delete



    Something else comes to mind - in a simpler environemt, it could be solved. If let's say, the url's all had a common suffix - let's say all SEARCH url's used {something}/WebServices/search.ws , then you could have this:

    wsPrefix.properties:
    #TEST
    #wsHost=http://testHost/test/
    #DEV
    wsHost=http://devServer/thisProject/

    wsUrls:
    search=WebServices/search
    add=WebServices/add
    delete=WebServices/delete

    <bean id="propertyConfigurer"
    class="PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:wsPrefix.properties"/>
    <property name="location" value="classpath:wsUrls.properties"/>
    </bean>

    <bean id="urlsHolder" class="my.package.URLHolder">
    <property name="searchWebServiceURL" value="${wsHost}${search}"/>
    <property name="addWebServiceURL" value="${wsHost}${add}"/>
    <property name="deleteWebServiceURL"value="${wsHost}${delete }"/>
    </bean>

    I think that would work, but I haven't tried it. Plus, it would only work if the url's all conformed. This might work in my environment, but I would rather do something more general, like what I described above.

    Thoughts? Am I missing something?

  2. #2
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    You may use multiply property placeholder configurers, each of them take context in the state in which is left by the previous one.
    So you may use 1st to selectevely load properties files,second configure your URLs and so forth. You just have to use different prefixes for them or just or just set them to ignore unresolved properties.

    This topic has been discussed here, so you may try to search for examples.

    Regards,
    Oleksandr

    Quote Originally Posted by antony.stubbs View Post
    Is it possible to assign the properties of a bean, to properties contained within a Properties object, loaded using utilroperties ?

    Atm, I have a property holder class, that get's assigned the properties object, and lazely get's the properties out e.g:

    public String getSearchURL(){
    return props.getProperty("search");
    }

    I know I can do it using PropertyPlaceholderConfigurer, but I can't in my case, because I already used that to selectively load a properties file.

    What I would like to do:
    <bean id="propertyConfigurer"
    class="PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:environment.properties"/>
    </bean>

    <utilroperties id="testURLs" value="classpath:test-urls.properties"/>
    <utilroperties id="devURLs" value="classpath:dev-urls.properties"/>

    <bean id="urlsHolder" class="my.package.URLHolder">
    <property name="searchWebServiceURL" value="${env}URLs.search"/>
    <property name="addWebServiceURL" value="${env}URLs.add"/>
    <property name="deleteWebServiceURL" value="${env}URLs.delete"/>
    </bean>

    environment.properties:
    env=test

    test-urls.properties:
    search=http://test/testerTeam/search
    add=http://test/testerTeam/add
    delete=http://test/testerTeam/delete

    dev-urls.properties:
    search=http://dev/developmentWS/search
    add=http://dev/developmentWS/add
    delete=http://dev/developmentWS/delete



    Something else comes to mind - in a simpler environemt, it could be solved. If let's say, the url's all had a common suffix - let's say all SEARCH url's used {something}/WebServices/search.ws , then you could have this:

    wsPrefix.properties:
    #TEST
    #wsHost=http://testHost/test/
    #DEV
    wsHost=http://devServer/thisProject/

    wsUrls:
    search=WebServices/search
    add=WebServices/add
    delete=WebServices/delete

    <bean id="propertyConfigurer"
    class="PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:wsPrefix.properties"/>
    <property name="location" value="classpath:wsUrls.properties"/>
    </bean>

    <bean id="urlsHolder" class="my.package.URLHolder">
    <property name="searchWebServiceURL" value="${wsHost}${search}"/>
    <property name="addWebServiceURL" value="${wsHost}${add}"/>
    <property name="deleteWebServiceURL"value="${wsHost}${delete }"/>
    </bean>

    I think that would work, but I haven't tried it. Plus, it would only work if the url's all conformed. This might work in my environment, but I would rather do something more general, like what I described above.

    Thoughts? Am I missing something?

  3. #3

    Cool

    Thanks for the prompt reply! Great!
    I am even more impressed with Spring now
    I have been scouring this forum for this information, but I guess I was unlucky to not stumble across my answer, or was looking for the wrong thing! Hopefully this post will help others.
    Here are the appropriate links to examples:
    http://forum.springframework.org/sho...derconfigurers
    http://forum.springframework.org/sho...derconfigurers
    http://forum.springframework.org/sho...derconfigurers

    And finally:
    http://forum.springframework.org/sea...earchid=826807

    Quote Originally Posted by al0 View Post
    You may use multiply property placeholder configurers, each of them take context in the state in which is left by the previous one.
    So you may use 1st to selectevely load properties files,second configure your URLs and so forth. You just have to use different prefixes for them or just or just set them to ignore unresolved properties.

    This topic has been discussed here, so you may try to search for examples.

    Regards,
    Oleksandr

Posting Permissions

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