1

I wonder if there is a way to extract properties from Spring Environment (e.g. obtained from ApplicationContext) in the form of Properties instance? Or, at least, is there a way to enumerate the properties in Spring Environment, or get them all as a map, or any other way I can turn a [initially unknown] set of properties into a Properties object?

I need this in order to create a jclouds Context by calling org.jclouds.ContextBuilder.newBuilder() and .overrides(Properties). The idea is to configure the actual cloud provider solely by means of .properties file, and I don't want to couple application logic with provider-specific properties.

[UPDATE]

The .properties files to be used are configured using <context:property-placeholder>, and it actually specifies a list of paths, like this:

< context:property-placeholder location=
    "classpath:/jdbc.properties,
    file:${jboss.server.config.dir}/jdbc.properties,
    file:${catalina.home}/conf/jdbc.properties"
    ignore-resource-not-found="true"/>

which suggests that the .properties file is searched in the mentioned list of locations in order. I would like to achieve the following:

  • keep the list of .properties files and their possible locations in this XML definition file only;
  • allow to place jclouds related properties in any of the .properties files mentioned in the XML;
  • access the properties, resolved and loaded by Spring, in the form of Properties object so I am able to feed that to jclouds ContextBuilder.

Please let me know if all of this is feasible. Thank you in advance!

-Vlad

Vlad.Z
  • 160
  • 10

2 Answers2

1

If you wan't to use properties in your Spring configuration then you can simply use:

<context:property-placeholder location="classpath:foo.properties" />

To get the properties in your code later you can simply read this file from the classpath into a Properties object:

props.load(MyClass.class.getClassLoader().getResourceAsStream("foo.properties"));

Alternatively you can have a look at PropertyPlaceholderConfigurer.

UPDATE

Updated after Deinum's remark but only if you are getting the properties from a Spring managed bean:

<util:properties id="myProps" 
           location="classpath:foo.properties"/>
<context:property-placeholder properties-ref="myProps" />

Now you can inject myProps into Spring managed beans (no need to load them again from the classpath).

Stijn Geukens
  • 15,454
  • 8
  • 66
  • 101
  • 1
    Why so "complex". You can simply use `` to read the properties which in turn can also be injected into the `` element. Saves you loading the file twice. – M. Deinum Dec 02 '14 at 12:09
  • 1
    Deinum, you are right but that will only work from Spring managed beans. Since the OP seems to be creating this object himself (newBuilder()) this will not work. – Stijn Geukens Dec 02 '14 at 12:13
  • He needs properties those should be injected, how they are read shouldn't matter. The reading should be, imho, externalized. – M. Deinum Dec 02 '14 at 12:14
  • 1
    How will he inject them in an Object he created himself? I did not make the assumption that he is doing this from a Spring managed bean. What is not externalized in my answer? – Stijn Geukens Dec 02 '14 at 12:20
  • @M.Deinum, is it possible to specify a list of locations to ``? – Vlad.Z Dec 02 '14 at 15:20
  • @StijnGeukens You are right that that is external too I was looking only on how to obtain the `Properties` object so that it can be passed to the `override` method. I would probably create some kind of factory for the creation of the context object so that it is nicely abstracted somehow. – M. Deinum Dec 02 '14 at 15:48
0

You could use PropertiesFactoryBean and do something like this:

<bean id="jcloudsProps" 
    class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="location">
    <value>your/props/file.properties</value>
  </property>
</bean>

You can then use jcloudsProps as you would any other Spring bean.

mkvcvc
  • 1,515
  • 1
  • 18
  • 41
  • is it possible to refer this bean to existing `` element? also, is that true that you can specify more than one value for location? – Vlad.Z Dec 02 '14 at 15:22