Announcement Announcement Module
Collapse
No announcement yet.
Set bean properties from a Properties bean, not assign the properties bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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 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?

  • #2
    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

    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?

    Comment


    • #3
      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

      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

      Comment

      Working...
      X