Loading property file from filesystem using util:properties? - spring

When i try to load a property file from the filesystem using util:properties, the property file is not found. I placed it on my root drive and all kinds of places but i seem not to be able to load a property file from the filesystem using util:properties.
Is it possible to do this?
<util:properties id="configProperties" location="file:///d:/config.properties"/>
Loading it from the classpath works ok.. but i want to configure the application so a system admin can change the properties.
<util:properties id="configProperties" location="classpath:/config.properties"/>

I have this in a Linux project and it works fine:
<util:properties id="jdbcConfiguration" location="file:///home/reporting/jdbc.properties"/>
so the problem is probably with the way you specify the path. Try it this way:
<util:properties id="configProperties" location="file:d:/config.properties"/>

Related

Liberty server properties file setup

I have properties file in local to which I am reading in code by below method
String pathOfFile = System.getProperties("arg.get.prop");
How to set this system properties to get my property file's path in liberty server.xml
You can specify environment variables in the server.env file placed either in ${wlp.install.dir}/etc/server.env or ${server.config.dir}/server.env. The server will also pick up variables from the current shell environment (server.env files take precedence). Then you can access the variables in the server.xml using the following notation:
${env.<variable name>}
For example, you can have the following in your server.env file:
HTTP_PORT=9001
and then in your server.xml:
<httpEndpoint id="defaultHttpEndpoint"
httpPort="${env.HTTP_PORT}"
httpsPort="9443" />
For more information on customizing the Liberty environment see: https://www.ibm.com/support/knowledgecenter/en/SSAW57_liberty/com.ibm.websphere.wlp.nd.multiplatform.doc/ae/twlp_admin_customvars.html
If you need to define system property the recommended way is to use jvm.options file and put your property there like:
# Set a system property.
-Darg.get.prop=ExampleValue
you may need to create that file in the ${server.config.dir} directory. For some more details check Customizing the Liberty environment
if your property file is in "variable=value" format .. then, you can include in in bootstrap.properties file of your liberty install.
bootstrap.properties can be used to supply variable values to liberty configuration. you can include additional files by specifying
bootstrap.include=

Spring: locate the folder with properties using systemEnvironment variable

I have a Spring project named 'finman'.
The directory structure
As you can see on the image the folder with properties (named 'config') is on the same level as 'src'. So I need to locate that path using
systemEnvironment['FINMAN_ROOT']
The code fragment from 'spring-mvc.xml' responsible for the resource bundle location:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:cacheSeconds="60"
p:defaultEncoding="UTF-8">
<property name="basenames" value="file:///#{systemEnvironment['FINMAN_ROOT']}/config/messages/app"/>
</bean>
The problem is that I don't know how to set that variable.
Environment variables set in OS:
Windows: https://www.chem.gla.ac.uk/~louis/software/faq/q1.html
Linux: http://www.cyberciti.biz/faq/set-environment-variable-linux/
After it do not forget to restart java (or IDE), as they get environments, when started.

How to get the host name in spring configuration file?

Is there any easy way to get the host name in spring configuration file ? Currently I am using Java code to get the host name and and auto wire the property in the bean . But looking for less coding approach if any !
Thanks
The following will give you the hostname in java
return InetAddress.getLocalHost().getHostName();
where InetAddress belongs to the java.net package. You can add that to your java configuration file. If you want to do it in xml, you can do the following
<bean id="localhostInetAddress"
class="java.net.InetAddress"
factory-method="getLocalHost"/>
<bean id="hostname"
factory-bean="localhostInetAddress"
factory-method="getHostName"/>

Configuring Spring batch xml

Issue: Using spring batch, i need to read a file which has todays date. E.g test_02032015.txt.This file will be in a directory /test/example. Its an unix environment that i need to fetch file from.
question is how to configure spring batch xml so that above mentioned file is read
Any pointers to relevant website or solution would be of great help.
You have a few ways to address a requirement like this:
If you don't need to worry about the other files in a directory, you can just use a wild card in the file name like this:
<property name="resource" value="data/iosample/input/*.xml" />
Another alternative would be to pass the value into the job as a parameter and reference it like this:
<property name="resource" value="#{jobParameters['input.file']}" />
Finally you could use SpEL to build the file name (Sorry I don't have an example of that handy).

How do I load ${catalina.home}/conf/application.properties in a Spring / Tomcat webapp?

How do I load ${catalina.home}/conf/application.properties in a Spring / Tomcat webapp?
Looking around on StackOverflow and Google I see many discussions which claim it's possible. However, it's just not working for me. In line with the advice from my research my Spring applicationContext.xml file contains the following line:
<context:property-placeholder location="${catalina.home}/conf/application.properties"/>
But I get this in the logs:
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/Users/username/Servers/apache-tomcat-6.0.36/conf/application.properties]
From the log entry I can see that ${catalina.home} is expanding correctly. When I expand it by hand in the applicationContext.xml file it returns the same error. The following returns the contents of the application.properties file as expected:
cat /Users/username/Servers/apache-tomcat-6.0.36/conf/application.properties
So the path is clearly correct. Is this a webapp security or Tomcat server configuration issue?
The location of a context:property-placeholder is a Resource, which means that if you provide just a file path (as opposed to a full URL with a protocol) then the path will be resolved against the base directory of the webapp - it is trying to load /Users/username/Servers/apache-tomcat-6.0.36/webapps/<appname>/Users/username/Servers/apache-tomcat-6.0.36/conf/application.properties, which does not exist. If you prefix it with file: it'll work as you require:
<context:property-placeholder location="file:${catalina.home}/conf/application.properties"/>
For annotation based configuration you can use:
#PropertySource("file:${catalina.home}/conf/application.properties")

Resources