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> 
1

3 Answers 3

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'] }"/> 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.