Get Spring Environment as Properties - java

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

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).

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.

Related

How to load a property file just once and use it throughout the application in java?

I have a scenario where i need to show the success/error messages in jsp's from the controller. The controller has access to many methods and each method may return some message. These messages are stored as key-value pair in a property file which i need to access. Now i want to load this property file just once and use it through out the application. How can this be achieved? the framework is spring mvc. Presently i am doing something like this in every class but this approach doesn't seem right. Please help!
Properties prop = new Properties();
prop.load(getClass().getClassLoader().getResourceAsStream(fileName+".properties"));
What you are looking for is probably what is described in this blog post.
In short, here is what you have to do:
First tell spring where the properties file in your spring configuration.
That will look something like:
<context:property-placeholder location="classpath:application.properties"/>
Second you inject the properties into Spring beans with the #Value annotation
you can load the properties file using spring configurations as below:
<context:property-placeholder location="classpath:application.properties"/>
Add the above configuration in the spring beans configuration file and put the properties file under the source folder which are by default in the classpath.
#Value annotation can be used for injecting the property values at your controller. Something like this:
#Value("${name}")
private String name;
your properties file entries would look like this:
enter code herename=your name
I hope this explanation would solve your problems.

Spring MVC putting values into XML Config file

I am working a Spring MVC project and in my Service object I need some information like system password, id, url etc but I would like to put this into one of the XML files so it can be changes without changing code.. which XML should I put it in and how do I read it into the object
Moving constants to XML is a first step, but to make your application truly configurable you should use external .properties file:
<context:property-placeholder location="file:///foo/bar/conf.properties" />
And then use it everywhere in your XML configuration:
<property name="password" value="${db_password}"/>
Where conf.properties contains:
db_password=secret
Note that you can also place properties file inside WAR (with location="classpath:/foo/bar/conf.properties").
If you are a happy user of Spring 3.1 (currently RC2) you can take advantage of new #PropertySourceannotation:
#Configuration
#PropertySource("classpath:/com/myco/app.properties")

Setting up connection time out using configuration

I realized that I haven't set the time out for the JDBCTemplate using the setQueryTimeOut method. My code is in production as I would ideally want a solution to set the timeout from some configuration instead of recompiling the code. Is there a way to set the query time out via say the data source configuration or any other property outside the Java.
I tried via the accepted solution to this post. Didn't work for me. I get org.springframework.beans.NotWritablePropertyException: Invalid property 'connectionProperties' of bean class
You can use the queryTimeout field with configuration:
In your JDBCTempalte xml - <property name="queryTimeout" value="${query.timeout} />
Use a PropertyPlaceholderConfigurer to load properties from a .properties file on the classpath. The easiest way is through <context:property-placeholder location=".." />
Add the query.timeout=x in your .properties files

Spring JMX. Set the default value of #ManagedAtrribute through XML

I'm using Spring 3.0.5
I have a #ManagedResource bean, for some of the #ManagedAttribute methods which I want to set a defaultValue. Instead of setting it to a hardcoded value I want to be able to read it from a property value at load time, since the default changes from environment to environment.
A snippet from my programs applicationContext.xml:
<context:mbean-export default-domain="sampleApp"/>
<bean id="managedBean" class="com.example.sampleBean">
<constructor-arg value="Sample Bean"/>
<constructor-arg value="${sample.property}"/>
</bean>
I believe I have to use the XML configuration to be able to do this, but haven't figured out how to do it yet.
You can add the following to your applicationContext.xml, it should expose the properties you are after:
<context:property-placeholder location="classpath:application.properties"/>
So if the application.properties file you are pointing to above contains a property called sample.property then Spring will use that to inject into your ${sample.property} placeholder.
For more details you can see the Spring reference here.

Spring, db and property file configuration

I have some JDO objects that I want to spring to configure with info from a property file.
But since spring isn't used to create (i.e these objects are not listed as beans in the xml. Should they, how would it look?) these objects how can I get it to configure those objects?
Only solution I can come up with is to have the property file info configured into the dao and then have the dao insert that data into the object before it returns it. Or I guess I can do some AOP magic, but that seems heavy handed and I don't think it will work in Google App Engine where this service will be deployed.
Any other advice.
You can put any bean in applicationContext.xml, and configure all of its properties there.
The properties file can be loaded via:
<context:property-placeholder location="classpath:application.properties" />
and then, on your bean definition:
<property name="propertyName" value="${valueFromPropertiesFile}" />
Then, in order to have the properly configured bean, you will have to inject it - either in the applicationContext.xml, or via #Resource / #Autowired
But if you can't let spring create, and configure your beans, then simply populate them with your properties manually - load a properties file with java.util.Properties, and fill the data needed.
I have some JDO objects that I want to spring to configure with info from a property file.
I don't get the whole idea. Are these objects persistent or not? If they are, just load them from the datastore. If not, they aren't really JDO objects as pointed out in comments. And in that case, I don't understand the point of the DAO and of the property file. Why don't you just declare them as Spring beans?

Categories

Resources