7

这是在运行时获取 java 应用程序当前工作目录的代码。

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

有什么方法可以使用 spring-context xml 进行配置。

例如:

<bean id="csvReportGenerator" class="some.path.CSVReportGenerator">  
<constructor-arg name="outputFileName" value="${currentWorkingDirectory}/${reportOutputFileGeneric}"/>
</bean>
4

3 回答 3

8

是的,您可以使用 Spring 表达式来做到这一点。见本文第6.4.1

<property name="userDir" value="#{ systemProperties['user.dir'] }"/>
<property name="fileSep" value="#{ systemProperties['file.separator'] }"/>
于 2013-05-20T09:13:56.710 回答
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>
于 2015-01-12T13:05:58.760 回答
1

如果您在 unix 环境(通常是)中部署,您可以简单地使用classpath:或可以使用。./说,classpath:sample.properties或者./sample.properties

于 2013-05-20T09:19:43.877 回答