【问题标题】:Getting the present working directory using spring使用spring获取当前工作目录
【发布时间】:2013-05-14 19:25:54
【问题描述】:

这是在运行时获取 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>

【问题讨论】:

标签: java spring


【解决方案1】:

spring-context.xml你可以使用

1) classpath:filename.properties

2)./filename.properties

3)file:./

对于context-xml 的当前目录,./ 应该可以工作,但对于工作目录,file:./ 可以正常工作。

例如。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      是的,您可以使用 Spring 表达式来实现。见this文章第6.4.1节

      <property name="userDir" value="#{ systemProperties['user.dir'] }"/>
      <property name="fileSep" value="#{ systemProperties['file.separator'] }"/>
      

      【讨论】:

        猜你喜欢
        • 2013-11-18
        • 2012-07-22
        • 2010-09-18
        • 1970-01-01
        • 1970-01-01
        • 2013-06-25
        • 1970-01-01
        • 2020-06-03
        • 2016-04-05
        相关资源
        最近更新 更多