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
Related
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.
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.
I was wondering if there is any way that we can set the value of environment variable dynamically without restarting the server so that different property files might get accessed based on the value of currently set environment.
Consider these are my two property files:
config-dev.properties
config-prod.properties
Now since I have appended the name of environment with every file, so I want to know that is it possible that I dynamically change or set the value of environment name and the corresponding property file will be used without restarting the server.
I know how to set the env.name in configuration XML file.
<context:property-placeholder location="classpath:/config-${env.name}.properties>
I want to change the environment name programmatically so the correct property file can be used when it is accessed.
<context:property-placeholder location="classpath:/config-${env.name}.properties> is one way to do it. where your env.name is the run configuration can be the startup parameter you set.
Another way to do it is to use the Spring bean Profile. where you define a profile and the beans defined for those profile would be loaded during the Spring container startup.
<beans profile="dev">
<bean .... />
<context:property-placeholder
location="classpath:conf/properties/dev.properties" />
</beans>
<beans profile="prod">
<bean .... />
<context:property-placeholder
location="classpath:conf/properties/prod.properties" />
</beans>
I am not sure about changing it without restarting the server. since your Spring container has already started, and the beans are already loaded. now you do want to load a different set of beans without restarting the Spring container. I am not sure about that.
The basic reason for that according to me would be upon, how do you change the already autowired dependencies during runtime without affecting any other running application classes? What about a functionality which is being executed and you want to change the DB URL through the switch in the properties file? How do you control that?
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.
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?