7

This is the code to get the present working directory of a java application at runtime.

String currentWorkingDirectory = System.getProperty("user.dir")+System.getProperty("file.separator");

Is there any way by which this can be configured using the spring-context xml.

For ex:

<bean id="csvReportGenerator" class="some.path.CSVReportGenerator">  
<constructor-arg name="outputFileName" value="${currentWorkingDirectory}/${reportOutputFileGeneric}"/>
</bean>
Vaibhav Jain
  • 3,729
  • 3
  • 25
  • 42
Saikat
  • 548
  • 1
  • 4
  • 12
  • This might help http://stackoverflow.com/questions/3965446/how-to-read-system-environment-variable-in-spring-applicationcontext – seenukarthi May 20 '13 at 09:13

3 Answers3

8

Yes, you can do it using Spring expressions. See section 6.4.1 of this article

<property name="userDir" value="#{ systemProperties['user.dir'] }"/>
<property name="fileSep" value="#{ systemProperties['file.separator'] }"/>
sanbhat
  • 17,522
  • 6
  • 48
  • 64
1

You can simply use classpath: or can use ./ if you are deploying in an unix environment(which usually is). Say, classpath:sample.properties or ./sample.properties

Arun
  • 2,562
  • 6
  • 25
  • 43
1

In spring-context.xml You can use

1) classpath:filename.properties or

2) ./filename.properties

3) file:./

For current dir of context-xml, ./ should work, but for working dir, file:./ works fine.

eg.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:annotation-config />

    <bean id="properties"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="singleton" value="true" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="locations">
            <list>
                <value>classpath:/shaharma.properties</value>
                <value>./shaharma-custom.properties</value>
            </list>
        </property>
    </bean>

</beans>
prayagupa
  • 30,204
  • 14
  • 155
  • 192