Java 如何在Spring中获取当前目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18238505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:38:53  来源:igfitidea点击:

How to get current directory in Spring?

javastringspringdirectory

提问by Tony

I've got a little project in Spring. It's located (for example) in C:/users/projects/blog. How can I get this path?

我在 Spring 有一个小项目。它位于(例如)中C:/users/projects/blog。我怎样才能得到这条路?

采纳答案by vinay

Java cannot determine the root of your project without projects meta data. You can get the absolute path to your current class executing the code using the following : YourClassName.class.getResource("").getPath()

如果没有项目元数据,Java 无法确定项目的根。您可以使用以下命令获取执行代码的当前类的绝对路径:YourClassName.class.getResource("").getPath()

回答by aim

Maybe this what you looking for:

也许这就是你要找的:

System.getProperty("user.dir")

This property returns user working directory for application.

此属性返回应用程序的用户工作目录。

回答by Ankit

Did you try using a properties file. here is an example

您是否尝试使用属性文件。这是一个例子

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>file:${path}</value>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

while running your code you will need to pass in -Dpath=C:\users\projects\blog as an argument

在运行您的代码时,您需要传入 -Dpath=C:\users\projects\blog 作为参数